Class: Anorexic::NoService

Inherits:
Object
  • Object
show all
Defined in:
lib/anorexic/server/services/no_service.rb

Overview

this class is a basic TCP socket service.

a protocol should be assigned, or the service will fall back to an echo service.

a protocol should answer to: on_connect(service), on_message(service, data), on_disconnect(service) and on_exception(service, exception)

the on_message method should return any data that wasn't used (to be sent again as part of the next `on_message` call, once more data is received).

if the protocol is a class, these methods should be instance methods. a protocol class should support the initialize(service, parameters={}) method as well.

to-do: fix logging

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, parameters = {}) ⇒ NoService

creates a new connection wrapper object for the new socket that was recieved from the `accept_nonblock` method call.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/anorexic/server/services/no_service.rb', line 34

def initialize socket, parameters = {}
  @handler = parameters[:handler]
  @socket = nil
  # socket.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, "\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" #== [10 sec 0 usec].pack '1_2'
  @out_que = []
  @locker = Mutex.new
  @parameters = parameters
  @protocol = parameters[:protocol]
  @protocol = protocol.new self, parameters if protocol.is_a?(Class)
  @protocol.on_connect self if @protocol && @protocol.methods.include?(:on_connect)
  touch
  @timeout ||= 5
  # Anorexic.callback self, :on_message
end

Instance Attribute Details

#active_timeObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def active_time
  @active_time
end

#closedObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def closed
  @closed
end

#handlerObject

Returns the value of attribute handler


31
32
33
# File 'lib/anorexic/server/services/no_service.rb', line 31

def handler
  @handler
end

#lockerObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def locker
  @locker
end

#out_queObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def out_que
  @out_que
end

#parametersObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def parameters
  @parameters
end

#protocolObject

Returns the value of attribute protocol


31
32
33
# File 'lib/anorexic/server/services/no_service.rb', line 31

def protocol
  @protocol
end

#socketObject (readonly)

instance methods


30
31
32
# File 'lib/anorexic/server/services/no_service.rb', line 30

def socket
  @socket
end

#timeoutObject

Returns the value of attribute timeout


31
32
33
# File 'lib/anorexic/server/services/no_service.rb', line 31

def timeout
  @timeout
end

Class Method Details

.acceptObject


24
25
26
# File 'lib/anorexic/server/services/no_service.rb', line 24

def self.accept
  false
end

.create_service(port, parameters) ⇒ Object

create a listener (io) - will create a TCPServer socket

listeners are 'server sockets' that answer to `accept` by creating a new connection socket (io).


20
21
22
# File 'lib/anorexic/server/services/no_service.rb', line 20

def self.create_service port, parameters
  self
end

Instance Method Details

#<<(data) ⇒ Object

adds data to the out buffer - but doesn't send the data until a send event is called.


89
90
91
92
# File 'lib/anorexic/server/services/no_service.rb', line 89

def << data
  touch
  locker.synchronize {@out_que << data}
end

#closeObject

closes the connection


129
130
# File 'lib/anorexic/server/services/no_service.rb', line 129

def close
end

#disconnectObject

disconects the service.


136
137
138
# File 'lib/anorexic/server/services/no_service.rb', line 136

def disconnect
  on_disconnect
end

#disconnected?Boolean

returns true if the service is disconnected

Returns:

  • (Boolean)

132
133
134
# File 'lib/anorexic/server/services/no_service.rb', line 132

def disconnected?
  false
end

#flushObject

makes sure any data in the que is send and calls `flush` on the socket, to make sure the buffer is sent.


95
96
# File 'lib/anorexic/server/services/no_service.rb', line 95

def flush
end

#has_incoming_data?Boolean

returns true if the socket has content to be read.

Returns:

  • (Boolean)

140
141
142
# File 'lib/anorexic/server/services/no_service.rb', line 140

def has_incoming_data?
   false
end

#ioObject

returns an IO-like object used for reading/writing (unlike the original IO object, this can be an SSL layer or any other wrapper object).


65
66
67
68
# File 'lib/anorexic/server/services/no_service.rb', line 65

def io
  touch
  @socket
end

#on_disconnectObject

called once a socket is disconnected or needs to be disconnected.


120
121
122
123
124
# File 'lib/anorexic/server/services/no_service.rb', line 120

def on_disconnect
  Anorexic.callback Anorexic, :remove_connection, self

  close
end

#on_messageObject

notice: since it is all async evet base - multipart messages might be garbled up…? todo: protect from garbeling.


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/anorexic/server/services/no_service.rb', line 102

def on_message
  # return false if locker.locked?
  return false if locker.locked?
  return disconnect if (_disconnected? rescue true)
  locker.synchronize do
    begin
      touch
      if protocol
        protocol.on_message self
      end
    rescue Exception => e
      Anorexic.error e
      return disconnect
    end
  end
end

#read(size = 1048576) ⇒ Object

this is a public method and it should be used by child classes to implement each read(_nonblock) action. accepts one argument ::size for an optional buffer size to be read.


161
162
163
# File 'lib/anorexic/server/services/no_service.rb', line 161

def read size = 1048576
  ''
end

#send(data = nil) ⇒ Object

sends data immidiately - forcing the data to be sent, flushing any pending messages in the que


71
72
73
# File 'lib/anorexic/server/services/no_service.rb', line 71

def send data = nil
  touch
end

#send_nonblock(data) ⇒ Object

sends data without waiting - data might be sent in a different order then intended.


82
83
84
85
86
# File 'lib/anorexic/server/services/no_service.rb', line 82

def send_nonblock data
  touch
  locker.synchronize {@out_que << data}
  Anorexic.callback(self, :send)
end

#send_unsafe_interrupt(data = nil) ⇒ Object

sends data immidiately, interrupting any pending que and ignoring thread safety.


76
77
78
79
# File 'lib/anorexic/server/services/no_service.rb', line 76

def send_unsafe_interrupt data = nil
  touch
  _send data rescue disconnect
end

#service_typeObject

returns the service type - set to normal


148
149
150
# File 'lib/anorexic/server/services/no_service.rb', line 148

def service_type
  'no-service'
end

#ssl?Boolean

returns true if the service is encrypted using the OpenSSL library.

Returns:

  • (Boolean)

152
153
154
# File 'lib/anorexic/server/services/no_service.rb', line 152

def ssl?
  false
end

#timedout?Boolean

checks if a connection timed out

Returns:

  • (Boolean)

55
56
57
# File 'lib/anorexic/server/services/no_service.rb', line 55

def timedout?
  Time.now - @active_time > @timeout
end

#touchObject

resets the timer for the connection timeout


60
61
62
# File 'lib/anorexic/server/services/no_service.rb', line 60

def touch
  @active_time = Time.now
end