HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
drce.ini.php
Go to the documentation of this file.
1 <?php
14 //Set default timezone if not set in host environment
15 @date_default_timezone_set(@date_default_timezone_get());
16 
17 require_once 'hce_cli_api.inc.php';
18 require_once 'hce_drce_api.inc.php';
19 
20 
21 defined('LOG_MODE_JSON') or define('LOG_MODE_JSON', 3);
22 defined('RESPONSE_TIMEOUT_DEFAULT') or define('RESPONSE_TIMEOUT_DEFAULT', 5000);
23 
24 if(php_sapi_name()!=='cli' || !defined('STDIN')){
25  echo 'Only cli execution mode supported'.PHP_EOL;
26  exit(1);
27 }
28 
30 
31 if(isset($args['h']) || isset($args['help'])){
32  echo 'Usage: '.
33  $argv[0].
34  ' [--host=<router_host>] [--port=<router_port>] [--request=<request_type{SET, CHECK, TERMINATE, GET, DELETE}>] [--id=<task_id>] [--json=<json_protocol_file>]'.
35  ' [--n=<max_queries_number>] [--d=<request_delay_ms>] [--ttl=<request_msg_ttl_ms>] [--l=<log_mode{0-no,1-result,(2-params),3-json,4-result+object}>]'.
36  ' [--t=<response_timeout_ms, '.RESPONSE_TIMEOUT_DEFAULT.' default>] [--cid=<client_id>] [--subtasks=<json_subtasks_file>]'.
37  ' [--route=<route string include of node names list comma separated>]'.
38  ' [--cover=<0-strip cover from response|1-response with cover> used only with --l=3]'.PHP_EOL;
39  exit(1);
40 }
41 
42 $MAX_QUERIES=isset($args['n']) ? $args['n']+0 : '1';
43 $REQUEST_DELAY=isset($args['d']) ? $args['d']+0 : 0;
44 $LOG_MODE=isset($args['l']) ? $args['l']+0 : 2;
45 $RESPONSE_TIMEOUT=isset($args['t']) ? ($args['t']+0) : HCE_DRCE_EXEC_DEFAULT_TIMEOUT;
46 $Connection_host=isset($args['host']) ? $args['host'] : 'localhost';
47 $Connection_port=isset($args['port']) ? $args['port'] : '5556';
48 $Route=isset($args['route']) ? $args['route'] : '';
50 $REQUEST_TYPE=isset($args['request']) ? $args['request'] : 'CHECK';
51 $REQUEST_TTL=isset($args['ttl']) ? ($args['ttl']+0) : HCE_DRCE_EXEC_DEFAULT_TTL;
52 $requests_names=array('SET'=>HCE_DRCE_REQUEST_TYPE_SET, 'CHECK'=>HCE_DRCE_REQUEST_TYPE_CHECK, 'TERMINATE'=>HCE_DRCE_REQUEST_TYPE_TERMINATE, 'GET'=>HCE_DRCE_REQUEST_TYPE_GET, 'DELETE'=>HCE_DRCE_REQUEST_TYPE_DELETE);
53 if(!in_array($REQUEST_TYPE, $requests_names)){
54  echo 'Error: --request not supported "'.$REQUEST_TYPE.'"'.PHP_EOL;
55  exit(1);
56 }else{
58 }
59 $REQUEST_ID=isset($args['id']) ? $args['id'] : 0;
60 if($REQUEST_ID==0 && $REQUEST_TYPE!=0){
61  echo 'Error: this --request required --id value greater than zero'.PHP_EOL;
62  exit(1);
63 }else{
64  //Generate request Id
65  if($REQUEST_ID==0 && $REQUEST_TYPE==0){
66  $REQUEST_ID=sprintf('%u', crc32(hce_unique_message_id(1, microtime(true))));
67  }
68 }
69 
70 //Get json file
71 if(!isset($args['json']) || !file_exists($args['json'])){
72  echo 'Error: --json required option or file not set or not found'.PHP_EOL;
73  exit(1);
74 }else{
75  $input_json=trim(file_get_contents($args['json']));
76 }
77 
78 //Get subtasks file
79 if(isset($args['subtasks'])){
80  if(!file_exists($args['subtasks'])){
81  echo 'Error: --subtasks file not set or not found: "'.$args['subtasks'].'"'.PHP_EOL;
82  exit(1);
83  }else{
84  //echo $args['subtasks'].PHP_EOL;
85  $subtasks=file_get_contents($args['subtasks']);
86  //echo $fc.PHP_EOL;
87  $subtasks=json_encode(file_get_contents_json(json_decode($subtasks, true)));
88  //echo 'subtasks1:['.cli_prettyPrintJson($subtasks, ' ').']'.PHP_EOL;
89  //echo 'subtasks2:['.json_encode(hce_drce_create_subtasks_array($subtasks)).']'.PHP_EOL;
90  }
91 }else{
92  $subtasks='';
93 }
94 
95 //Strip cover of general router protocol for resulted json. Can be used only for router request.
96 $strip_cover=isset($args['cover']) ? $args['cover']+0 : 1;
97 
100 
101 if($LOG_MODE==2 || $LOG_MODE==4){
102  echo 'Request json: '.PHP_EOL.$Request_body.PHP_EOL;
103  if($LOG_MODE==4){
104  $r=json_decode($Request_body, true);
105  if(isset($r[HCE_DRCE_EXEC_FIELD_DATA])){
106  $r[HCE_DRCE_EXEC_FIELD_DATA]=json_decode($r[HCE_DRCE_EXEC_FIELD_DATA], true);
107  if(isset($r[HCE_DRCE_EXEC_FIELD_DATA][HCE_DRCE_EXEC_FIELD_DATA])){
108  $r[HCE_DRCE_EXEC_FIELD_DATA][HCE_DRCE_EXEC_FIELD_DATA]=json_decode(base64_decode($r[HCE_DRCE_EXEC_FIELD_DATA][HCE_DRCE_EXEC_FIELD_DATA]), true);
109  if(is_array($r[HCE_DRCE_EXEC_FIELD_DATA]['subtasks'])){
110  foreach($r[HCE_DRCE_EXEC_FIELD_DATA]['subtasks'] as $key=>$st){
111  if(is_array($st) && isset($st['data'])){
112  $r[HCE_DRCE_EXEC_FIELD_DATA]['subtasks'][$key]['data']=json_decode(base64_decode($st['data']), true);
113  }
114  }
115  }
116  echo PHP_EOL.cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
117  }
118  }
119  }
120 }
121 
122 if($Request_body===null){
123  echo 'Error: create request json fault'.PHP_EOL;
124  exit(1);
125 }
126 
127 ?>