/as3logger/src/de/dev_lab/logging/publisher/SOSPublisher.as

http://as3logger.googlecode.com/ · ActionScript · 156 lines · 84 code · 20 blank · 52 comment · 6 complexity · 57594bae1a26c9e357ee87006cf913d3 MD5 · raw file

  1. package de.dev_lab.logging.publisher
  2. {
  3. import de.dev_lab.logging.Logger;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.events.SecurityErrorEvent;
  7. import flash.net.XMLSocket;
  8. /**
  9. * Publisher for Powerflasher Socket Output Server (SOS).
  10. *
  11. * http://sos.powerflasher.com/
  12. *
  13. * @author Sebastian Weyrauch
  14. *
  15. * LastChanged:
  16. *
  17. * $Author: sebastian.weyrauch $
  18. * $Revision: 10 $
  19. * $LastChangedDate: 2010-07-04 20:34:28 +0200 (Sun, 04 Jul 2010) $
  20. * $URL: http://as3logger.googlecode.com/svn/trunk/as3logger/src/de/dev_lab/logging/publisher/SOSPublisher.as $
  21. *
  22. */
  23. public class SOSPublisher implements IPublisher
  24. {
  25. private var _socket : XMLSocket;
  26. private var _queue : Array;
  27. private var _clearOnConnect : Boolean;
  28. /**
  29. * Constructor. Creates the socket and the connects to it.
  30. *
  31. * @param clearOnConnect <code>true</code> if the output shall be reset on connect,
  32. * <code>false</code> if not
  33. * @param hostname The hostname to connect
  34. * @param port The port to connect
  35. */
  36. public function SOSPublisher( clearOnConnect : Boolean = true , hostname : String = "localhost" , port : int = 4445 )
  37. {
  38. _clearOnConnect = clearOnConnect;
  39. _queue = [];
  40. _socket = new XMLSocket;
  41. _socket.addEventListener( Event.CONNECT , onConnect );
  42. _socket.addEventListener( IOErrorEvent.IO_ERROR , onError );
  43. _socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onError );
  44. _socket.connect( hostname , port );
  45. }
  46. /**
  47. * Publishs a message. If the socket is not connect yet,
  48. * it will be added to a queue.
  49. *
  50. * @param logLevel Log level
  51. * @param message Message
  52. */
  53. public function publish ( logLevel : int , object : * , ...additional ) : void
  54. {
  55. var message : String = String ( object );
  56. if( message.length == 0 ) return;
  57. if ( _socket.connected )
  58. {
  59. sendToSocket ( logLevel , message );
  60. }
  61. else
  62. {
  63. if ( _queue ) _queue.push ( new LogObject ( logLevel , message ) );
  64. }
  65. }
  66. /**
  67. * Resets the output of SOS.
  68. */
  69. public function clear () : void
  70. {
  71. _socket.send( "<clear/>\n" );
  72. }
  73. /**
  74. * Disconnects from the socket.
  75. */
  76. public function destroy () : void
  77. {
  78. _socket.removeEventListener( IOErrorEvent.IO_ERROR , onError );
  79. _socket.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onError );
  80. _socket.removeEventListener( Event.CONNECT , onConnect );
  81. try
  82. {
  83. _socket.close();
  84. }
  85. catch ( error : Error )
  86. {
  87. trace( error );
  88. }
  89. }
  90. /**
  91. * Sends a message to the socket.
  92. *
  93. * @param logLevel Log level
  94. * @param message Message
  95. */
  96. private function sendToSocket ( logLevel : int, message : String ) : void
  97. {
  98. var xmlMessage : String = "<showMessage key=\"" + Logger.getLogType ( logLevel ) + "\"/> " + message;
  99. _socket.send( xmlMessage + "\n");
  100. }
  101. /**
  102. * Handler for Socket connect.
  103. * Sends all messages in the queue.
  104. *
  105. * @param event
  106. */
  107. private function onConnect ( event : Event ) : void
  108. {
  109. if ( _clearOnConnect ) clear();
  110. for each ( var logObject : LogObject in _queue )
  111. {
  112. sendToSocket ( logObject.logLevel , logObject.message );
  113. }
  114. _queue = null;
  115. }
  116. /**
  117. * Handler for Socker errors.
  118. * Destroys the publisher.
  119. */
  120. private function onError ( event : Event ) : void
  121. {
  122. _queue = null;
  123. destroy();
  124. }
  125. }
  126. }
  127. internal class LogObject
  128. {
  129. public var logLevel : int;
  130. public var message : String;
  131. public function LogObject ( logLevel : int , message : String )
  132. {
  133. this.logLevel = logLevel;
  134. this.message = message;
  135. }
  136. }