Class: Cod::TcpServer

Inherits:
Object
  • Object
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 Method Summary collapse

Constructor Details

#initialize(bind_to, serializer) ⇒ TcpServer

Returns a new instance of TcpServer.



30
31
32
33
34
35
36
# File 'lib/cod/tcp_server.rb', line 30

def initialize(bind_to, serializer)
  @socket = TCPServer.new(*bind_to.split(':'))
  @client_sockets = []
  @round_robin_index = 0
  @messages = Array.new
  @serializer = serializer
end

Instance Method Details

#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.



102
103
104
# File 'lib/cod/tcp_server.rb', line 102

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

#closeObject

Closes the channel.



75
76
77
78
# File 'lib/cod/tcp_server.rb', line 75

def close
  @socket.close
  @client_sockets.each { |io| io.close }
end

#connectionsObject

Returns the number of clients that are connected to this server currently.



89
90
91
# File 'lib/cod/tcp_server.rb', line 89

def connections
  @client_sockets.size
end

#get(opts = {}) ⇒ Object

Receives one object from the channel. This will receive one message from one of the connected clients in a round-robin fashion.

Examples:

channel.get # => object

Parameters:

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

Returns:

  • (Object)

    message sent by one of the clients



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

def get(opts={})
  msg, socket = _get(opts)
  return msg
end

#get_ext(opts = {}) ⇒ Array<Object, TcpClient>

Receives one object from the channel. Returns a tuple of <message,channel> where channel is a tcp channel that links back to the client that sent message.

Using this method, the server can communicate back to its clients individually instead of collectively.

Examples:

Answering to the client that sent a specific message

msg, chan = server.get_ext
chan.put :answer

Parameters:

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

Returns:

  • (Array<Object, TcpClient>)

    tuple of the message that was sent a channel back to the client that sent the message



66
67
68
69
70
71
# File 'lib/cod/tcp_server.rb', line 66

def get_ext(opts={})
  msg, socket = _get(opts)
  return [
    msg, 
    produce_back_channel(socket)]
end

#request_close(socket) ⇒ void

This method returns an undefined value.

Notifies the TcpServer that one of its connections needs to be closed. This can be triggered by using #get_ext to obtain a handle to connections and then calling #close on that connection.

Parameters:

  • socket (TCPSocket)

    the socket that needs to be closed



115
116
117
118
# File 'lib/cod/tcp_server.rb', line 115

def request_close(socket)
  @client_sockets.delete(socket)
  socket.close
end

#serviceObject

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



95
96
97
# File 'lib/cod/tcp_server.rb', line 95

def service
  Service.new(self)
end

#to_read_fdsObject

Returns an array of IOs that Cod.select should select on.



82
83
84
# File 'lib/cod/tcp_server.rb', line 82

def to_read_fds
  @client_sockets
end