Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
10 KiB

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import mqoa.*;
  5. import com.ms.com.*;
  6. import com.ms.io.*;
  7. import java.io.*;
  8. import com.ms.wfc.ui.MessageBox;
  9. //
  10. //
  11. // DistDraw-
  12. // This Class is used to manage all the creation and managment of MSMQ queues
  13. // and events. It also uses to manage the sending and receiving of messages.
  14. //
  15. //
  16. public class DistDraw extends Frame implements ScribbleL, ActionListener, _DMSMQEventEvents
  17. {
  18. ScribbleC scribble = null;
  19. TextField txtSend = null;
  20. TextField txtReceive = null;
  21. Button sendButton = null;
  22. MenuItem menuConnect = null;
  23. MenuItem menuClear = null;
  24. TextField statusBar = null;
  25. IMSMQQueue msmqSend = null;
  26. IMSMQQueue msmqReceive = null;
  27. MSMQEvent msmqEvent = null;
  28. String loginName = null;
  29. private ConnectionPointCookie cookie = null;
  30. public static void main(String[] args)
  31. {
  32. DistDraw st = new DistDraw();
  33. //
  34. // Check DS status.
  35. //
  36. if (!st.IsDsEnabledLocally())
  37. {
  38. MessageBox.show("This application is not designed to run on DS-Disabled environment", "Error", MessageBox.OK);
  39. System.exit(0);
  40. }
  41. st.setSize(320, 200);
  42. st.show();
  43. st.start();
  44. }
  45. public boolean IsDsEnabledLocally()
  46. {
  47. IMSMQApplication2 qapp2 = (IMSMQApplication2) new MSMQApplication();
  48. boolean fIsDsEnabeledLocaly = qapp2.getIsDsEnabled() ;
  49. return fIsDsEnabeledLocaly;
  50. }
  51. public DistDraw()
  52. {
  53. setLayout(new BorderLayout());
  54. scribble = new ScribbleC();
  55. scribble.addScribbleListener(this);
  56. add("Center", scribble);
  57. // Create and add text panel
  58. Panel textPanel = new Panel();
  59. textPanel.setLayout(new BorderLayout(4,4));
  60. txtSend = new TextField(25);
  61. txtReceive = new TextField(25);
  62. txtReceive.setEditable(false);
  63. sendButton = new Button("Send");
  64. textPanel.add("Center", txtSend);
  65. textPanel.add("East", sendButton);
  66. textPanel.add("South", txtReceive);
  67. sendButton.addActionListener(this);
  68. add("North", textPanel);
  69. // Add menu for connecting and clearing screen
  70. Menu fileMenu = new Menu("File");
  71. Menu viewMenu = new Menu("View");
  72. menuConnect = new MenuItem("Connect");
  73. menuClear = new MenuItem("Clear");
  74. fileMenu.add(menuConnect);
  75. viewMenu.add(menuClear);
  76. MenuBar menuBar = new MenuBar();
  77. menuBar.add(fileMenu);
  78. menuBar.add(viewMenu);
  79. setMenuBar(menuBar);
  80. menuConnect.addActionListener(this);
  81. menuClear.addActionListener(this);
  82. //Add status bar
  83. statusBar = new TextField();
  84. statusBar.setEditable(false);
  85. add("South", statusBar);
  86. // Enable events for closing window
  87. enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  88. }
  89. public void start()
  90. {
  91. NameDlg d = new NameDlg(this, "Please Enter Your Name", "Login");
  92. d.pack();
  93. d.show();
  94. loginName = d.getName();
  95. setTitle(loginName);
  96. createReceiveQueue(loginName);
  97. }
  98. public void processWindowEvent(WindowEvent e)
  99. {
  100. if(e.getID() == e.WINDOW_CLOSING)
  101. {
  102. System.exit(0);
  103. }
  104. }
  105. // Callback for the ScribbleC (scribble canvas) class
  106. public void strokeCreated(Stroke stroke)
  107. {
  108. sendData(stroke, 3);
  109. }
  110. public void sendData(Object data, int priority)
  111. {
  112. // Check for send queue
  113. if(msmqSend != null)
  114. {
  115. // Create safe array based on stroke
  116. SafeArrayOutputStream safeOutStream = new SafeArrayOutputStream();
  117. ObjectOutputStream objOutStream = null;
  118. try
  119. {
  120. objOutStream= new ObjectOutputStream(safeOutStream);
  121. objOutStream.writeObject(data);
  122. objOutStream.flush();
  123. safeOutStream.close();
  124. }
  125. catch(Exception e)
  126. {
  127. System.out.println("Error in ObjectOutputStream");
  128. }
  129. SafeArray bytes = safeOutStream.getSafeArray();
  130. // Create message to send
  131. IMSMQMessage msmqMsg=(IMSMQMessage)new MSMQMessage();
  132. Variant msgbody=new Variant();
  133. msgbody.putSafeArray ( bytes );
  134. msmqMsg.setBody ( msgbody );
  135. msmqMsg.setPriority( priority);
  136. Variant noParam = new Variant();
  137. noParam.noParam();
  138. msmqMsg.Send ( msmqSend, noParam);
  139. }
  140. }
  141. // Callback for the connect menu
  142. public void actionPerformed(ActionEvent e)
  143. {
  144. if(e.getSource() == menuConnect)
  145. {
  146. // Connect to friend queue
  147. NameDlg nd = new NameDlg(this, "Please Enter Friends Name", "Enter");
  148. nd.pack();
  149. nd.show();
  150. if(nd.getName() != "")
  151. {
  152. System.out.println(nd.getName());
  153. boolean retval = createSendQueue(nd.getName());
  154. if(retval)
  155. setTitle(loginName + " connected to "+nd.getName());
  156. }
  157. }
  158. else if(e.getSource() == menuClear)
  159. {
  160. scribble.clear();
  161. sendData(new ClrAction(), 6);
  162. }
  163. else if(e.getSource() == sendButton)
  164. {
  165. // Send text
  166. sendData(txtSend.getText(), 5);
  167. }
  168. }
  169. public void createReceiveQueue(String queueName)
  170. {
  171. Variant noParam = new Variant();
  172. Variant vLabel = new Variant() ;
  173. noParam.noParam ();
  174. vLabel.putString (queueName) ;
  175. IMSMQQuery query = (IMSMQQuery)new MSMQQuery();
  176. IMSMQQueueInfos qinfos = null;
  177. IMSMQQueueInfo qinfo = null;
  178. showStatus("Looking up Queue "+queueName);
  179. // Search for queues with this name
  180. qinfos = query.LookupQueue (noParam, noParam, vLabel, noParam, noParam, noParam, noParam, noParam, noParam) ;
  181. qinfos.Reset();
  182. qinfo = qinfos.Next();
  183. if(qinfo != null)
  184. {
  185. // Get the queue if it exists
  186. try
  187. {
  188. showStatus("Attempting to open Queue "+queueName);
  189. msmqReceive=qinfo.Open ( MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE );
  190. }
  191. catch(Exception e)
  192. {
  193. showStatus("Attempt to open "+queueName+" failed");
  194. System.out.println(e.toString());
  195. }
  196. }
  197. else
  198. {
  199. // Create a queue
  200. Variant isTransactional;
  201. Variant IsWorldReadable;
  202. qinfo = (IMSMQQueueInfo) new MSMQQueueInfo();
  203. qinfo.setPathName(".\\"+queueName); // setPathName using JactiveX, putPathName using Typelib wizard
  204. isTransactional = new Variant();
  205. isTransactional.putBoolean(false);
  206. IsWorldReadable = new Variant();
  207. IsWorldReadable.putBoolean(false);
  208. qinfo.setLabel(queueName);
  209. try
  210. {
  211. showStatus("Creating Queue named "+queueName);
  212. qinfo.Create(IsWorldReadable, isTransactional);
  213. }
  214. catch(Exception e)
  215. {
  216. System.out.println(e.toString());
  217. }
  218. showStatus("Opening Queue named "+queueName);
  219. msmqReceive=qinfo.Open ( MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE );
  220. }
  221. // Enable notifications on the receive queue
  222. if ( msmqReceive != null )
  223. {
  224. msmqEvent = new MSMQEvent();
  225. showStatus("Enabling Notification for "+queueName);
  226. msmqReceive.EnableNotification((IMSMQEvent)msmqEvent, noParam, noParam);
  227. try
  228. {
  229. cookie = new ConnectionPointCookie(msmqEvent, this, Class.forName("mqoa._DMSMQEventEvents"));
  230. showStatus("Done.");
  231. }
  232. catch(Exception ex)
  233. {
  234. showStatus("Unable setup Notification");
  235. }
  236. }
  237. }
  238. public boolean createSendQueue(String queueName)
  239. {
  240. Variant noParam = new Variant();
  241. Variant vLabel = new Variant() ;
  242. noParam.noParam ();
  243. vLabel.putString (queueName) ;
  244. IMSMQQuery query = (IMSMQQuery)new MSMQQuery();
  245. IMSMQQueueInfos qinfos = null;
  246. IMSMQQueueInfo qinfo = null;
  247. // Search for queues with this name
  248. showStatus("Looking up Queue "+queueName);
  249. qinfos = query.LookupQueue (noParam, noParam, vLabel, noParam, noParam, noParam, noParam, noParam, noParam) ;
  250. qinfos.Reset();
  251. qinfo = qinfos.Next();
  252. if ( qinfo != null )
  253. {
  254. // We have at least one queue in this list
  255. // Get the queue if it exists
  256. showStatus("Attempting to open Queue "+queueName);
  257. msmqSend=qinfo.Open ( MQACCESS.MQ_SEND_ACCESS, MQSHARE.MQ_DENY_NONE );
  258. if ( msmqSend == null )
  259. {
  260. // We were not sucessful at opening the queue
  261. showStatus("Attempt to open "+queueName+" failed");
  262. System.out.println("Cannot open existing queue");
  263. return false;
  264. }
  265. else
  266. {
  267. showStatus("Done.");
  268. return true;
  269. }
  270. }
  271. else
  272. {
  273. showStatus("Cannot find \"Friend\" "+queueName);
  274. return false;
  275. }
  276. }
  277. // Event handlers for DMSMQEventEvents
  278. public void Arrived(Object pdispQueue, int cursor)
  279. {
  280. IMSMQMessage msmqMsg;
  281. Variant wantbody=new Variant();
  282. wantbody.putBoolean( true );
  283. Variant noParam = new Variant();
  284. noParam.noParam();
  285. try
  286. {
  287. showStatus("Message arrived.");
  288. msmqMsg=msmqReceive.Receive ( noParam, noParam, wantbody, noParam);
  289. if ( msmqMsg != null )
  290. {
  291. // Get Variant from message and convert to SafeArray
  292. Variant body = msmqMsg.getBody();
  293. SafeArray bytes = body.toSafeArray();
  294. SafeArrayInputStream safeInStream = new SafeArrayInputStream(bytes);
  295. ObjectInputStream objInStream = new ObjectInputStream(safeInStream);
  296. Object data = objInStream.readObject();
  297. if(data instanceof Stroke)
  298. scribble.addStroke((Stroke)data);
  299. else if(data instanceof String)
  300. txtReceive.setText((String)data);
  301. else if(data instanceof ClrAction)
  302. scribble.clear();
  303. }
  304. }
  305. catch(Exception e)
  306. {
  307. showStatus("An error occured while processing message");
  308. System.out.println(e.toString());
  309. }
  310. showStatus("Done.");
  311. //Reenable notifications from the input queue
  312. msmqReceive.EnableNotification((IMSMQEvent)msmqEvent, noParam, noParam);
  313. }
  314. public void ArrivedError(Object pdispQueue, int lErrorCode, int cursor)
  315. {
  316. showStatus("An error occurred.");
  317. Variant noParam = new Variant();
  318. noParam.noParam ();
  319. //Reenable notifications from the input queue
  320. msmqReceive.EnableNotification((IMSMQEvent)msmqEvent, noParam, noParam);
  321. }
  322. public void showStatus(String txt)
  323. {
  324. statusBar.setText(txt);
  325. }
  326. }
  327. //
  328. //
  329. // SafeArrayOutputStream -
  330. // This class is used to convert data from an OutputStream to a SafeArray .
  331. //
  332. //
  333. class SafeArrayOutputStream extends ByteArrayOutputStream
  334. {
  335. public SafeArray getSafeArray()
  336. {
  337. // Allocate an array the size of the vector
  338. byte[] byteArray = toByteArray();
  339. // Create the safearray
  340. SafeArray array = new SafeArray(Variant.VariantByte,
  341. byteArray.length);
  342. // Fill in safearray
  343. array.setBytes(0, byteArray.length, byteArray, 0);
  344. return array;
  345. }
  346. }
  347. //
  348. //
  349. // SafeArrayInputStream-
  350. // This class is used to convert data from a SafeArray
  351. // to an InputStream.
  352. //
  353. //
  354. class SafeArrayInputStream extends ByteArrayInputStream
  355. {
  356. public SafeArrayInputStream(SafeArray array)
  357. {
  358. super(array.toByteArray());
  359. }
  360. }