Class: N::Sync::Server

Inherits:
Application show all
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

Attributes inherited from Application

#create_time, #daemonized, #description, #name, #pid, #pidfile, #title, #version

Instance Method Summary collapse

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

#addressObject (readonly)

the listening address/port for this server.



51
52
53
# File 'lib/n/sync/server.rb', line 51

def address
  @address
end

#handler_classObject (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

#handlersObject

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_handlersObject

maximum number of clients to connect.



57
58
59
# File 'lib/n/sync/server.rb', line 57

def max_handlers
  @max_handlers
end

#portObject (readonly)

the listening address/port for this server.



51
52
53
# File 'lib/n/sync/server.rb', line 51

def port
  @port
end

#statusObject

status



63
64
65
# File 'lib/n/sync/server.rb', line 63

def status
  @status
end

#tcp_serverObject (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_monitorObject



144
145
146
# File 'lib/n/sync/server.rb', line 144

def end_monitor
	Thread.kill(@monitor)
end

#runObject



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

#shutdownObject



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

#startObject



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_monitorObject




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

#stopObject



89
90
91
# File 'lib/n/sync/server.rb', line 89

def stop()
	@status = STATUS_STOPED
end