14 require_once
'zmsg.php';
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');
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');
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');
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);
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');
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');
110 defined('HCE_ADMIN_CMD_DELIMITER') or define('HCE_ADMIN_CMD_DELIMITER', "\
t");
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);
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);
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');
178 $ret=array(
'error'=>HCE_PROTOCOL_ERROR_OK,
'context'=>null,
'socket'=>null,
'type'=>null,
'host'=>null,
'port'=>null,
'identity'=>null);
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());
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;
187 $con_ar=$connection_array;
192 if($ret[
'error']==HCE_PROTOCOL_ERROR_OK){
194 $ret[
'context']=
new ZMQContext();
195 if($ret[
'context']==null){
196 $ret[
'error']=HCE_PROTOCOL_ERROR_CONTEXT_CREATE;
199 $ret[
'socket']=
new ZMQSocket($ret[
'context'], ZMQ::SOCKET_DEALER);
200 if($ret[
'socket']==null){
201 $ret[
'error']=HCE_PROTOCOL_ERROR_SOCKET_CREATE;
204 $ret[
'socket']->setSockOpt(ZMQ::SOCKOPT_IDENTITY, $con_ar[
'identity']);
205 if($con_ar[
'type']==HCE_CONNECTION_TYPE_ADMIN){
207 $ret[
'socket']->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);
211 $ret[
'socket']->connect(HCE_PROTOCOL_ADMIN_DEFAULT.
'://'.$con_ar[
'host'].
':'.$con_ar[
'port']);
214 $ret[
'host']=$con_ar[
'host'];
215 $ret[
'port']=$con_ar[
'port'];
216 $ret[
'type']=$con_ar[
'type'];
217 $ret[
'identity']=$con_ar[
'identity'];
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;
239 return $connection_array;
251 $ret=array(
'error'=>HCE_PROTOCOL_ERROR_OK,
'message'=>null);
253 if(!isset($fields_array[
'body']) || !isset($fields_array[
'id'])){
254 $ret[
'error']=HCE_PROTOCOL_ERROR_FIELDS_NOT_SET;
260 if(isset($fields_array[
'route']) && trim($fields_array[
'route'])!=
''){
261 $ret[
'message']->wrap($fields_array[
'route'], NULL);
265 $ret[
'message']->wrap($fields_array[
'body'], NULL);
266 $ret[
'message']->wrap($fields_array[
'id'], NULL);
269 $ret[
'message']->send();
283 $ret=array(
'error'=>HCE_PROTOCOL_ERROR_OK,
'messages'=>array());
286 $read=$write=array();
289 $events=$poll->poll($read, $write, $timeout);
292 foreach($read as $socket) {
293 $zmsg_r=
new Zmsg($socket);
296 $ret[
'messages'][]=array(
'error'=>HCE_PROTOCOL_ERROR_OK,
'message'=>$zmsg_r,
'id'=>$zmsg_r->unwrap(),
'body'=>$zmsg_r->unwrap());
299 $ret[
'error']=HCE_PROTOCOL_ERROR_TIMEOUT;
313 $prefix=date(
'Y-m-d H:i:s').
'-'.microtime(
true).
'-';
316 $ret=HCE_CLIENT_IDENTITY_PREFIX.$prefix;
335 $ret=HCE_MSG_ID_PREFIX.$prefix;
355 return $handler.HCE_ADMIN_CMD_DELIMITER.$command.HCE_ADMIN_CMD_DELIMITER.$parameters.HCE_ADMIN_CMD_DELIMITER;
369 $ret=uniqid(md5(rand()),
true);
372 $ret=dechex(time()).dechex(mt_rand(1, 65535));
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]);
379 $ret=uniqid(php_uname(
'n').
'-',
true);
382 $ret=time().substr(md5(microtime(
true)), 0, rand(5, 12));
385 $ret=sprintf(
'%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
386 mt_rand(0, 65535), mt_rand(0, 65535),
389 bindec(substr_replace(sprintf(
'%016b', mt_rand(0, 65535)),
'01', 6, 2)),
393 mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)