Class: N::Sync::Server
- Inherits:
-
Application
- Object
- Application
- N::Sync::Server
- Defined in:
- lib/n/sync/server.rb
Overview
Server
A Synchronous server tha communicates with clients using application specific xml protocols. Typically communicates with Flash clients utilising the XmlSocket functionality to enable ‘push’ flash applications.
The Macromedia XML Socket protocol is used, 000 is used as a eof marker. NO: the XML protocol is not really appropriate, better use a space optimized delimited ASCII protocol.
Constant Summary collapse
- MONITOR_INTERVAL =
2 * 60
- STATUS_IDLE =
0
- STATUS_RUNNING =
10
- STATUS_STOPPED =
20
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
the listening address/port for this server.
-
#handler_class ⇒ Object
readonly
the handler class, used to instantiate handlers.
-
#handlers ⇒ Object
the handler list, all handler attached to the server.
-
#max_handlers ⇒ Object
maximum number of clients to connect.
-
#port ⇒ Object
readonly
the listening address/port for this server.
-
#status ⇒ Object
status.
-
#tcp_server ⇒ Object
readonly
a single tcp server accepts all tcp requests.
Attributes inherited from Application
#create_time, #daemonized, #description, #name, #pid, #pidfile, #title, #version
Instance Method Summary collapse
- #broadcast(cmd) ⇒ Object
- #end_monitor ⇒ Object
-
#initialize(address = "localhost", port = 2121, handler_class = N::Sync::Handler, max_handlers = nil) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
-
#send(cmd, handler) ⇒ Object
——————————————————————.
- #shutdown ⇒ Object
- #start ⇒ Object
-
#start_monitor ⇒ Object
——————————————————————.
- #stop ⇒ Object
Methods inherited from Application
#daemon_pid, #daemonize, #parse_arguments, #restart
Constructor Details
#initialize(address = "localhost", port = 2121, handler_class = N::Sync::Handler, max_handlers = nil) ⇒ Server
Returns a new instance of Server.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/n/sync/server.rb', line 69 def initialize(address = "localhost", port = 2121, handler_class = N::Sync::Handler, max_handlers = nil) @address, @port = address, port @max_handlers = max_handlers @name, @title = "nsync", "Navel Sync" @status = STATUS_IDLE @handlers = N::SafeArray.new() @handler_class = handler_class super() end |
Instance Attribute Details
#address ⇒ Object (readonly)
the listening address/port for this server.
51 52 53 |
# File 'lib/n/sync/server.rb', line 51 def address @address end |
#handler_class ⇒ Object (readonly)
the handler class, used to instantiate handlers
54 55 56 |
# File 'lib/n/sync/server.rb', line 54 def handler_class @handler_class end |
#handlers ⇒ Object
the handler list, all handler attached to the server.
60 61 62 |
# File 'lib/n/sync/server.rb', line 60 def handlers @handlers end |
#max_handlers ⇒ Object
maximum number of clients to connect.
57 58 59 |
# File 'lib/n/sync/server.rb', line 57 def max_handlers @max_handlers end |
#port ⇒ Object (readonly)
the listening address/port for this server.
51 52 53 |
# File 'lib/n/sync/server.rb', line 51 def port @port end |
#status ⇒ Object
status
63 64 65 |
# File 'lib/n/sync/server.rb', line 63 def status @status end |
#tcp_server ⇒ Object (readonly)
a single tcp server accepts all tcp requests
48 49 50 |
# File 'lib/n/sync/server.rb', line 48 def tcp_server @tcp_server end |
Instance Method Details
#broadcast(cmd) ⇒ Object
154 155 156 157 158 |
# File 'lib/n/sync/server.rb', line 154 def broadcast(cmd) for handler in @handlers handler.write(cmd) end end |
#end_monitor ⇒ Object
144 145 146 |
# File 'lib/n/sync/server.rb', line 144 def end_monitor Thread.kill(@monitor) end |
#run ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/n/sync/server.rb', line 93 def run() @status = STATUS_RUNNING $log.info "Server is running." begin while (STATUS_RUNNING == @status) socket = @tcp_server.accept() $log.debug "Socket accepted" if $DBG handler = @handler_class.new(self, socket) handlers << handler handler.start() end rescue Exception, StandardError => e $log.error pp_exception(e) # tml, FIXME: # this is dangerous! make more FAULT TOLERANT! end @status = STATUS_IDLE end |
#send(cmd, handler) ⇒ Object
150 151 152 |
# File 'lib/n/sync/server.rb', line 150 def send(cmd, handler) handler.write(cmd) end |
#shutdown ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/n/sync/server.rb', line 114 def shutdown() end_monitor() for handler in handlers handler.stop() end Thread.exit() end |
#start ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/n/sync/server.rb', line 81 def start() # a single tcp server accepts all tcp requests @tcp_server = TCPServer.new(address, port) start_monitor() run() end |
#start_monitor ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/n/sync/server.rb', line 124 def start_monitor @monitor = Thread.new { begin while true sleep(MONITOR_INTERVAL) $log.debug "monitor beat -- #{@handlers.size()} handlers" if $DBG for handler in @handlers unless handler.live? $log.info "Idle handler detected!" handler.gc!() end end end rescue Exception, StandardError => e $log.error pp_exception(e) end } end |
#stop ⇒ Object
89 90 91 |
# File 'lib/n/sync/server.rb', line 89 def stop() @status = STATUS_STOPED end |