Module: MarilynRPC::Server

Defined in:
lib/marilyn-rpc/server.rb

Overview

The server will be used to make incomming connections possible. The server handles the low level networking functions, so that the services don’t have to deal with them. Because of the way eventmachine works you can have as many servers as you want.

Examples:

a server which is available througth 3 connections:

EM.run {
  EM.start_server "localhost", 8000, MarilynRPC::Server
  EM.start_server "localhost", 8008, MarilynRPC::Server, :secure => true
  EM.start_unix_domain_server("tmp.socket", MarilynRPC::Server)
}

Instance Method Summary collapse

Instance Method Details

#initialize(options = {}) ⇒ Object

initalize the server with connection options

Parameters:

  • options (Hash) (defaults to: {})

    the options passed to the server

Options Hash (options):

  • :secure (Boolean)

    enable secure transfer for the server possible values ‘true` or `false`



18
19
20
# File 'lib/marilyn-rpc/server.rb', line 18

def initialize(options = {})
  @secure = options[:secure]
end

#post_initObject

Initialize the first recieving envelope for the connection and create the service cache since each connection gets it’s own service instance.



24
25
26
27
28
# File 'lib/marilyn-rpc/server.rb', line 24

def post_init
  @envelope = MarilynRPC::Envelope.new
  @cache = MarilynRPC::ServiceCache.new
  start_tls if @secure
end

#receive_data(data) ⇒ Object

Handler for the incoming data. EventMachine compatible.

Parameters:

  • data (String)

    the data that should be parsed into envelopes



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/marilyn-rpc/server.rb', line 32

def receive_data(data)
  overhang = @envelope.parse!(data)
  
  # was massage parsed successfully?
  if @envelope.finished?
    # grep the request
    answer = @cache.call(@envelope)
    if answer.is_a? String
      send_data(answer)
    else
      answer.connection = self # pass connection for async responses
    end
    
    # initialize the next envelope
    @envelope.reset!
    receive_data(overhang) if overhang # reenter the data loop
  end
end

#unbindObject

Handler for client disconnect



52
53
54
# File 'lib/marilyn-rpc/server.rb', line 52

def unbind
  @cache.disconnect!
end