Class: RTunnel::ControlServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/server.rb

Constant Summary collapse

@@control_connections =
[]
@@remote_listen_servers =
[]
@@m =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ControlServer

Returns a new instance of ControlServer.



65
66
67
68
# File 'lib/server.rb', line 65

def initialize(*args)
  super
  @maxConnections = 1024
end

Instance Attribute Details

#ping_intervalObject

Returns the value of attribute ping_interval.



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

def ping_interval
  @ping_interval
end

Class Method Details

.close_tunnel(conn_id) ⇒ Object



81
82
83
84
85
# File 'lib/server.rb', line 81

def close_tunnel(conn_id)
  D "sending close connection command"

  @@m.synchronize { control_connection_for(conn_id).write CloseConnectionCommand.new(conn_id) }
end

.new_tunnel(conn_id) ⇒ Object



71
72
73
74
75
# File 'lib/server.rb', line 71

def new_tunnel(conn_id)
  D "sending create connection command: #{conn_id}"

  @@m.synchronize { control_connection_for(conn_id).write CreateConnectionCommand.new(conn_id) }
end

.send_data(conn_id, data) ⇒ Object



77
78
79
# File 'lib/server.rb', line 77

def send_data(conn_id, data)
  @@m.synchronize { control_connection_for(conn_id).write SendDataCommand.new(conn_id, data) }
end

Instance Method Details

#serve(sock) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/server.rb', line 98

def serve(sock)
  D "new control connection"
  @@control_connections << sock
  sock.sync = true

  cmd_queue = ""
  sock.while_reading(cmd_queue = '') do
    while Command.match(cmd_queue)
      case cmd = Command.parse(cmd_queue)
      when RemoteListenCommand
        @@m.synchronize do
          addr, port = cmd.address.split(/:/)
          if rls = @@remote_listen_servers.detect {|s| s.port == port.to_i }
            rls.stop
            @@remote_listen_servers.delete rls
          end
          (new_rls = RemoteListenServer.new(port, addr, sock)).start
          @@remote_listen_servers << new_rls
        end
        D "listening for remote connections on #{cmd.address}"
      when SendDataCommand
        conn = RemoteListenServer::CONNECTIONS[cmd.conn_id]
        begin
          conn.write(cmd.data)  if conn
        rescue Errno::EPIPE
          D "broken pipe on #{cmd.conn_id}"
        end
      when CloseConnectionCommand
        if connection = RemoteListenServer::CONNECTIONS[cmd.conn_id]
          D "closing remote connection: #{cmd.conn_id}"
          connection.close
        end
      else
        D "bad command received: #{cmd.inspect}"
      end
    end
  end
rescue Errno::ECONNRESET
  D "client disconnected (conn reset)"
rescue
  D $!.inspect
  D $@*"\n"
  raise
ensure
  @@control_connections.delete sock
end

#startingObject



94
95
96
# File 'lib/server.rb', line 94

def starting
  start_pinging
end

#stoppingObject



145
146
147
148
# File 'lib/server.rb', line 145

def stopping
  @ping_thread.kill
  @@remote_listen_server.stop
end