Class: Secure::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/secure_con.rb,
lib/appswarm/secure_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientsObject (readonly)

Returns the value of attribute clients.



37
38
39
# File 'lib/appswarm/secure_connection.rb', line 37

def clients
  @clients
end

#connectionsObject (readonly)

Returns the value of attribute connections.



124
125
126
# File 'lib/appswarm/secure_con.rb', line 124

def connections
  @connections
end

#messagesObject (readonly)

Returns the value of attribute messages.



123
124
125
# File 'lib/appswarm/secure_con.rb', line 123

def messages
  @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

#hostObject



147
148
149
# File 'lib/appswarm/secure_con.rb', line 147

def host
  @iserver.host
end

#killObject



54
55
56
# File 'lib/appswarm/secure_connection.rb', line 54

def kill
  @mainThread.kill if @mainThread
end

#portObject



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

#runObject



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

#stopObject



136
137
138
139
# File 'lib/appswarm/secure_con.rb', line 136

def stop
  @iserver.stop
  @threads.each{|th|th.kill}
end