Class: Tem::MultiProxy::Server

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

Overview

RPC server for a tem_multi_proxy server.

The server’s RPC interface provides administrative information about the cards connected to it. To communicate with the smart-cards, use the transports in the smartcard gem (see the Smartcard::Iso namespace).

Constant Summary collapse

DEFAULT_PORT =
9051
Protocol =
Zerg::Support::Protocols::ObjectProtocol
Adapter =
Zerg::Support::Sockets::ProtocolAdapter.adapter_module Protocol
SocketFactory =
Zerg::Support::SocketFactory

Instance Method Summary collapse

Constructor Details

#initialize(server_port = nil) ⇒ Server

Returns a new instance of Server.



27
28
29
30
31
# File 'lib/tem_multi_proxy/server.rb', line 27

def initialize(server_port = nil)
  @manager = Manager.new
  @socket = SocketFactory.socket :in_port => (server_port || DEFAULT_PORT),
                                 :no_delay => true    
end

Instance Method Details

#serve_client(client) ⇒ Object

Handle a single client.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tem_multi_proxy/server.rb', line 51

def serve_client(client)
  client.extend Adapter
  request = client.recv_object
  if request
    case request[:query]
    when 'tem_ports'
      client.send_object @manager.tem_ports
    else
      client.send_object 'Unknown request'
    end
  end
  client.close rescue nil
end

#serving_loopObject

The main server loop.



34
35
36
37
# File 'lib/tem_multi_proxy/server.rb', line 34

def serving_loop
  Thread.new { socket_loop }
  @manager.management_loop
end

#socket_loopObject

Socket-serving loop that takes in clients and spins off a thread for each client.



41
42
43
44
45
46
47
48
# File 'lib/tem_multi_proxy/server.rb', line 41

def socket_loop
  @socket.listen
  loop do
    client_socket, client_addr = @socket.accept
    Thread.new(client_socket) { |client_socket| serve_client client_socket }      
  end
  @manager.tem_ports
end