/** * creates a connection using connection factory * @param $connFactory SDO Data object type = msd:ConnectionFactory * @return SAMConnection or null */ private function _configConnection($connFactory) { $optionsarray = array(); foreach ($connFactory as $key => $value) { switch ($key) { case "protocol": $protocol = $value; break; default: $optionsarray[self::$samOptions[$key]] = $value; break; } } /*check mandatory fields */ if (!isset($protocol)) { throw new SCA_RuntimeException('message binding configuration connectionFactory missing: protocol. '); } if (self::$test_mode) { /*in test mode, use $this::test_queueborker */ if (is_null(self::$test_queueborker)) { self::$test_queueborker = array(); } $connection = null; } else { /*connecting to a real broker*/ $connection = new SAMConnection(); $connection->connect($protocol, $optionsarray); if (!$connection) { throw new SCA_RuntimeException("SAM connection failed"); } } return $connection; }
<?php require 'SAM/php_sam.php'; //create a new connection object $conn = new SAMConnection(); //start initialise the connection $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883)); //create a new MQTT message with the output of the shell command as the body $msgCpu = new SAMMessage($_REQUEST['message']); //send the message on the topic cpu $conn->send('topic://' . $_REQUEST['target'], $msgCpu); $conn->disconnect(); echo 'MQTT Message to ' . $_REQUEST['target'] . ' sent: ' . $_REQUEST['message'];
<?php require 'SAM/php_sam.php'; define("SAM_HOST", "SAM_HOST"); define("SAM_PORT", "SAM_PORT"); define("SAM_WAIT", "SAM_WAIT"); define("SAM_MQTT_QOS", "SAM_MQTT_QOS"); //create a new connection object $conn = new SAMConnection(); //start initialise the connection $conn->connect(SAM_MQTT, array(SAM_HOST => "test.mosquitto.org", SAM_PORT => 1883)); //subscribe to topic cpu $subUp = $conn->subscribe('topic://cpupc1-asitha', array(SAM_WAIT => 1, SAM_MQTT_QOS => 0)) or die('could not subscribe'); //print confirmation to terminal echo "subscribed"; $i = 0; $prev = ""; while ($conn) { //receive latest message on topic $subUp $msgUp = $conn->receive($subUp); //if($msgUp==true) /* echo $conn->errno; echo $conn->error; echo $conn->debug; */ //if there is a message if ($msgUp) { if (!$msgUp->body == "") { //echo message to terminal // echo $msgUp->body; //if ($msgUp->body!=$prev) // {
<?php // Saved state of control packet $packet = array('setpoint' => array('type' => 'int', 'value' => 16.5)); $packet['setpoint']['value'] = (double) $_GET['val']; echo "State set to: " . $packet['setpoint']['value']; require 'SAM/php_sam.php'; $conn = new SAMConnection(); $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883)); $msg_state = new SAMMessage($packet['setpoint']['value']); $conn->send('topic://state', $msg_state); $conn->disconnect();
<?php error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); require 'SAM/php_sam.php'; $conn = new SAMConnection(); $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883)); $i = 0; while (true) { if (time() - $ltime > 1) { $ltime = time(); print $i . "\n"; $i = 0; } $msg_rawserial = new SAMMessage("hello"); $conn->send('topic://myqueue', $msg_rawserial); $i++; }
<?php /* @info Αρχείο για την ειδοποίηση των οδηγών ότι τον επέλεξε ο πελάτης και είναι έτοιμος για παραλαβή */ header('Content-Type: text/html; charset=utf-8'); require 'SAM/php_sam.php'; require 'config.php'; $conn = new SAMConnection(); //type:1 = notifyDrivers //type:2 = message for acception if (isset($_REQUEST["customerlat"]) && isset($_REQUEST["customerlon"]) && isset($_REQUEST["driverDevIdtoSend"]) && isset($_REQUEST["selectedOrderid"])) { $message = "2" . ":" . "Πελάτης για παραλαβή" . ":" . $_REQUEST["customerlat"] . ":" . $_REQUEST["customerlon"] . ":" . $_REQUEST["driverDevIdtoSend"] . ":" . $_REQUEST["selectedOrderid"]; $target = $_REQUEST["driverDevIdtoSend"]; } else { $message = ""; $target = ""; } $conn->connect(SAM_MQTT, $SAM_MQTTCONF); $conn->send('topic://' . $target, $message); $conn->disconnect(); //for debug function write2file($string) { $file = fopen("test.txt", "a"); fwrite($file, $string); fclose($file); }
<p/> <hr/> Attempting connection to valid host - <?php $conn = new SAMConnection(); if ($conn->connect($protocol, array(SAM_HOST => $host, SAM_PORT => $port, SAM_BROKER => $broker))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a second connection to valid host - <?php $conn = new SAMConnection(); if ($conn->connect($protocol, array(SAM_HOST => $host, SAM_PORT => $port, SAM_BROKER => $broker))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting send - <?php if ($conn) { $msg = new SAMMessage('This is a test message'); $msg->header->SAM_REPLY_TO = 'queue://test/replyQueue'; $msg->header->SAM_TYPE = SAM_TEXT; $msg->header->myStringProp1 = array('a string property', SAM_STRING);
<p/> <hr/> Attempting connection to valid host -<br> <?php $conn1 = new SAMConnection(); if ($conn1->connect($protocol, array(SAM_HOST => $host, SAM_PORT => $port, SAM_BROKER => $broker))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a second connection to valid host -<br> <?php $conn = new SAMConnection(); if ($conn->connect($protocol, array(SAM_HOST => $host, SAM_PORT => $port, SAM_BROKER => $broker))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a receive with an invalid subscription id -<br> <?php if ($conn) { $msg = $conn->receive('invalid sub name', array(SAM_WAIT => 5000)); if (!$msg) { echo '<font color="green"><b>OK</b></font> receive failed with errno=' . $conn->errno . ' ' . $conn->error . '<br>'; } else {
$msg = new SAMMessage('This is a 2nd test message'); $correlid2 = $conn->send($queuename, $msg, array(SAM_SELECT => $correlid)); if (!$correlid) { echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}"; } else { echo '<font color="green"><b>OK</b></font> correlID=' . $correlid; } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?> <p/> <hr/> Creating 2nd connection - <?php $conn2 = new SAMConnection(); if ($conn2->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting receive using 2nd connection -<br/> <?php if ($conn2) { $msg = $conn2->receive($queuename, array(SAM_WAIT => 5000)); if ($msg) { echo '<font color="red"><b>FAILED</b></font> Oops, that message shouldn\'t have been accessible!'; } else {
<p/> <hr/> Attempting connection to valid host -<br> <?php $conn1 = new SAMConnection(); if ($conn1->connect($protocol)) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a second connection to valid host -<br> <?php $conn = new SAMConnection(); if ($conn->connect($protocol, array(SAM_MQTT_CLEANSTART => false))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a receive with an invalid subscription id -<br> <?php if ($conn) { $msg = $conn->receive('invalid sub name as it is too long', array(SAM_WAIT => 5000)); if (!$msg) { echo '<font color="green"><b>OK</b></font> receive failed with errno=' . $conn->errno . ' ' . $conn->error . '<br>'; } else {
<?php error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); require "SAM/php_sam.php"; $conn = new SAMConnection(); $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => '1883')); $subUp = $conn->subscribe('topic://noderx') or die('could not subscribe'); while ($conn) { $msgUp = $conn->receive($subUp); if ($msgUp) { $body = $msgUp->body . "\n"; print $body; } }
$msg = new SAMMessage('This is a 2nd test message'); $correlid2 = $conn->send($queuename, $msg, array(SAM_SELECT => $correlid)); if (!$correlid) { echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}"; } else { echo '<font color="green"><b>OK</b></font> correlID=' . $correlid; } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?> <p/> <hr/> Creating 2nd connection - <?php $conn2 = new SAMConnection(); if ($conn2->connect($protocol, array(SAM_HOST => $host, SAM_PORT => $port, SAM_BROKER => $broker))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting receive using 2nd connection -<br/> <?php if ($conn2) { $msg = $conn2->receive($queuename, array(SAM_WAIT => 5000)); if ($msg) { echo '<font color="red"><b>FAILED</b></font> Oops, that message shouldn\'t have been accessible!'; } else {
<p/> <hr/> Attempting connection to valid host -<br> <?php $conn = new SAMConnection(); if ($conn->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a second connection to valid host -<br> <?php $conn = new SAMConnection(); if ($conn->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting send -<br> <?php if ($conn) { $msg = new SAMMessage('This is a test message'); $msg->header->SAM_REPLY_TO = 'queue://test/replyQueue'; $msg->header->SAM_TYPE = SAM_TEXT; $msg->header->myStringProp1 = array('a string property', SAM_STRING);
<body style="font-family: Arial;border: 0 none;"> <div id="content"> <table align="center" cellspacing="0" cellpadding="0"> <tr> <td> <div id="title">Tokudu Android Push Demo</div> </td> </tr> <tr> <td> <div class="status"> Server status: <?php require 'SAM/php_sam.php'; //create a new connection object $conn = new SAMConnection(); //start initialise the connection $result = $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1', SAM_PORT => 1883)); if ($result) { $conn->disconnect(); print_r("<span class='online'>Online</span>"); } else { print_r("<span class='offline'>Offline</span>"); } ?> </div </td> </tr> <tr> <td>
<p/> <hr/> Attempting connection to valid host -<br> <?php $conn1 = new SAMConnection(); if ($conn1->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target, SAM_WPM_DUR_SUB_HOME => $home))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a second connection to valid host -<br> <?php $conn = new SAMConnection(); if ($conn->connect(SAM_WPM, array(SAM_ENDPOINTS => $endpoints, SAM_BUS => $bus, SAM_TARGETCHAIN => $target, SAM_WPM_DUR_SUB_HOME => $home))) { echo '<font color="green"><b>OK</b></font>'; } else { echo '<font color="red"><b>FAILED</b></font>'; } ?> <p/> <hr/> Attempting a receive with an invalid subscription id -<br> <?php if ($conn) { $msg = $conn->receive('invalid sub name', array(SAM_WAIT => 5000)); if (!$msg) { echo '<font color="green"><b>OK</b></font> receive failed with errno=' . $conn->errno . ' ' . $conn->error . '<br>'; } else {
<body style="font-family: Arial;border: 0 none;"> <div id="content"> <table align="center" cellspacing="0" cellpadding="0"> <tr> <td> <div id="title">Tokudu Android Push Demo</div> </td> </tr> <tr> <td> <div class="status"> Server status: <?php require 'SAM/php_sam.php'; //create a new connection object $conn = new SAMConnection(); //start initialise the connection $result = $conn->connect(SAM_MQTT, array(SAM_HOST => 'test.mosquitto.org', SAM_PORT => 1883)); if ($result) { $conn->disconnect(); print_r("<span class='online'>Online</span>"); } else { print_r("<span class='offline'>Offline</span>"); } ?> </div </td> </tr> <tr> <td>