Class: LanGrove::Adaptor::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/langrove/adaptor_base.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/langrove/adaptor_base.rb', line 9

def initialize( config, logger )

  @config = config
  @logger = logger
  
  @transport = :tcp
  @iface     = '127.0.0.1'
  @port      = 12701
  
  @iface     = @config[ :iface ] if @config.has_key? :iface
  @port      = @config[ :port ]  if @config.has_key? :port
  @transport = @config[ :transport ].to_sym  if @config.has_key? :transport
  
  #
  # Assign the call signature for the EventMachine server
  #
  @em_server_call = case @transport
    
    when :tcp
      
      :start_server
      
    when :udp
      
      :open_datagram_socket
      
    else
      
      :start_server
    
  end

end

Instance Method Details

#listen(client, protocol, handler) ⇒ Object



43
44
45
46
47
48
49
50
51
52
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/langrove/adaptor_base.rb', line 43

def listen( client, protocol, handler )
  
  #
  # <client> = {
  # 
  #    :class   => <loaded class constant>
  #    :config  => <client's branch of config>
  # 
  # } 
  # 
  # <protocol> = {  ...the same, but protocolic }
  #
  #

  @logger.info "starting listen at UDP #{@iface}:#{@port}"
  
  EventMachine::send( @em_server_call, @iface, @port, 
  
    client[:class] ) do | client_connected |
      
      #
      # @em_server_call as:
      # 
      # udp - EM::open_datagram_socket, yields an instance of 
      #       client[:class] into here on bind to the port 
      #
      #       ie. at starting to listen
      #       
      #       unverified: ? If more than 1 datagram source is 
      #                   transmitting to here - will there still
      #                   still be only ONE EM:Connection instance?
      # 
      # tcp - EM::start_server yields an new instance of client[:class]
      #       with every connecting socket pair.
      #
      #       ie. On socket pair binding - so each remote client will 
      #           have it's own associated instance of a client[:class] 
      #           running in the machine.
      #
      
      
      #
      # Initialize the client with the (application layer) Protocol
      #
      
      client_connected.logger = @logger
      
      client_connected.config = client[ :config ]
      
      client_connected.protocol = protocol[ :class ]\
      
        .new( protocol[ :config ], @logger )
        
      @logger.info( "TODO: make a reference to the Client on the Handler" )
 
      #
      # Bi-Directionally bind the Client and Handler
      #
      handler.connect( client_connected )
      client_connected.handler = handler
      
      client_connected.start_client
      
      #
      # Client is ready and running inside the reactor
      #
      # EM::start_server will yield again on the next connect.
      # 
      
  end

end