Class: Smartcard::Iso::JcopRemoteServer

Inherits:
Object
  • Object
show all
Includes:
JcopRemoteProtocol
Defined in:
lib/smartcard/iso/jcop_remote_server.rb

Overview

A server for the JCOP simulator protocol.

The JCOP simulator protocol is generally useful when talking to a real JCOP simulator. This server is only handy for testing, and for forwarding connections (JCOP’s Eclipse plug-in makes the simulator listen to 127.0.0.1, and sometimes you want to use it from another box).

Instance Method Summary collapse

Methods included from JcopRemoteProtocol

#recv_message, #send_message

Constructor Details

#initialize(options, serving_logic = nil) ⇒ JcopRemoteServer

Creates a new JCOP server.

The options hash supports the following keys:

port:: the port to serve on
ip:: the IP of the interface to serve on (defaults to all interfaces)

If the |serving_logic| parameter is nil, a serving logic implementation must be provided when calling JcopRemoteServer#run. The server will crash otherwise.



73
74
75
76
77
78
# File 'lib/smartcard/iso/jcop_remote_server.rb', line 73

def initialize(options, serving_logic = nil)
  @logic = serving_logic
  @running = false
  @options = options
  @mutex = Mutex.new
end

Instance Method Details

#run(serving_logic = nil) ⇒ Object

Runs the serving loop indefinitely.

This method serves incoming conenctions until #stop is called.

If |serving_logic| contains a non-nil value, it overrides any previously specified serving logic implementation. If no implementation is specified when the server is instantiated via JcopRemoteServer#new, one must be passed into |serving_logic|.



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
# File 'lib/smartcard/iso/jcop_remote_server.rb', line 88

def run(serving_logic = nil)
  @mutex.synchronize do
    @logic ||= serving_logic
    @serving_socket = serving_socket @options
    @running = true
  end
  loop do
    break unless @mutex.synchronize { @running }
    begin
      client_socket, client_address = @serving_socket.accept
    rescue
      # An exception will occur if the socket is closed
      break
    end
    @logic.connection_start
    loop do        
      break unless @mutex.synchronize { @running }
      break unless process_request client_socket
    end
    client_socket.close rescue nil
    @logic.connection_end  # implemented by subclass
  end
  @mutex.synchronize do
    @serving_socket.close if @serving_socket
    @serving_socket = nil
  end
end

#stopObject

Stops the serving loop.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/smartcard/iso/jcop_remote_server.rb', line 117

def stop
  @mutex.synchronize do
    if @running
      @serving_socket.close rescue nil
      @serving_socket = nil
      @running = false
    end
  end
  
  # TODO(costan): figure out a way to let serving logic reach this directly.
end