Class Smartcard::Iso::JcopRemoteServer
In: lib/smartcard/iso/jcop_remote_server.rb
Parent: Object

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

Methods

new   run   stop  

Included Modules

JcopRemoteProtocol

Public Class methods

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)
  reusable:: if set, the serving port can be shared with another
             application (REUSEADDR flag will be set on the socket)

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

[Source]

    # File lib/smartcard/iso/jcop_remote_server.rb, line 75
75:   def initialize(options, serving_logic = nil)
76:     @logic = serving_logic
77:     @running = false
78:     @options = options
79:     @mutex = Mutex.new
80:   end

Public Instance methods

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

[Source]

     # File lib/smartcard/iso/jcop_remote_server.rb, line 90
 90:   def run(serving_logic = nil)
 91:     @mutex.synchronize do
 92:       @logic ||= serving_logic
 93:       @serving_socket = serving_socket @options
 94:       @running = true
 95:     end
 96:     loop do
 97:       break unless @mutex.synchronize { @running }
 98:       begin
 99:         client_socket, client_address = @serving_socket.accept
100:       rescue
101:         # An exception will occur if the socket is closed
102:         break
103:       end
104:       @logic.connection_start
105:       loop do        
106:         break unless @mutex.synchronize { @running }
107:         break unless process_request client_socket
108:       end
109:       client_socket.close rescue nil
110:       @logic.connection_end  # implemented by subclass
111:     end
112:     @mutex.synchronize do
113:       @serving_socket.close if @serving_socket
114:       @serving_socket = nil
115:     end
116:   end

Stops the serving loop.

[Source]

     # File lib/smartcard/iso/jcop_remote_server.rb, line 119
119:   def stop
120:     @mutex.synchronize do
121:       if @running
122:         @serving_socket.close rescue nil
123:         @serving_socket = nil
124:         @running = false
125:       end
126:     end
127:     
128:     # TODO(costan): figure out a way to let serving logic reach this directly.
129:   end

[Validate]