Hoa central
Shared.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Worker;
38 
39 use Hoa\Socket;
40 
49 class Shared
50 {
56  protected $_client = null;
57 
58 
59 
68  public function __construct($workerId)
69  {
70  if (is_string($workerId)) {
71  $wid = Run::get($workerId);
72  $this->_client = new Socket\Client($wid['socket']);
73 
74  return;
75  } elseif ($workerId instanceof Socket\Client) {
76  $this->_client = $workerId;
77 
78  return;
79  }
80 
81  throw new Exception(
82  'Either you give a worker ID or you give an object of type ' .
83  '\Hoa\Socket\Client, but not anything else; given %s',
84  0,
85  is_object($workerId)
86  ? get_class($workerId)
87  : $workerId
88  );
89 
90  return;
91  }
92 
99  public function postMessage($message)
100  {
101  $this->_client->connect();
102  $this->_client->writeAll(Backend\Shared::pack(
103  Backend\Shared::TYPE_MESSAGE,
104  $message
105  ));
106  $this->_client->disconnect();
107 
108  return;
109  }
110 
116  public function getInformations()
117  {
118  $this->_client->connect();
119  $this->_client->writeAll(
120  Backend\Shared::pack(
121  Backend\Shared::TYPE_INFORMATIONS,
122  "\0"
123  )
124  );
125  $this->_client->read(2); // skip type.
126  $length = unpack('Nl', $this->_client->read(4));
127  $message = $this->_client->read($length['l']);
128  $this->_client->read(1); // skip eom.
129  $this->_client->disconnect();
130 
131  return unserialize($message);
132  }
133 }
static get($workerId)
Definition: Run.php:101
__construct($workerId)
Definition: Shared.php:68
postMessage($message)
Definition: Shared.php:99