getSocket(ZMQ::SOCKET_REQ); $reqrep->connect("tcp://127.0.0.1:".get_cfg_var('REQREP_PORT')); $reqrep2 = $zmqCtx->getSocket(ZMQ::SOCKET_REQ); $reqrep2->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); // Wait for SMP to startup sleep(1); $deviceInfo = [ 'dinetrpc' => 1, 'id' => 1, 'req' => 'device:info', 'time' => 1234567890, 'device:uid' => '0200deadbeef00beef00cafebabe0000' ]; /////////////////////////////////////////////////////////////////////////////// $test->step("Start Secure Multi Proxy server"); /////////////////////////////////////////////////////////////////////////////// sleep(1); // Wait for SMP to startup /////////////////////////////////////////////////////////////////////////////// $test->step("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("10. Send a 'device:info' request from the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// $start = microtime(true); $reqrep->send(json_encode($deviceInfo)); /////////////////////////////////////////////////////////////////////////////// $test->step("12. Check if the request is sent through to the 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)); $n = 15; /////////////////////////////////////////////////////////////////////////////// $test->step("10. Do " . $n . " 'device:ping' request-replies to a non-existing device"); /////////////////////////////////////////////////////////////////////////////// $req = $deviceInfo; for ($i = 0; $i < 15; $i ++) { $start2 = microtime(true); $req['device:uid'] = "9900deadbeef99beef99cafebabe0099"; $req['id'] = 100 + $i; $req['req'] = 'device:ping'; $reqrep2->send(json_encode($req)); usleep(10 * 1000); do { $msg = $reqrep2->recv(); } while ($reqrep2->getSockOpt(ZMQ::SOCKOPT_RCVMORE)); $end2 = microtime(true); $test->range(round(($end2 - $start2) * 1000, 3), 0, 1000); $test->info("reqrep roundtrip took: " . round(($end2 - $start2) * 1000, 3) . 'ms'); $msg = json_decode($msg, true); $test->equal(1, $msg['dinetrpc']); $test->equal('device:ping', $msg['rep']); $test->isTrue(isset($msg['time'])); $test->equal($msg['id'], 100 + $i); } /////////////////////////////////////////////////////////////////////////////// $test->step("13. Send a reply through the second device interface"); /////////////////////////////////////////////////////////////////////////////// $mpackHex = "85aa6465766963653a756964d9203032303064656164626565663030626565663030636166656261626530303030a864696e657472706301a2696401a3726570ab6465766963653a696e666fa474696d65ce499602d2"; $ret = fwrite($sock, hex2bin("444A521000" . dechex((strlen($mpackHex) / 2) + 6) . $mpackHex), (strlen($mpackHex) / 2) + 6); $test->equal((strlen($mpackHex) / 2) + 6, $ret); /////////////////////////////////////////////////////////////////////////////// $test->step("14. Check if a reply is sent to the request-reply socket"); /////////////////////////////////////////////////////////////////////////////// do { $msg = $reqrep->recv(); } while ($reqrep->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("16. Disconnect from the device interface"); /////////////////////////////////////////////////////////////////////////////// fclose($sock); $test->finish();