Class: Secure::Server
- Inherits:
-
Object
- Object
- Secure::Server
- Defined in:
- lib/appswarm/secure_con.rb,
lib/appswarm/secure_connection.rb
Instance Attribute Summary collapse
-
#clients ⇒ Object
readonly
Returns the value of attribute clients.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
- #addConnection(con) ⇒ Object
- #connect(there, port) ⇒ Object
- #eventConnected(con) ⇒ Object
- #hook(&b) ⇒ Object
- #hookConnected(&hook) ⇒ Object
- #hookDisconnected(&b) ⇒ Object
- #host ⇒ Object
-
#initialize(port, host = "localhost") ⇒ Server
constructor
A new instance of Server.
- #kill ⇒ Object
- #port ⇒ Object
- #receive(from, xy) ⇒ Object
- #removeConnection(con) ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(port, host = "localhost") ⇒ Server
Returns a new instance of Server.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/appswarm/secure_con.rb', line 125 def initialize(port) @iserver=IServer.new(self,port,"127.0.0.1",40) @iserver.audit = true @iserver.start @connections=[] @messages=[] @threads=[] @hook=nil @hookConnected=nil @hookDisconnected=nil end |
Instance Attribute Details
#clients ⇒ Object (readonly)
Returns the value of attribute clients.
37 38 39 |
# File 'lib/appswarm/secure_connection.rb', line 37 def clients @clients end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
124 125 126 |
# File 'lib/appswarm/secure_con.rb', line 124 def connections @connections end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
123 124 125 |
# File 'lib/appswarm/secure_con.rb', line 123 def @messages end |
Instance Method Details
#addConnection(con) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/appswarm/secure_con.rb', line 193 def addConnection(con) glog "addConnection #{con}" assert{con.is_a?(Connection)} @connections<<con eventConnected(con) end |
#connect(there, port) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/appswarm/secure_con.rb', line 167 def connect(there,port) puts "#{self}:CONNECTING #{there} #{port}" host=Socket.gethostbyname(there) sock=TCPSocket.new(there,port) c=Connection.new(sock,self) @connections<<c @threads<<Thread.new{ begin puts "RECEIVING" c.receiveLoop rescue Object=>e glog "Exception in connect-Thread " ,e end } c end |
#eventConnected(con) ⇒ Object
188 189 190 191 |
# File 'lib/appswarm/secure_con.rb', line 188 def eventConnected(con) glog "eventConnected #{con}" @hookConnected.call(con) if @hookConnected end |
#hook(&b) ⇒ Object
141 142 143 |
# File 'lib/appswarm/secure_con.rb', line 141 def hook(&b) @hook=b end |
#hookConnected(&hook) ⇒ Object
184 185 186 |
# File 'lib/appswarm/secure_con.rb', line 184 def hookConnected(&hook) @hookConnected=hook end |
#hookDisconnected(&b) ⇒ Object
144 145 146 |
# File 'lib/appswarm/secure_con.rb', line 144 def hookDisconnected(&b) @hookDisconnected=b end |
#host ⇒ Object
147 148 149 |
# File 'lib/appswarm/secure_con.rb', line 147 def host @iserver.host end |
#kill ⇒ Object
54 55 56 |
# File 'lib/appswarm/secure_connection.rb', line 54 def kill @mainThread.kill if @mainThread end |
#port ⇒ Object
150 151 152 |
# File 'lib/appswarm/secure_con.rb', line 150 def port @iserver.port end |
#receive(from, xy) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/appswarm/secure_con.rb', line 154 def receive(from,xy) #pp "RECVED:#{from}:#{xy}" if @xy==:closeConnection from.disconnect(false) elsif @hook #pp "calling hook" @hook.call(from,xy,Time.now) else #pp "pushing message" @messages<<[from,xy,Time.now] end end |
#removeConnection(con) ⇒ Object
199 200 201 202 203 |
# File 'lib/appswarm/secure_con.rb', line 199 def removeConnection(con) assert{con.is_a?(Connection)} @connections.delete(con) @hookDisconnected.call(con) end |
#run ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/appswarm/secure_connection.rb', line 58 def run @mainThread=Thread.new { client=@server.accept c=Client.new(client) @mutex.synchronize { @clients<<c } begin c.run rescue Exception=>e log e @mutex.synchronize { @clients.delete(c) } end } end |
#stop ⇒ Object
136 137 138 139 |
# File 'lib/appswarm/secure_con.rb', line 136 def stop @iserver.stop @threads.each{|th|th.kill} end |