HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
hce_node_api.inc.php
Go to the documentation of this file.
1 <?php
14 require_once 'zmsg.php';
15 
19 defined('HCE_HANDLER_ADMIN') or define('HCE_HANDLER_ADMIN', 'Admin');
20 defined('HCE_HANDLER_ROUTER_SERVER_PROXY') or define('HCE_HANDLER_ROUTER_SERVER_PROXY', 'RouterServerProxy');
21 defined('HCE_HANDLER_DATA_SERVER_PROXY') or define('HCE_HANDLER_DATA_SERVER_PROXY', 'DataServerProxy');
22 defined('HCE_HANDLER_DATA_CLIENT_PROXY') or define('HCE_HANDLER_DATA_CLIENT_PROXY', 'DataClientProxy');
23 defined('HCE_HANDLER_DATA_PROCESSOR_DATA') or define('HCE_HANDLER_DATA_PROCESSOR_DATA', 'DataProcessorData');
24 defined('HCE_HANDLER_DATA_CLIENT_DATA') or define('HCE_HANDLER_DATA_CLIENT_DATA', 'DataClientData');
25 defined('HCE_HANDLER_DATA_REDUCER_PROXY') or define('HCE_HANDLER_DATA_REDUCER_PROXY', 'DataReducerProxy');
26 
30 defined('HCE_CMD_ECHO') or define('HCE_CMD_ECHO', 'ECHO');
31 defined('HCE_CMD_LLGET') or define('HCE_CMD_LLGET', 'LLGET');
32 defined('HCE_CMD_LLSET') or define('HCE_CMD_LLSET', 'LLSET');
33 defined('HCE_CMD_MMGET') or define('HCE_CMD_MMGET', 'MMGET');
34 defined('HCE_CMD_MMSET') or define('HCE_CMD_MMSET', 'MMSET');
35 defined('HCE_CMD_MPMGET') or define('HCE_CMD_MPMGET', 'MPMGET');
36 defined('HCE_CMD_MPMSET') or define('HCE_CMD_MPMSET', 'MPMSET');
37 defined('HCE_CMD_MRCSGET') or define('HCE_CMD_MRCSGET', 'MRCSGET');
38 defined('HCE_CMD_MRCSSET') or define('HCE_CMD_MRCSSET', 'MRCSSET');
39 defined('HCE_CMD_POLL_TIMEOUT_GET') or define('HCE_CMD_POLL_TIMEOUT_GET', 'POLL_TIMEOUT_GET');
40 defined('HCE_CMD_POLL_TIMEOUT_SET') or define('HCE_CMD_POLL_TIMEOUT_SET', 'POLL_TIMEOUT_SET');
41 defined('HCE_CMD_PROPERTY_INTERVAL_GET') or define('HCE_CMD_PROPERTY_INTERVAL_GET', 'PROPERTY_INTERVAL_GET');
42 defined('HCE_CMD_PROPERTY_INTERVAL_SET') or define('HCE_CMD_PROPERTY_INTERVAL_SET', 'PROPERTY_INTERVAL_SET');
43 defined('HCE_CMD_DUMP_INTERVAL_GET') or define('HCE_CMD_DUMP_INTERVAL_GET', 'DUMP_INTERVAL_GET');
44 defined('HCE_CMD_DUMP_INTERVAL_SET') or define('HCE_CMD_DUMP_INTERVAL_SET', 'DUMP_INTERVAL_SET');
45 
49 defined('HCE_CMD_STAT') or define('HCE_CMD_STAT', 'STAT');
50 defined('HCE_CMD_TIME') or define('HCE_CMD_TIME', 'TIME');
51 defined('HCE_CMD_PROPERTIES') or define('HCE_CMD_PROPERTIES', 'PROPERTIES');
52 defined('HCE_CMD_REBUILD_SERVER_CONNECTION') or define('HCE_CMD_REBUILD_SERVER_CONNECTION', 'REBUILD_SERVER_CONNECTION');
53 defined('HCE_CMD_DISCONNECT_SERVER_CONNECTION') or define('HCE_CMD_DISCONNECT_SERVER_CONNECTION', 'DISCONNECT_SERVER_CONNECTION');
54 defined('HCE_CMD_REBUILD_CLIENT_CONNECTION') or define('HCE_CMD_REBUILD_CLIENT_CONNECTION', 'REBUILD_CLIENT_CONNECTION');
55 defined('HCE_CMD_DISCONNECT_CLIENT_CONNECTION') or define('HCE_CMD_DISCONNECT_CLIENT_CONNECTION', 'DISCONNECT_CLIENT_CONNECTION');
56 defined('HCE_CMD_UPDATE_SCHEMA') or define('HCE_CMD_UPDATE_SCHEMA', 'UPDATE_SCHEMA');
57 defined('HCE_CMD_REBUILD_CLIENT_CONNECTION') or define('HCE_CMD_REBUILD_CLIENT_CONNECTION', 'REBUILD_CLIENT_CONNECTION');
58 defined('HCE_CMD_SHUTDOWN') or define('HCE_CMD_SHUTDOWN', 'SHUTDOWN');
59 defined('HCE_CMD_SPHINX') or define('HCE_CMD_SPHINX', 'SPHINX');
60 defined('HCE_CMD_DRCE') or define('HCE_CMD_DRCE', 'DRCE');
61 defined('HCE_CMD_DRCE_SET_HOST' ) or define ('HCE_CMD_DRCE_SET_HOST', 'DRCE_SET_HOST' );
62 defined('HCE_CMD_DRCE_GET_HOST' ) or define ('HCE_CMD_DRCE_GET_HOST', 'DRCE_GET_HOST' );
63 defined('HCE_CMD_DRCE_SET_PORT' ) or define ('HCE_CMD_DRCE_SET_PORT', 'DRCE_SET_PORT' );
64 defined('HCE_CMD_DRCE_GET_PORT' ) or define ('HCE_CMD_DRCE_GET_PORT', 'DRCE_GET_PORT' );
65 defined('HCE_CMD_DRCE_GET_TASKS' ) or define ('HCE_CMD_DRCE_GET_TASKS', 'DRCE_GET_TASKS' );
66 defined('HCE_CMD_DRCE_GET_TASKS_INFO' ) or define ('HCE_CMD_DRCE_GET_TASKS_INFO', 'DRCE_GET_TASKS_INFO' );
67 defined('HCE_CMD_NODE_RECOVER_NOTIFICATION_CONNECTION' ) or define ('HCE_CMD_NODE_RECOVER_NOTIFICATION_CONNECTION', 'NODE_RECOVER_NOTIFICATION_CONNECTION');
68 defined('HCE_CMD_NODE_ROUTES' ) or define ('HCE_CMD_NODE_ROUTES', 'NODE_ROUTES');
69 defined('HCE_CMD_NODE_RESOURCE_USAGE' ) or define ('HCE_CMD_NODE_RESOURCE_USAGE', 'NODE_RESOURCE_USAGE');
70 defined('HCE_CMD_HEARTBEAT_DELAY_SET') or define ('HCE_CMD_HEARTBEAT_DELAY_SET', 'HEARTBEAT_DELAY_SET');
71 defined('HCE_CMD_HEARTBEAT_DELAY_GET') or define ('HCE_CMD_HEARTBEAT_DELAY_GET', 'HEARTBEAT_DELAY_GET');
72 defined('HCE_CMD_HEARTBEAT_TIMEOUT_SET') or define ('HCE_CMD_HEARTBEAT_TIMEOUT_SET', 'HEARTBEAT_TIMEOUT_SET');
73 defined('HCE_CMD_HEARTBEAT_TIMEOUT_GET') or define ('HCE_CMD_HEARTBEAT_TIMEOUT_GET', 'HEARTBEAT_TIMEOUT_GET');
74 defined('HCE_CMD_HEARTBEAT_MODE_SET') or define ('HCE_CMD_HEARTBEAT_MODE_SET', 'HEARTBEAT_MODE_SET');
75 defined('HCE_CMD_HEARTBEAT_MODE_GET') or define ('HCE_CMD_HEARTBEAT_MODE_GET', 'HEARTBEAT_MODE_GET');
76 
80 defined('HCE_PROTOCOL_ERROR_OK') or define('HCE_PROTOCOL_ERROR_OK', 0);
81 defined('HCE_PROTOCOL_ERROR_CONNECTION_PARAMS') or define('HCE_PROTOCOL_ERROR_CONNECTION_PARAMS', -1);
82 defined('HCE_PROTOCOL_ERROR_CONTEXT_CREATE') or define('HCE_PROTOCOL_ERROR_CONTEXT_CREATE', -2);
83 defined('HCE_PROTOCOL_ERROR_SOCKET_CREATE') or define('HCE_PROTOCOL_ERROR_SOCKET_CREATE', -3);
84 defined('HCE_PROTOCOL_ERROR_TIMEOUT') or define('HCE_PROTOCOL_ERROR_TIMEOUT', -5);
85 
86 
90 defined('HCE_ADMIN_CMD_SPHINX') or define('HCE_ADMIN_CMD_SPHINX', 'SPHINX');
91 defined('HCE_ADMIN_CMD_ECHO') or define('HCE_ADMIN_CMD_ECHO', 'ECHO');
92 defined('HCE_ADMIN_CMD_STAT') or define('HCE_ADMIN_CMD_STAT', 'STAT');
93 defined('HCE_ADMIN_CMD_REBUILD_SERVER_CONNECTION') or define('HCE_ADMIN_CMD_REBUILD_SERVER_CONNECTION', 'REBUILD_SERVER_CONNECTION');
94 defined('HCE_ADMIN_CMD_DISCONNECT_SERVER_CONNECTION') or define('HCE_ADMIN_CMD_DISCONNECT_SERVER_CONNECTION', 'DISCONNECT_SERVER_CONNECTION');
95 defined('HCE_ADMIN_CMD_REBUILD_CLIENT_CONNECTION') or define('HCE_ADMIN_CMD_REBUILD_CLIENT_CONNECTION', 'REBUILD_CLIENT_CONNECTION');
96 defined('HCE_ADMIN_CMD_DISCONNECT_CLIENT_CONNECTION') or define('HCE_ADMIN_CMD_DISCONNECT_CLIENT_CONNECTION', 'DISCONNECT_CLIENT_CONNECTION');
97 defined('HCE_ADMIN_CMD_UPDATE_SCHEMA') or define('HCE_ADMIN_CMD_UPDATE_SCHEMA', 'UPDATE_SCHEMA');
98 defined('HCE_ADMIN_CMD_REBUILD_CLIENT_CONNECTION') or define('HCE_ADMIN_CMD_REBUILD_CLIENT_CONNECTION', 'REBUILD_CLIENT_CONNECTION');
99 defined('HCE_ADMIN_CMD_SHUTDOWN') or define('HCE_ADMIN_CMD_SHUTDOWN', 'SHUTDOWN');
100 
104 defined('HCE_ADMIN_NODE_ADMIN_ERROR_OK') or define('HCE_ADMIN_NODE_ADMIN_ERROR_OK', 'OK');
105 defined('HCE_ADMIN_NODE_ADMIN_ERROR_ERROR') or define('HCE_ADMIN_NODE_ADMIN_ERROR_ERROR', 'ERROR');
106 
110 defined('HCE_ADMIN_CMD_DELIMITER') or define('HCE_ADMIN_CMD_DELIMITER', "\t"/*':'*/);
111 
115 defined('HCE_PROTOCOL_ADMIN_DEFAULT') or define('HCE_PROTOCOL_ADMIN_DEFAULT', 'tcp');
119 defined('HCE_HOST_ADMIN_DEFAULT') or define('HCE_HOST_ADMIN_DEFAULT', 'localhost');
123 defined('HCE_PORT_ADMIN_DEFAULT') or define('HCE_PORT_ADMIN_DEFAULT', 5548);
127 defined('HCE_PORT_ROUTER_DEFAULT') or define('HCE_PORT_ROUTER_DEFAULT', 5557);
131 defined('HCE_CMD_DELIMITER') or define('HCE_CMD_DELIMITER', ':');
135 defined('HCE_CLIENT_IDENTITY_PREFIX') or define('HCE_CLIENT_IDENTITY_PREFIX', 'HCE-CLU-');
139 defined('HCE_MSG_ID_PREFIX') or define('HCE_MSG_ID_PREFIX', 'ID-');
143 defined('HCE_MSG_RESPONSE_TIMEOUT') or define('HCE_MSG_RESPONSE_TIMEOUT', 1500);
147 defined('HCE_CONNECTION_TYPE_ADMIN') or define('HCE_CONNECTION_TYPE_ADMIN', 0);
148 defined('HCE_CONNECTION_TYPE_ROUTER') or define('HCE_CONNECTION_TYPE_ROUTER', 1);
149 
153 defined('HCE_HANDLER_TYPE_NULL') or define('HCE_HANDLER_TYPE_NULL', 0);
154 defined('HCE_HANDLER_TYPE_SPHINX') or define('HCE_HANDLER_TYPE_SPHINX', 1);
155 defined('HCE_HANDLER_TYPE_DRCE') or define('HCE_HANDLER_TYPE_DRCE', 2);
156 defined('HCE_HANDLER_TYPE_SQLITE') or define('HCE_HANDLER_TYPE_SQLITE', 3);
157 defined('HCE_HANDLER_TYPE_MYSQL') or define('HCE_HANDLER_TYPE_MYSQL', 4);
158 defined('HCE_HANDLER_TYPE_FAKE') or define('HCE_HANDLER_TYPE_FAKE', 5);
159 
163 defined('HCE_HANDLER_COVER_FIELD_TYPE') or define('HCE_HANDLER_COVER_FIELD_TYPE', 'type');
164 defined('HCE_HANDLER_COVER_FIELD_DATA') or define('HCE_HANDLER_COVER_FIELD_DATA', 'data');
165 defined('HCE_HANDLER_COVER_FIELD_TTL') or define('HCE_HANDLER_COVER_FIELD_TTL', 'ttl');
166 
167 
168 /*
169  * @desc create ACN connection (new zmq context, zmq socket and connect using specified or default options)
170  * @param $connection_array - array of connection configuration options: host, port, type {HCE_CONNECTION_TYPE_ADMIN, HCE_CONNECTION_TYPE_ROUTER}, identity - string of unique client Id
171  * in cluster
172  *
173  * @return ACN connection array; error item is HCE_PROTOCOL_ERROR_OK - if success and negative error code: HCE_PROTOCOL_ERROR_CONNECTION_PARAMS - wrong one or several connection parameter,
174  * HCE_PROTOCOL_ERROR_CONTEXT_CREATE - error create zmq context, HCE_PROTOCOL_ERROR_SOCKET_CREATE - error create zmq socket
175  */
176  function hce_connection_create($connection_array=null){
177  //Init returned connection
178  $ret=array('error'=>HCE_PROTOCOL_ERROR_OK, 'context'=>null, 'socket'=>null, 'type'=>null, 'host'=>null, 'port'=>null, 'identity'=>null);
179 
180  //Check connection parameters
181  if($connection_array===null){
182  $con_ar=array('host'=>HCE_HOST_ADMIN_DEFAULT, 'port'=>HCE_PORT_ADMIN_DEFAULT, 'type'=>HCE_CONNECTION_TYPE_ADMIN, 'identity'=>hce_unique_client_id());
183  }else{
184  if(!isset($connection_array['host']) || !isset($connection_array['port']) || !isset($connection_array['type']) || !isset($connection_array['identity'])){
185  $ret['error']=HCE_PROTOCOL_ERROR_CONNECTION_PARAMS;
186  }else{
187  $con_ar=$connection_array;
188  }
189  }
190 
191  //Create connection
192  if($ret['error']==HCE_PROTOCOL_ERROR_OK){
193  //Create zmq context
194  $ret['context']=new ZMQContext();
195  if($ret['context']==null){
196  $ret['error']=HCE_PROTOCOL_ERROR_CONTEXT_CREATE;
197  }else{
198  //Create zmq socket
199  $ret['socket']=new ZMQSocket($ret['context'], ZMQ::SOCKET_DEALER);
200  if($ret['socket']==null){
201  $ret['error']=HCE_PROTOCOL_ERROR_SOCKET_CREATE;
202  }else{
203  //Set request identity
204  $ret['socket']->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $con_ar['identity']);
205  if($con_ar['type']==HCE_CONNECTION_TYPE_ADMIN){
206  //Set linger 0, do not wait on close
207  $ret['socket']->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
208  }
209 
210  //Connect
211  $ret['socket']->connect(HCE_PROTOCOL_ADMIN_DEFAULT.'://'.$con_ar['host'].':'.$con_ar['port']);
212 
213  //Set properties
214  $ret['host']=$con_ar['host'];
215  $ret['port']=$con_ar['port'];
216  $ret['type']=$con_ar['type'];
217  $ret['identity']=$con_ar['identity'];
218  }
219  }
220  }
221 
222  return $ret;
223  }
224 
225 /*
226  * @desc delete ACN connection (disconnect zmq socket, free zmq socket and context)
227  * @param $connection_array - array of connection configuration options: host, port, type and identity
228  *
229  * @return ACN connection array in initial state
230  */
231  function hce_connection_delete(&$connection_array){
232  $connection_array['context']=NULL;
233  $connection_array['socket']=NULL;
234  $connection_array['host']=NULL;
235  $connection_array['port']=NULL;
236  $connection_array['type']=NULL;
237  $connection_array['identity']=NULL;
238 
239  return $connection_array;
240  }
241 
242 /*
243  * @desc send ACN connection message
244  * @param $hce_connection - acn connection returned by hce_connection_create() call
245  * $fields_array - array of message fields 'id' and 'body'
246  *
247  * @return acn message array or error HCE_PROTOCOL_ERROR_FIELDS_NOT_SET - fields array not set proper way
248  */
249  function hce_message_send($hce_connection, $fields_array){
250  //Init return acn message array
251  $ret=array('error'=>HCE_PROTOCOL_ERROR_OK, 'message'=>null);
252 
253  if(!isset($fields_array['body']) || !isset($fields_array['id'])){
254  $ret['error']=HCE_PROTOCOL_ERROR_FIELDS_NOT_SET;
255  }else{
256  //Create message
257  $ret['message']=new Zmsg($hce_connection['socket']);
258 
259  //Set route value to message depends from existing or not value input data about route
260  if(isset($fields_array['route']) && trim($fields_array['route'])!=''){
261  $ret['message']->wrap($fields_array['route'], NULL);
262  }
263 
264  //Set fields
265  $ret['message']->wrap($fields_array['body'], NULL);
266  $ret['message']->wrap($fields_array['id'], NULL);
267 
268  //Send message
269  $ret['message']->send();
270  }
271 
272  return $ret;
273  }
274 
275 /*
276  * @desc receive ACN connection message
277  * @param $hce_connection - acn connection returned by hce_connection_create() call
278  *
279  * @return ACN responses array or HCE_PROTOCOL_ERROR_TIMEOUT - if timeout reached
280  */
281  function hce_message_receive($hce_connection, $timeout=HCE_MSG_RESPONSE_TIMEOUT){
282  //Init return acn response array
283  $ret=array('error'=>HCE_PROTOCOL_ERROR_OK, 'messages'=>array());
284 
285  //Poll socket for a reply, with timeout
286  $read=$write=array();
287  $poll=new ZMQPoll();
288  $poll->add($hce_connection['socket'], ZMQ::POLL_IN);
289  $events=$poll->poll($read, $write, $timeout);
290  //Handle events
291  if($events){
292  foreach($read as $socket) {
293  $zmsg_r=new Zmsg($socket);
294  $zmsg_r->recv();
295  //echo PHP_EOL.'---------'.PHP_EOL.$zmsg_r->__toString().PHP_EOL.'---------'.PHP_EOL;
296  $ret['messages'][]=array('error'=>HCE_PROTOCOL_ERROR_OK, 'message'=>$zmsg_r, 'id'=>$zmsg_r->unwrap(), 'body'=>$zmsg_r->unwrap());
297  }
298  }else{
299  $ret['error']=HCE_PROTOCOL_ERROR_TIMEOUT;
300  }
301  $poll->remove($hce_connection['socket']);
302 
303  return $ret;
304  }
305 
306 /*
307  * @desc generate unique client identifier
308  *
309  * @return unique client identifier string
310  */
311  function hce_unique_client_id($type=1, $prefix=null){
312  if($prefix===null){
313  $prefix=date('Y-m-d H:i:s').'-'.microtime(true).'-';
314  }
315 
316  $ret=HCE_CLIENT_IDENTITY_PREFIX.$prefix;
317 
318  if($type==0){
319  //Simple fast
320  $ret.=hce_unique_id(1);
321  }else{
322  //More accurate complexity
323  $ret.=hce_unique_id(3).'-'.hce_unique_id(5);
324  }
325 
326  return $ret;
327  }
328 
329 /*
330  * @desc generate unique message identifier
331  *
332  * @return unique message identifier string
333  */
334  function hce_unique_message_id($type=1, $prefix=''){
335  $ret=HCE_MSG_ID_PREFIX.$prefix;
336 
337  if($type==0){
338  //Simple fast
339  $ret.=hce_unique_id(0);
340  }else{
341  //More accurate complexity
342  //$ret.=hce_unique_id(3).'-'.hce_unique_id(5);
343  $ret.=hce_unique_id(4);
344  }
345 
346  return $ret;
347  }
348 
349 /*
350  * @desc create ACN API admin message
351  *
352  * @return ACN API admin message string
353  */
354  function hce_admin_message_create($handler, $command, $parameters){
355  return $handler.HCE_ADMIN_CMD_DELIMITER.$command.HCE_ADMIN_CMD_DELIMITER.$parameters.HCE_ADMIN_CMD_DELIMITER;
356  }
357 
358 
359 /*
360  * @desc generate unique identifier by one of several methods
361  *
362  * @return unique message identifier string
363  */
364  function hce_unique_id($type=0){
365  $ret=null;
366 
367  if($type==0){
368  //Complex string
369  $ret=uniqid(md5(rand()), true);
370  }elseif($type==1){
371  //Complex string hexadec
372  $ret=dechex(time()).dechex(mt_rand(1, 65535));
373  }elseif($type==2){
374  //Random for better crypto cypher hexadec
375  $r=unpack('v*', fread(fopen('/dev/random', 'r'), 16));
376  $ret=sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $r[1], $r[2], $r[3], $r[4] & 0x0fff | 0x4000, $r[5] & 0x3fff | 0x8000, $r[6], $r[7], $r[8]);
377  }elseif($type==3){
378  //Host name and microseconds
379  $ret=uniqid(php_uname('n').'-', true);
380  }elseif($type==4){
381  //Another fast useful for file name
382  $ret=time().substr(md5(microtime(true)), 0, rand(5, 12));
383  }elseif($type==5){
384  //The field names refer to RFC 4122 section 4.1.2
385  $ret=sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
386  mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
387  mt_rand(0, 65535), // 16 bits for "time_mid"
388  mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
389  bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
390  // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
391  // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
392  // 8 bits for "clk_seq_low"
393  mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
394  );
395  }
396 
397  return $ret;
398  }
399 
400 ?>