Class: Cod::TcpServer

Inherits:
SocketServer show all
Defined in:
lib/cod/tcp_server.rb

Overview

A tcp server channel. Messages are read from any of the connected sockets in a round robin fashion.

Synopsis

server = Cod.tcp_server('localhost:12345') 
server.get  # 'a message'
msg, chan = server.get_ext

There is no implementation of #put that would broadcast back to all connected sockets, this is up to you to implement. Instead, you can use one of two ways to obtain a channel for talking back to a specific client:

msg, chan = server.get_ext

chan is a two way connected channel to the specific client that has opened its communication with msg.

# on the client: 
client.put [client, :msg]
# on the server
chan, msg = server.get

This means that you can transmit the client channel through the connection as part of the message you send.

Instance Attribute Summary

Attributes inherited from SocketServer

#serializer, #socket

Instance Method Summary collapse

Methods inherited from SocketServer

#close, #connections, #get, #get_ext, #request_close, #to_read_fds

Methods included from Callbacks

#callbacks_enabled?, #register_callback, #using_callbacks

Constructor Details

#initialize(bind_to, serializer) ⇒ TcpServer

Returns a new instance of TcpServer.



30
31
32
# File 'lib/cod/tcp_server.rb', line 30

def initialize(bind_to, serializer)
  super(serializer, TCPServer.new(*bind_to.split(':')))
end

Instance Method Details

#back_channel(socket) ⇒ Object



41
42
43
44
45
# File 'lib/cod/tcp_server.rb', line 41

def back_channel(socket)
  TcpClient.new(
    TcpClient::Connection.new(socket, self), 
    serializer)    
end

#client(answers_to) ⇒ Object

Note:

It is really more convenient to just construct a Cod.tcp_client

and ask that for a client object. In the case of TCP, this is enough.



56
57
58
# File 'lib/cod/tcp_server.rb', line 56

def client(answers_to)
  Service::Client.new(answers_to, answers_to)
end

#deserialize_special(socket, obj) ⇒ Object



34
35
36
37
38
39
# File 'lib/cod/tcp_server.rb', line 34

def deserialize_special(socket, obj)
  if obj.kind_of?(TcpClient::OtherEnd)
    return back_channel(socket)
  end
  return obj
end

#serviceObject

——————————————————— service/client



49
50
51
# File 'lib/cod/tcp_server.rb', line 49

def service
  Service.new(self)
end