getSocket(ZMQ::SOCKET_REQ); $reqrep->connect("tcp://127.0.0.1:".get_cfg_var('REQREP_PORT')); $pubsub = $zmqCtx->getSocket(ZMQ::SOCKET_SUB); $pubsub->connect("tcp://127.0.0.1:".get_cfg_var('PUB_PORT')); usleep(50*1000); $pubsub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); $pubsub->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 1000); $logging = $zmqCtx->getSocket(ZMQ::SOCKET_SUB); $logging->connect("tcp://127.0.0.1:".get_cfg_var('LOGIF_PORT')); usleep(50*1000); $logging->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); $logging->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 1000); // Wait for SMP to startup sleep(1); $deviceInfo = [ 'dinetrpc' => 1, 'id' => 1, 'req' => 'device:info', 'time' => 1234567890, 'device:uid' => '0200deadbeef00beef00cafebabe0000' ]; /////////////////////////////////////////////////////////////////////////////// $test->step("1. Start Secure Multi Proxy server"); /////////////////////////////////////////////////////////////////////////////// sleep(1); // Wait for SMP to startup /////////////////////////////////////////////////////////////////////////////// $test->step("2. Open connection on the device interface"); /////////////////////////////////////////////////////////////////////////////// $sock = fsockopen("localhost", get_cfg_var('TCP_PORT')); $test->isTrue(is_resource($sock)); usleep(300 * 1000); /////////////////////////////////////////////////////////////////////////////// $test->step("3. Send the handshake"); /////////////////////////////////////////////////////////////////////////////// $ret = fwrite($sock, hex2bin("444A52010026" . bin2hex("0200deadbeef00beef00cafebabe0000")), 38); $test->equal(38, $ret); $ret = fread($sock, 6); $ret = bin2hex($ret); $test->equal(12, strlen($ret)); $bytesRemain = hexdec(substr($ret, 8)) - 6; $ret = fread($sock, $bytesRemain); $test->equal('MKAY', $ret); /////////////////////////////////////////////////////////////////////////////// $test->step("4. Check if a connection:connect message is sent on the publish interface"); /////////////////////////////////////////////////////////////////////////////// $msg = $pubsub->recv(); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:connect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); /////////////////////////////////////////////////////////////////////////////// $test->step("5. Check if a connection:connect message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// $msg = $logging->recv(); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:connect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); /////////////////////////////////////////////////////////////////////////////// $test->step("6. Open a second connection on the device interface"); /////////////////////////////////////////////////////////////////////////////// $sock2 = fsockopen("localhost", get_cfg_var('TCP_PORT')); $test->isTrue(is_resource($sock2)); usleep(300 * 1000); /////////////////////////////////////////////////////////////////////////////// $test->step("7. Send the handshake on the second connection"); /////////////////////////////////////////////////////////////////////////////// $ret = fwrite($sock2, hex2bin("444A52010026" . bin2hex("0200deadbeef00beef00cafebabe0000")), 38); $test->equal(38, $ret); $ret = fread($sock2, 6); $ret = bin2hex($ret); $test->equal(12, strlen($ret)); $bytesRemain = hexdec(substr($ret, 8)) - 6; $ret = fread($sock2, $bytesRemain); $test->equal('MKAY', $ret); /////////////////////////////////////////////////////////////////////////////// $test->step("8. Check if a connection:connect message is sent on the publish interface"); /////////////////////////////////////////////////////////////////////////////// $msg = $pubsub->recv(); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:connect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); /////////////////////////////////////////////////////////////////////////////// $test->step("9. Check if a connection:connect message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// $msg = $logging->recv(); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:connect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); /////////////////////////////////////////////////////////////////////////////// $test->step("10. Send a 'device:info' request from the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// $start = microtime(true); $reqrep->send(json_encode($deviceInfo)); /////////////////////////////////////////////////////////////////////////////// $test->step("11. Check if the message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['req']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->equal($msg['time'], 1234567890); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("12. Check if the request is sent through to the second device interface"); /////////////////////////////////////////////////////////////////////////////// $ret = fread($sock2, 6); $ret = bin2hex($ret); $test->equal(12, strlen($ret)); $test->equal("444a5210005c", $ret); $bytesRemain = hexdec(substr($ret, 8)) - 6; $ret = fread($sock2, $bytesRemain); $test->equal("85", substr(bin2hex($ret), 0, 2)); sleep(9); /////////////////////////////////////////////////////////////////////////////// $test->step("14. Check if a timeout reply is sent to the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $reqrep->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $end = microtime(true); $test->info("reqrep roundtrip took: " . round(($end - $start) * 1000, 3) . 'ms'); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['error'])); $test->equal(9, $msg['error']['code']); $test->info("error code is: " . $msg['error']['code']); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("15. Check if the reply is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['error'])); $test->equal(9, $msg['error']['code']); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("19. Send a 'device:info' request from the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// $start = microtime(true); $reqrep->send(json_encode($deviceInfo)); /////////////////////////////////////////////////////////////////////////////// $test->step("20. Check if the message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['req']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->equal($msg['time'], 1234567890); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("21. Check if the request is sent through to the first device interface"); /////////////////////////////////////////////////////////////////////////////// $ret = fread($sock, 6); $ret = bin2hex($ret); $test->equal(12, strlen($ret)); $test->equal("444a5210005c", $ret); $bytesRemain = hexdec(substr($ret, 8)) - 6; $ret = fread($sock, $bytesRemain); $test->equal("85", substr(bin2hex($ret), 0, 2)); /////////////////////////////////////////////////////////////////////////////// $test->step("22. Send a reply through the first device interface"); /////////////////////////////////////////////////////////////////////////////// $mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2"; $start = microtime(true); $ret = fwrite($sock, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6); $test->equal((strlen($mpackHex) / 2) + 6, $ret); /////////////////////////////////////////////////////////////////////////////// $test->step("23. Check if a reply is sent to the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $reqrep->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $end = microtime(true); $test->info("reqrep roundtrip took: " . round(($end - $start) * 1000, 3) . 'ms'); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("24. Check if the message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("25. Disconnect from the device interface"); /////////////////////////////////////////////////////////////////////////////// fclose($sock); /////////////////////////////////////////////////////////////////////////////// $test->step("26. Check if a connection:disconnect message is sent on the publish interface"); /////////////////////////////////////////////////////////////////////////////// $msg = $pubsub->recv(); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:disconnect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); $test->isTrue(isset($msg['result'][0]['code'])); /////////////////////////////////////////////////////////////////////////////// $test->step("27. Check if a connection:disconnect message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('connection:disconnect', $msg['pub']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->isTrue(isset($msg['result'][0]['peer'])); $test->isTrue(isset($msg['result'][0]['code'])); /////////////////////////////////////////////////////////////////////////////// $test->step("Send a 'device:info' request from the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// $start = microtime(true); $reqrep->send(json_encode($deviceInfo)); /////////////////////////////////////////////////////////////////////////////// $test->step("Check if the message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['req']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->equal($msg['time'], 1234567890); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("Check if the request is sent through to the first device interface"); /////////////////////////////////////////////////////////////////////////////// $ret = fread($sock2, 6); $ret = bin2hex($ret); $test->equal(12, strlen($ret)); $test->equal("444a5210005c", $ret); $bytesRemain = hexdec(substr($ret, 8)) - 6; $ret = fread($sock2, $bytesRemain); $test->equal("85", substr(bin2hex($ret), 0, 2)); /////////////////////////////////////////////////////////////////////////////// $test->step("Send a reply through the first device interface"); /////////////////////////////////////////////////////////////////////////////// $mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2"; $start = microtime(true); $ret = fwrite($sock2, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6); $test->equal((strlen($mpackHex) / 2) + 6, $ret); /////////////////////////////////////////////////////////////////////////////// $test->step("Check if a reply is sent to the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $reqrep->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $end = microtime(true); $test->info("reqrep roundtrip took: " . round(($end - $start) * 1000, 3) . 'ms'); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->equal($msg['id'], 1); /////////////////////////////////////////////////////////////////////////////// $test->step("Check if the message is sent on the logging interface"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $logging->recv(); } while ($logging->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:info', $msg['rep']); $test->equal('0200deadbeef00beef00cafebabe0000', $msg['device:uid']); $test->isTrue(isset($msg['time'])); $test->equal($msg['id'], 1); $test->finish();