Hoa central
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Socket\Connection\Handler Class Reference
Inheritance diagram for Hoa\Socket\Connection\Handler:

Public Member Functions

 __construct (Connection $connection)
 
 getConnection ()
 
 run ()
 
 merge (self $other)
 
 send ($message, Socket\Node $node=null)
 
 broadcast ($message)
 
 broadcastIf (\Closure $predicate, $message)
 

Protected Member Functions

 setConnection (Connection $connection)
 
 getOriginalConnection ()
 
 _run (Socket\Node $node)
 
 _send ($message, Socket\Node $node)
 

Protected Attributes

 $_originalConnection = null
 
 $_connection = null
 
 $_connections = []
 

Detailed Description

Class .

This class provides a connection handler: a complete connection skeleton. We are able to run() a connection (client or server), to merge() with other ones and to send messages in different ways (A -> A, A -> B, A -> * etc.).

Definition at line 51 of file Handler.php.

Constructor & Destructor Documentation

Hoa\Socket\Connection\Handler::__construct ( Connection  $connection)

Constructor. Must be called.

Parameters
\Hoa\Socket\Connection$connectionConnection.
Returns
void

Definition at line 82 of file Handler.php.

83  {
84  $this->_originalConnection = $connection;
85  $this->setConnection($connection);
86 
87  return;
88  }
setConnection(Connection $connection)
Definition: Handler.php:96

Here is the call graph for this function:

Member Function Documentation

Hoa\Socket\Connection\Handler::_run ( Socket\Node  $node)
abstractprotected

The node dedicated part of the run() method. A run is pretty simple, schematically:

while(true) foreach($connection->select() as $node)
    // body

The body is given by this method.

Parameters
\Hoa\Socket\Node$nodeNode.
Returns
void

Here is the caller graph for this function:

Hoa\Socket\Connection\Handler::_send (   $message,
Socket\Node  $node 
)
abstractprotected

The sending dedicated part of the self::send() method. If the send() method is overrided with more arguments, this method could return a function: it works like a currying.

Parameters
string$messageMessage.
\Hoa\Socket\Node$nodeNode (if null, current node).
Returns
void

Here is the caller graph for this function:

Hoa\Socket\Connection\Handler::broadcast (   $message)

Broadcast a message, i.e. send the message to all other nodes except the current one.

Parameters
string$messageMessage.
… …
Returns
void

Definition at line 277 of file Handler.php.

278  {
279  $currentNode = $this->getConnection()->getCurrentNode();
280  $arguments = func_get_args();
281  array_unshift(
282  $arguments,
283  function (Socket\Node $node) use ($currentNode) {
284  return $node !== $currentNode;
285  }
286  );
287 
288  return call_user_func_array([$this, 'broadcastIf'], $arguments);
289  }

Here is the call graph for this function:

Hoa\Socket\Connection\Handler::broadcastIf ( \Closure  $predicate,
  $message 
)

Broadcast a message to a subset of nodes that respect a predicate.

Parameters
\Closure$predicatePredicate. Take a node in argument.
string$messageMessage.
… …
Returns
void

Definition at line 299 of file Handler.php.

300  {
301  $connection = $this->getConnection();
302  $currentSocket = $this->getOriginalConnection()->getSocket();
303 
304  if (1 === func_num_args()) {
305  foreach ($connection->getNodes() as $node) {
306  if (true === $predicate($node) &&
307  $node->getConnection()->getSocket() === $currentSocket) {
308  $this->send($message, $node);
309  }
310  }
311 
312  return;
313  }
314 
315  $arguments = array_slice(func_get_args(), 2);
316  array_unshift($arguments, $message, null);
317  $callable = [$this, 'send'];
318 
319  foreach ($connection->getNodes() as $node) {
320  if (true === $predicate($node) &&
321  $node->getConnection()->getSocket() === $currentSocket) {
322  $arguments[1] = $node;
323  call_user_func_array($callable, $arguments);
324  }
325  }
326 
327  return;
328  }
send($message, Socket\Node $node=null)
Definition: Handler.php:240

Here is the call graph for this function:

Hoa\Socket\Connection\Handler::getConnection ( )

