Class: RCS::Backdoor::Transport

Inherits:
Object
  • Object
show all
Includes:
Tracer
Defined in:
lib/rcs-backdoor/transport.rb

Constant Summary collapse

OPEN_TIMEOUT =
600
READ_TIMEOUT =
600

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ Transport

Returns a new instance of Transport.



21
22
23
24
25
# File 'lib/rcs-backdoor/transport.rb', line 21

def initialize(param)
  trace :debug, "Protocol initialized #{param}"
  @host_param = param
  init_host(param)
end

Instance Method Details

#connect_to(host) ⇒ Object

connection to the remote host for the REST protocol (HTTP) we don’t have a persistent connection to the sync server, just instantiate the objects here and make an HTTP request every message



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rcs-backdoor/transport.rb', line 44

def connect_to(host)
  Net::HTTP.version_1_2
  init_host(@host_param)
  @host << host << "/service"
  
  trace_named_put(:host, @host)
  
  @uri = URI.parse(@host)
  trace :info, "Connecting to: " << @host
  @cookie = nil
      
  # the HTTP connection (better to instantiate it here, only once)
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = @ssl
  @http.open_timeout = OPEN_TIMEOUT
  @http.read_timeout = READ_TIMEOUT
  #@http.set_debug_output $stderr
  # start the HTTP connection (needed for keep-alive option)
  # without this, the connection will be closed after the first request
  # see this: http://redmine.ruby-lang.org/issues/4522
  @http.start
end

#disconnectObject

nothing to do here for HTTP connections



103
104
105
106
107
108
# File 'lib/rcs-backdoor/transport.rb', line 103

def disconnect
  @cookie = nil
  trace_named_remove(:cookie)
  trace_named_remove(:host)
  trace :info, "End point closed: " << @host
end

#init_host(param) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rcs-backdoor/transport.rb', line 27

def init_host(param)
  case param
    when :HTTP
      @host = "http://"
      @ssl = false
    when :HTTPS
      @host = "https://"
      @ssl = true
    else
      raise "Unsupported Transport"
  end
end

#message(msg) ⇒ Object

every message is an HTTP POST request. the protocol is always write and read.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rcs-backdoor/transport.rb', line 69

def message(msg)
  
  # the REST protocol is always a POST
  request = Net::HTTP::Post.new(@uri.request_uri)
  
  # the message body
  request.body = msg
  request['Content-Type'] = "application/octet-stream"
  
  # set the cookie if we already have it (got from the Auth phase)
  request['Cookie'] = @cookie unless @cookie.nil?

  # keep the connection open for faster communication
  request['Connection'] = 'Keep-Alive'

  #request['X-Forwarded-For'] = '1.2.3.4'

  res = nil

  # fire !
  Timeout::timeout(READ_TIMEOUT) do
    res = @http.request(request)
  end
  
  #trace :debug, "Cookie: " << res['Set-Cookie'] unless res['Set-Cookie'].nil?
  
  # save the cookie for later use
  @cookie = res['Set-Cookie'] unless res['Set-Cookie'].nil?
  trace_named_put(:cookie, @cookie)
  
  return res.body
end