Class: Pione::Relay::TransmitterSocket

Inherits:
DRb::DRbTCPSocket
  • Object
show all
Defined in:
lib/pione/relay/transmitter-socket.rb

Overview

TransmitterSocket is a connection layer from PIONE relay to PIONE client.

Defined Under Namespace

Classes: TransmitterSocketError

Constant Summary collapse

@@receiver_socket =
{}
@@proxy_socket =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, receiver_socket, proxy_socket, config) ⇒ TransmitterSocket

Returns a new instance of TransmitterSocket.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pione/relay/transmitter-socket.rb', line 84

def initialize(uri, receiver_socket, proxy_socket, config)
  @uri = uri
  @receiver_socket = receiver_socket
  @proxy_socket = proxy_socket
  @config = config
  @acl = config[:tcp_acl]
  @receiver_msg = DRb::DRbMessage.new(config)
  @proxy_msg = DRb::DRbMessage.new(config)
  set_sockopt(@receiver_socket) if @receiver_socket
  set_sockopt(@proxy_socket) if @proxy_socket
end

Class Method Details

.open(uri, config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a fake connection from proxy to relay transmitter.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pione/relay/transmitter-socket.rb', line 32

def self.open(uri, config)
  # reverse socket needs URI always
  raise ArgumentError.new("You should specify transmitter URI.") unless uri

  # get transmitter_id
  transmitter_id, _ = parse_uri(uri)
  transmitter_id.untaint

  # get proxy side socket
  soc = @@proxy_socket[transmitter_id]
  unless soc
    msg = "No receiver side socket for %s." % transmitter_id
    raise TransmitterSocketError.new(msg)
  end
  proxy_socket = TCPSocket.new("localhost", soc.addr[1])

  # create instance with proxy socket
  self.new(uri, nil, proxy_socket, config)
end

.open_server(uri, config) ⇒ Object

Opens a fake socket.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pione/relay/transmitter-socket.rb', line 53

def self.open_server(uri, config)
  # the method needs URI explicitly
  raise ArgumentError.new("You should specify transmitter URI.") unless uri

  # get transmitter_id
  transmitter_id, _ = parse_uri(uri)
  transmitter_id.untaint

  # get receiver side socket
  receiver_socket = @@receiver_socket[transmitter_id]
  unless receiver_socket
    msg = "No receiver side sockets for %s." % transmitter_id
    raise TransmitterSocketError.new(msg)
  end

  # get proxy side socket
  proxy_socket = @@proxy_socket[transmitter_id]
  unless proxy_socket
    msg = "No proxy side sockets for %s." % transmitter_id
    raise TransmitterSocketError.new(msg)
  end

  # create instance with receiver and proxy sockets
  self.new(uri, receiver_socket, proxy_socket, config)
end

.parse_uri(uri) ⇒ Object

Parses special URI for reverse scheme.



19
20
21
22
23
24
25
26
27
28
# File 'lib/pione/relay/transmitter-socket.rb', line 19

def self.parse_uri(uri)
  if uri =~ /^transmitter:\/\/([\w-]+)(\?(.*))?$/
    transmitter_id = $1
    option = $3
    return transmitter_id, option
  else
    raise DRb::DRbBadScheme.new(uri) unless uri =~ /^transmitter:/
    raise DRb::DRbBadURI.new('can\'t parse uri:' + uri)
  end
end

.proxy_socketObject



14
15
16
# File 'lib/pione/relay/transmitter-socket.rb', line 14

def self.proxy_socket
  @@proxy_socket
end

.receiver_socketObject



10
11
12
# File 'lib/pione/relay/transmitter-socket.rb', line 10

def self.receiver_socket
  @@receiver_socket
end

.uri_option(uri, config) ⇒ Object



79
80
81
82
# File 'lib/pione/relay/transmitter-socket.rb', line 79

def self.uri_option(uri, config)
  transmitter_id, option = parse_uri(uri)
  return "transmitter://%s" % transmitter_id, option
end

Instance Method Details

#acceptObject

Opens fake connection from proxy to transmitter. Just returns self.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/pione/relay/transmitter-socket.rb', line 140

def accept
  while true
    soc = @proxy_socket.accept
    break if (@acl ? @acl.allow_socket?(soc) : true)
    soc.close
  end

  if Global.show_communication
    puts "proxy connects to transmitter"
  end

  # create instance
  self.class.new(uri, nil, soc, @config)
rescue => e
  soc.close
  if Global.show_communication
    puts "closed transmitter's proxy side socket"
    puts "%s: %s" % [e.class, e.message]
    caller.each {|line| puts "    %s" % line}
  end
  retry
end

#closeObject

Closes proxy side socket.



133
134
135
136
# File 'lib/pione/relay/transmitter-socket.rb', line 133

def close
  @proxy_socket.close
  @proxy_socket = nil
end

#recv_replyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Receives a reply from receiver to transmitter.



124
125
126
127
128
129
130
# File 'lib/pione/relay/transmitter-socket.rb', line 124

def recv_reply
  if @receiver_socket
    @receiver_msg.recv_reply(@receiver_socket)
  else
    @proxy_msg.recv_reply(@proxy_socket)
  end
end

#recv_requestObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Receives a request from proxy to transmitter.



112
113
114
# File 'lib/pione/relay/transmitter-socket.rb', line 112

def recv_request
  @proxy_msg.recv_request(@proxy_socket)
end

#send_reply(req_id, succ, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a reply from transmitter to proxy.



118
119
120
# File 'lib/pione/relay/transmitter-socket.rb', line 118

def send_reply(req_id, succ, result)
  @proxy_msg.send_reply(req_id, @proxy_socket, succ, result)
end

#send_request(ref, msg_id, arg, b) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a request from transmitter to receiver.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pione/relay/transmitter-socket.rb', line 98

def send_request(ref, msg_id, arg, b)
  if @receiver_socket
    ref = DRbObject.new_with_uri("receiver://%s:%s" % ["localhost", Global.relay_port])
    #p ref.__drburi if ref.kind_of?(DRbObject)
    #p msg_id
    #p arg
    @receiver_msg.send_request(@receiver_socket, ref, msg_id, arg, b)
  else
    @proxy_msg.send_request(@proxy_socket, ref, msg_id, arg, b)
  end
end