Get current connection.

Returns

Definition at line 119 of file Handler.php.

120  {
121  return $this->_connection;
122  }

Here is the caller graph for this function:

Hoa\Socket\Connection\Handler::getOriginalConnection ( )
protected

Get original connection.

Returns

Definition at line 109 of file Handler.php.

110  {
112  }

Here is the caller graph for this function:

Hoa\Socket\Connection\Handler::merge ( self  $other)

Merge a connection into this one. If we have two connections that must run at the same time, the Hoa::consider() and Hoa::is() methods are helpful but this whole class eases the merge of “high-level” connections.

Parameters
\Hoa\Socket\Connection$otherConnection to merge.
Returns

Definition at line 205 of file Handler.php.

206  {
207  $thisConnection = $this->getConnection();
208  $otherConnection = $other->getConnection();
209 
210  $thisConnection->consider($otherConnection);
211 
212  if ($otherConnection instanceof Socket\Server) {
213  $other->setConnection($thisConnection);
214  }
215 
216  $this->_connections[] = $other;
217 
218  return $this;
219  }

Here is the call graph for this function:

Hoa\Socket\Connection\Handler::run ( )

Run the connection.

Returns
void

Definition at line 143 of file Handler.php.

144  {
145  $connection = $this->getConnection();
146 
147  if ($connection instanceof Socket\Server) {
148  $connection->connectAndWait();
149  } else {
150  $connection->connect();
151  }
152 
153  while (true) {
154  foreach ($connection->select() as $node) {
155 
156  // Connection has failed to detect the node, maybe it is a resource
157  // from a merged client in a server.
158  if (false === is_object($node)) {
159  $socket = $node;
160 
161  foreach ($this->_connections as $other) {
162  $otherConnection = $other->getOriginalConnection();
163 
164  if (!($otherConnection instanceof Socket\Client)) {
165  continue;
166  }
167 
168  $node = $otherConnection->getCurrentNode();
169 
170  if ($node->getSocket() === $socket) {
171  $other->_run($node);
172 
173  continue 2;
174  }
175  }
176  }
177 
178  foreach ($this->_connections as $other) {
179  if (true === $connection->is($other->getOriginalConnection())) {
180  $other->_run($node);
181 
182  continue 2;
183  }
184  }
185 
186  $this->_run($node);
187  }
188  }
189 
190  $connection->disconnect();
191 
192  return;
193  }
_run(Socket\Node $node)

Here is the call graph for this function:

Hoa\Socket\Connection\Handler::send (   $message,
Socket\Node  $node = null 
)

Send a message to a specific node.

Parameters
string$messageMessage.
\Hoa\Socket\Node$nodeNode (if null, current node). current node).
Returns
mixed

Definition at line 240 of file Handler.php.

241  {
242  if (null === $node) {
243  $node = $this->getConnection()->getCurrentNode();
244  }
245 
246  if (null === $node) {
247  return null;
248  }
249 
250  $old = $this->getConnection()->_setStream($node->getSocket());
251  $send = $this->_send($message, $node);
252 
253  if ($send instanceof \Closure) {
254  $self = $this;
255 
256  return function () use (&$send, &$old, &$self) {
257  $out = call_user_func_array($send, func_get_args());
258  $self->getConnection()->_setStream($old);
259 
260  return $out;
261  };
262  }
263 
264  $this->getConnection()->_setStream($old);
265 
266  return $send;
267  }
_send($message, Socket\Node $node)

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Socket\Connection\Handler::setConnection ( Connection  $connection)
protected

Set current connection.

Parameters
\Hoa\Socket\Connection$connectionConnection.
Returns

Definition at line 96 of file Handler.php.

97  {
98  $old = $this->_connection;
99  $this->_connection = $connection;
100 
101  return $old;
102  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Socket\Connection\Handler::$_connection = null
protected

Definition at line 65 of file Handler.php.

Hoa\Socket\Connection\Handler::$_connections = []
protected

Definition at line 72 of file Handler.php.

Hoa\Socket\Connection\Handler::$_originalConnection = null
protected

Definition at line 58 of file Handler.php.


The documentation for this class was generated from the following file: