Class: Secure::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, server) ⇒ Connection

Returns a new instance of Connection.



27
28
29
30
31
32
33
34
# File 'lib/appswarm/secure_con.rb', line 27

def initialize(conn,server)
  @conn=conn
  @data=""
  @server=server
  @conn.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  @valid=true
  @mutex=Mutex.new
end

Instance Attribute Details

#validObject (readonly)

Returns the value of attribute valid.



26
27
28
# File 'lib/appswarm/secure_con.rb', line 26

def valid
  @valid
end

Instance Method Details

#disconnect(notifyRemote = true) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/appswarm/secure_con.rb', line 69

def disconnect(notifyRemote=true)
  puts "Disconnecting from #{@conn.addr} to #{@conn.peeraddr}"
  begin
  #self.send(:closeConnection) if notifyRemote
  #@mutex.synchronize {
    #puts "SYNC Disconnecting from #{@conn.addr} to #{@conn.peeraddr}"
    @valid=false
    @server.removeConnection(self)
    @conn.close
  #}
  rescue Object=>e
    glog e
  end
end

#receiveLoopObject

pseudo-private



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/appswarm/secure_con.rb', line 85

def receiveLoop
  return unless @valid
  begin
   loop do
     curdata=@conn.recv(1012)
     @data+=curdata
     if curdata.length>0
       pp "GOT DATA (at #{@conn.addr}) #{@data.length}"
     else
       puts "NO DATA"
       break
     end
     d=Secure::Data.new(@data)
     begin
       result=Secure::Marshal.load(d)
       # important: loop until buffer is empty, because it's possible that there are more than 1 messages in @data
       while result
         @server.receive(self,result)
         @data=d.str
         result=Secure::Marshal.load(d)
       end
     rescue Secure::Marshal::OutOfData=>e
       pp e,@data
     end
     break unless @valid
   end
  rescue Exception=>e
    #glog e,e.backtrace,self
    unless e.inspect.to_s=="#<RuntimeError: stop>"
      pp e,e.backtrace,self
    end
  end
  @server.removeConnection(self)
  @valid=false
end

#remoteHostObject



62
63
64
65
66
67
# File 'lib/appswarm/secure_con.rb', line 62

def remoteHost
  begin
    @conn.peeraddr[3]
  rescue
  end
end

#remotePortObject



56
57
58
59
60
61
# File 'lib/appswarm/secure_con.rb', line 56

def remotePort
  begin
    @conn.peeraddr[1]
  rescue
  end
end

#send(*what) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appswarm/secure_con.rb', line 35

def send(*what)
  return unless @valid
    begin
      data=Secure::Marshal.dump(what)
      puts "Sending data #{data.length} from #{@conn.addr} to #{@conn.peeraddr}"
      @mutex.synchronize {
        if @valid
          len=data.length
          result=@conn.write(data)
          assert {len==result}
          @conn.flush
        else
          raise "Connection invalid"
        end
      }
    rescue Exception=>e
      glog e,e.backtrace
      @valid=false
    end
end