Module Smartcard::Iso::JcopRemoteProtocol
In: lib/smartcard/iso/jcop_remote_protocol.rb

Mixin implementing the JCOP simulator protocol.

The (pretty informal) protocol specification is contained in the JavaDocs for the class com.ibm.jc.terminal.RemoteJCTerminal and should be easy to find by www.google.com/search?q=%22com.ibm.jc.terminal.RemoteJCTerminal%22

Methods

Public Instance methods

Reads and decodes a JCOP simulator message from a TCP socket.

:call_seq:

  client.read_message(socket) -> Hash or nil

If the other side of the TCP socket closes the connection, this method returns nil. Otherwise, a Hash is returned, with the format required by the JcopRemoteProtocol#send_message.

[Source]

    # File lib/smartcard/iso/jcop_remote_protocol.rb, line 37
37:   def recv_message(socket)
38:     header = ''
39:     while header.length < 4
40:       begin
41:         partial = socket.recv 4 - header.length
42:       rescue  # Abrupt hangups result in exceptions that we catch here.        
43:         return nil
44:       end
45:       return false if partial.length == 0
46:       header += partial
47:     end
48:     message_type, node_address, data_length = *header.unpack('CCn')
49:     raw_data = ''
50:     while raw_data.length < data_length
51:       begin
52:         partial = socket.recv data_length - raw_data.length
53:       rescue  # Abrupt hangups result in exceptions that we catch here.
54:         return nil
55:       end
56:       return false if partial.length == 0
57:       raw_data += partial
58:     end
59:     
60:     return false unless raw_data.length == data_length
61:     data = raw_data.unpack('C*')
62:     return { :type => message_type, :node => node_address, :data => data }
63:   end

Encodes and sends a JCOP simulator message to a TCP socket.

The message must contain the following keys:

  type:: Integer expressing the message type (e.g. 1 = APDU exchange)
  node:: Integer expressing the node address (e.g. 0 for most purposes)
  data:: message payload, as an array of Integers ranging from 0 to 255

[Source]

    # File lib/smartcard/iso/jcop_remote_protocol.rb, line 23
23:   def send_message(socket, message)
24:     raw_message = [message[:type], message[:node], message[:data].length].
25:                   pack('CCn') + message[:data].pack('C*')
26:     socket.send raw_message, 0
27:   end

[Validate]