Class: Cod::SocketServer

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/cod/socket_server.rb

Overview

Abstract base class for all kinds of socket based servers. Useful for all types of channels that know an #accept.

Direct Known Subclasses

BidirServer, TcpServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#callbacks_enabled?, #register_callback, #using_callbacks

Constructor Details

#initialize(serializer, socket) ⇒ SocketServer

Returns a new instance of SocketServer.



14
15
16
17
18
19
20
# File 'lib/cod/socket_server.rb', line 14

def initialize(serializer, socket)
  @socket = socket
  @client_sockets = []
  @round_robin_index = 0
  @messages = Array.new
  @serializer = serializer
end

Instance Attribute Details

#serializerObject (readonly)

Returns the value of attribute serializer.



12
13
14
# File 'lib/cod/socket_server.rb', line 12

def serializer
  @serializer
end

#socketObject (readonly)

Returns the value of attribute socket.



11
12
13
# File 'lib/cod/socket_server.rb', line 11

def socket
  @socket
end

Instance Method Details

#closeObject

Closes the channel.



59
60
61
62
# File 'lib/cod/socket_server.rb', line 59

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

#connectionsObject

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



73
74
75
# File 'lib/cod/socket_server.rb', line 73

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



31
32
33
34
# File 'lib/cod/socket_server.rb', line 31

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



50
51
52
53
54
55
# File 'lib/cod/socket_server.rb', line 50

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

#request_close(socket) ⇒ Object

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.

void

Parameters:

  • socket (TCPSocket)

    the socket that needs to be closed @return



86
87
88
89
# File 'lib/cod/socket_server.rb', line 86

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

#to_read_fdsObject

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



66
67
68
# File 'lib/cod/socket_server.rb', line 66

def to_read_fds
  @client_sockets + [@socket]
end