Class: Tem::Transport::JcopRemoteTransport

Inherits:
Object
  • Object
show all
Includes:
JavaCardMixin, JcopRemoteProtocol
Defined in:
lib/tem/transport/jcop_remote_transport.rb

Overview

Implements the transport layer for a JCOP simulator instance.

Instance Method Summary collapse

Methods included from JcopRemoteProtocol

#recv_message, #send_message

Methods included from JavaCardMixin

#applet_apdu, #applet_apdu!, deserialize_response, #install_applet, #select_applet, serialize_apdu

Constructor Details

#initialize(options) ⇒ JcopRemoteTransport

Creates a new unconnected transport for a JCOP simulator serving TCP/IP.

The options parameter must have the following keys:

host:: the DNS name or IP of the host running the JCOP simulator
port:: the TCP/IP port of the JCOP simulator server


16
17
18
19
# File 'lib/tem/transport/jcop_remote_transport.rb', line 16

def initialize(options)
  @host, @port = options[:host], options[:port]
  @socket = nil
end

Instance Method Details

#connectObject

Makes a transport-level connection to the TEM.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tem/transport/jcop_remote_transport.rb', line 28

def connect
  begin
    Socket.getaddrinfo(@host, @port, Socket::AF_INET,
                       Socket::SOCK_STREAM).each do |addr_info|
      begin
        @socket = Socket.new(addr_info[4], addr_info[5], addr_info[6])
        @socket.connect Socket.pack_sockaddr_in(addr_info[1], addr_info[3])
        break
      rescue
        @socket = nil
      end
    end  
    raise 'Connection refused' unless @socket
    
    # Wait for the card to be inserted.

    send_message @socket, :type => 0, :node => 0, :data => [0, 1, 0, 0]
    recv_message @socket  # ATR should come here, but who cares      

  rescue Exception
    @socket = nil
    raise
  end
end

#disconnectObject

Breaks down the transport-level connection to the TEM.



52
53
54
55
56
57
# File 'lib/tem/transport/jcop_remote_transport.rb', line 52

def disconnect
  if @socket
    @socket.close
    @socket = nil
  end
end

#exchange_apdu(apdu) ⇒ Object



22
23
24
25
# File 'lib/tem/transport/jcop_remote_transport.rb', line 22

def exchange_apdu(apdu)
  send_message @socket, :type => 1, :node => 0, :data => apdu
  recv_message(@socket)[:data]
end

#to_sObject



59
60
61
62
# File 'lib/tem/transport/jcop_remote_transport.rb', line 59

def to_s
  "#<JCOP Remote Terminal: disconnected>" if @socket.nil?
  "#<JCOP Remote Terminal: #{@host}:#{@port}>"
end