PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/translation-helper/translation-helper-app/lib/adobe_air_sdk/frameworks/projects/air/Core/src/air/net/SocketMonitor.as

http://dn-translation.googlecode.com/
ActionScript | 201 lines | 83 code | 19 blank | 99 comment | 3 complexity | b5dfa249c44e33007a33f88b5e95aeed MD5 | raw file
  1. /*
  2. ADOBE SYSTEMS INCORPORATED
  3. Copyright ? 2008 Adobe Systems Incorporated. All Rights Reserved.
  4. NOTICE: Adobe permits you to modify and distribute this file only in accordance with
  5. the terms of Adobe AIR SDK license agreement. You may have received this file from a
  6. source other than Adobe. Nonetheless, you may modify or distribute this file only in
  7. accordance with such agreement.
  8. */
  9. package air.net
  10. {
  11. import flash.events.TimerEvent;
  12. import flash.events.Event;
  13. import flash.events.SecurityErrorEvent;
  14. import flash.events.IOErrorEvent;
  15. import flash.net.Socket;
  16. import flash.errors.IOError;
  17. /**
  18. * A SocketMonitor object monitors availablity of a TCP endpoint.
  19. *
  20. * <p platform="actionscript">This class is included in the aircore.swc file.
  21. * Flash Builder loads this class automatically when you create a project for AIR.
  22. * The Flex SDK also includes this aircore.swc file, which you should include
  23. * when compiling the application if you are using Flex SDK.
  24. * </p>
  25. *
  26. * <p platform="actionscript">In Adobe<sup>&#xAE;</sup> Flash<sup>&#xAE;</sup> CS3 Professional,
  27. * this class is included in the ServiceMonitorShim.swc file. To use classes in the air.net package ,
  28. * you must first drag the ServiceMonitorShim component from the Components panel to the
  29. * Library and then add the following <code>import</code> statement to your ActionScript 3.0 code:
  30. * </p>
  31. *
  32. * <listing platform="actionscript">import air.net.~~;</listing>
  33. *
  34. * <p platform="actionscript">To use air.net package in Adobe<sup>&#xAE;</sup> Flash<sup>&#xAE;</sup> Professional (CS4 or higher): </p>
  35. *
  36. * <ol platform="actionscript">
  37. * <li>Select the File &gt; Publish Settings command.</li>
  38. * <li>In the Flash panel, click the Settings button for ActionScript 3.0. Select Library Path.</li>
  39. * <li>Click the Browse to SWC File button. Browse to Adobe Flash CS<i>n</i>/AIK<i>n.n</i>/frameworks/libs/air/aircore.swc
  40. file in the Adobe Flash Professional installation folder.</li>
  41. * <li>Click the OK button.</li>
  42. * <li>Add the following <code>import</code> statement to your ActionScript 3.0 code: <code>import air.net.~~;</code></li>
  43. * </ol>
  44. *
  45. * <p platform="javascript">To use this class in JavaScript code, load the aircore.swf
  46. * file, as in the following:</p>
  47. *
  48. * <listing platform="javascript">&lt;script src="aircore.swf" type="application/x-shockwave-flash"&gt;</listing>
  49. *
  50. * @playerversion AIR 1.0
  51. *
  52. * @playerversion AIR 1.0
  53. */
  54. public class SocketMonitor extends ServiceMonitor
  55. {
  56. /**
  57. * Creates a SocketMonitor object for a specified TCP endpoint.
  58. *
  59. * <p>After creating a SocketMonitor object, the caller should call <code>start</code>
  60. * to begin monitoring the status of the service.</p>
  61. *
  62. * <p>As with the Timer object, the caller should maintain a reference to the SocketMonitor
  63. * object. Otherwise, the runtime deletes the object and monitoring ends.</p>
  64. *
  65. * @param host The host to monitor.
  66. * @param port The port to monitor.
  67. *
  68. * @playerversion AIR 1.0
  69. */
  70. public function SocketMonitor(host:String, port:int)
  71. {
  72. super();
  73. _host = host;
  74. _port = port;
  75. /* Variable_socket can be null if createSocket is overridden in derived classes.
  76. * For example, subclass SecureSocketMonitor returns null when the SecureSocket API is not supported.
  77. */
  78. if(!_socket)
  79. {
  80. stop();
  81. return;
  82. }
  83. function cool(event:Event):void
  84. {
  85. // trace(event);
  86. available = true;
  87. try
  88. {
  89. _socket.close();
  90. }
  91. catch(e:IOError)
  92. {
  93. // if we were not open...there is nothing to worry about
  94. }
  95. connecting = false;
  96. }
  97. function uncool(event:Event):void
  98. {
  99. // trace(event);
  100. available = false;
  101. try
  102. {
  103. _socket.close();
  104. }
  105. catch(e:IOError)
  106. {
  107. // if we were not open...there is nothing to worry about
  108. }
  109. connecting = false;
  110. }
  111. _socket.addEventListener(Event.CONNECT, cool);
  112. _socket.addEventListener(IOErrorEvent.IO_ERROR, uncool);
  113. _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, uncool);
  114. }
  115. private var connecting:Boolean = false;
  116. /**
  117. * The host being monitored.
  118. *
  119. * @playerversion AIR 1.0
  120. */
  121. public function get host():String
  122. {
  123. return _host
  124. }
  125. private var _host:String;
  126. /**
  127. * The port being monitored.
  128. *
  129. * @playerversion AIR 1.0
  130. */
  131. public function get port():int
  132. {
  133. return _port;
  134. }
  135. private var _port:int;
  136. /**
  137. * Calling the <code>checkStatus()</code> method of a SocketMonitor object causes
  138. * the application to try connecting to the socket, to check for a
  139. * <code>connect</code> event.
  140. *
  141. * @playerversion AIR 1.0
  142. */
  143. protected override function checkStatus():void
  144. {
  145. if(!_socket)
  146. {
  147. stop();
  148. return;
  149. }
  150. // trace('socket connect', _host, _port);
  151. // Thrashing around on the Socket.connect API spawns lots of threads.
  152. // So don't start a new one until the old one completes.
  153. if (connecting)
  154. return;
  155. connecting = true;
  156. _socket.connect(_host, _port);
  157. }
  158. /**
  159. * @inheritDoc
  160. *
  161. * @playerversion AIR 1.0
  162. */
  163. public override function toString():String
  164. {
  165. return '[SocketMonitor host="' + _host + '" port="' + _port +
  166. '" available="' + available + '"]';
  167. }
  168. /**
  169. * Creates a Socket object.
  170. *
  171. * @return the Socket object to be used by this SocketMonitor.
  172. *
  173. * @playerversion AIR 1.0
  174. */
  175. protected function createSocket():Socket
  176. {
  177. return new Socket();
  178. }
  179. private var _socket:Socket = createSocket();
  180. }
  181. }