Class: Rapid::Services::NetworkService

Inherits:
Service
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ury_rapid/services/network_service.rb

Overview

A base class for Rapid services that connect to a network server

This class provides an opinionated framework for TCP-based services, that separates the service-specific workload into three separate objects:

  • A requester, which hooks into the service model and translates model changes into raw request strings to send to the downstream server;

  • A responder, which receives parsed responses from the downstream server and updates the model to reflect them;

  • A client, which is given a requests queue and access to the responder, and maintains the connection as well as the machinery needed to read and parse responses.

See the Service class for information about methods that must be implemented by subclasses of NetworkService. In addition, subclasses must implement:

  • #make_requester, which returns a requester given the requests queue;

  • #make_responder, which returns a responder given the requester;

  • #make_client, which returns a client given the requests queue, host and port;

  • #default_host, which returns a tuple of default host and port.

They may also override:

  • #initial_requests, which is given the requester and can be used to make any initial requests necessary to start the session with the network server upon the service start.

Direct Known Subclasses

Baps::Service

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ NetworkService

Initialises the service

Examples:

Create a new network service

service = NetworkService.new(environment)

42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ury_rapid/services/network_service.rb', line 42

def initialize(*_)
  super

  # We need a queue to hold requests to the network server.  This will
  # later need to be given to the client for reading, and also to the
  # requester for writing.
  @queue = EventMachine::Queue.new

  # The requester contains all the logic for instructing the network
  # server to make model changes happen.
  @requester = make_requester(@queue)

  host(*default_host)
end

Instance Method Details

#host(host, port) ⇒ void

This method returns an undefined value.

Sets the host and port of this network service's downstream server

Examples:

Set the service to connect to localhost, port 1350

# (in config.rb)
host 'localhost', 1350

Parameters:

  • host (String)

    The name or address of the downstream host.

  • port (Integer)

    The network port of the downstream host.


74
75
76
77
# File 'lib/ury_rapid/services/network_service.rb', line 74

def host(host, port)
  @host = host
  @port = port
end

#runvoid

This method returns an undefined value.

Begin running the service

This function is always run within an EventMachine run block.

Examples:

Run the network service.

ns.run

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ury_rapid/services/network_service.rb', line 95

def run
  # Most of the actual network server poking is contained inside this
  # client object, which is what hooks into the Rapid EventMachine
  # instance.  We need to give it access to parts of the service config
  # so it knows where and how to connect to the server.
  client = make_client(@queue, @host, @port)

  # The responder receives responses from downstream via the client and
  # reacts on them, either updating the model or asking the requester to
  # intervene.
  responder = make_responder(@requester)

  # Now we can run the client, passing it the responder so it can send
  # BAPS responses to it.  The client will get BAPS requests sent to it
  # via the queue, thus completing the communication paths.
  client.run(responder)

  # Finally, the network service may wish to send some initial requests
  # to the downstream server here.
  initial_requests(@requester)
end