Class: GReactor::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/greactor/protocol.rb

Overview

This is a basic handler class for the reactor.

By inheriting this class and setting a handler that is of type Class, you will get more functionality.

To use the Protocol class, inherit the class and override any of the following methods:

#on_connect

an initialization callback that initializes the new protocol object that will be created for every connection.

#on_message data

called whenever data is recieved from the IO.

#on_disconnect

called AFTER the IO is closed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Protocol

initializes the protocol object


17
18
19
20
21
# File 'lib/greactor/protocol.rb', line 17

def initialize io
  @io = io 
  @params = io.params
  on_connect
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io


14
15
16
# File 'lib/greactor/protocol.rb', line 14

def io
  @io
end

#lockerObject (readonly)

Returns the value of attribute locker


14
15
16
# File 'lib/greactor/protocol.rb', line 14

def locker
  @locker
end

#paramsObject (readonly)

Returns the value of attribute params


14
15
16
# File 'lib/greactor/protocol.rb', line 14

def params
  @params
end

Instance Method Details

#[](key) ⇒ Object

Reads data from the IO wrapper's cache (a cache attached to the IO object).


65
66
67
# File 'lib/greactor/protocol.rb', line 65

def [] key
  @io[key]
end

#[]=(key, val) ⇒ Object

Writes data to the IO wrapper's cache (a cache attached to the IO object).


69
70
71
# File 'lib/greactor/protocol.rb', line 69

def []= key, val
  @io[key] = val
end

#cacheObject

Allows access to the IO wrapper's cache (a cache attached to the IO object).


73
74
75
# File 'lib/greactor/protocol.rb', line 73

def cache
  @io.cache
end

#call(c_io) ⇒ Object

This method is called by the reactor. By default, this method reads the data from the IO and calls the `#on_message data` method.

This method is called within a lock on the connection (Mutex) - craeful from double locking.


43
44
45
46
# File 'lib/greactor/protocol.rb', line 43

def call c_io
  data = c_io.read
  on_message data if data
end

#closeObject

Closes the IO object - shortcut for @io.close which wraps the `Reactor.close_io socket` method.


35
36
37
# File 'lib/greactor/protocol.rb', line 35

def close
  @io.close
end

#on_connectObject

Called AFTER a connection is initialized but BEFORE it is added to the Reactor's IO loop.

Override this method to complete any initialization tasks. Avoid overriding the `#initialize io` method.


51
52
# File 'lib/greactor/protocol.rb', line 51

def on_connect
end

#on_disconnect(c_io) ⇒ Object

Called after the IO is closed


61
62
# File 'lib/greactor/protocol.rb', line 61

def on_disconnect c_io
end

#on_message(data) ⇒ Object

Called by the `Protocol#call` method after data is recieved.

This method is called within a lock on the connection (Mutex) - craeful from double locking.


57
58
# File 'lib/greactor/protocol.rb', line 57

def on_message data
end

#readObject

Reads data from the IO - shortcut for @io.read


30
31
32
# File 'lib/greactor/protocol.rb', line 30

def read
  @io.read
end

#write(data) ⇒ Object Also known as: send

Writes data to the IO - shortcut for @io.write


24
25
26
# File 'lib/greactor/protocol.rb', line 24

def write data
  @io.write data
end