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

- (JcopRemoteServer) initialize(options, serving_logic = nil)

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.



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

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

Instance Method Details

- (Object) run(serving_logic = nil)

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|.



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

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

- (Object) stop

Stops the serving loop.



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

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