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) // { $ch = curl_init(); $data4 = explode(',', $msgUp->body); curl_setopt($ch, CURLOPT_URL, "http://localhost/mythings/system/controller/carcheck_process.php"); curl_setopt($ch, CURLOPT_POST, 1);
$correlid2 = $conn->send($queuename, $msg, array(SAM_CORRELID => $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/> Attempting receive with correlation id -<br/> <?php if ($conn) { $msg = $conn->receive($queuename, array(SAM_CORRELID => $correlid, SAM_WAIT => 5000)); if ($msg) { foreach ($msg->header as $key => $value) { echo 'property <b>' . $key . '</b> set to <b>' . $value . '</b><br/>'; } } echo 'Retrieving property by name(JMS_IBM_PutDate) value is -' . $msg->header->JMS_IBM_PutDate . '-<br/>'; if (!$msg) { echo '<font color="red"><b>FAILED</b></font>' . " ({$conn->errno}) {$conn->error}"; } else { echo '<font color="green"><b>OK</b></font> ' . "message={$msg->body}"; } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?>
<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 { echo '<font color="red"><b>FAILED</b></font> Should not have received a message!'; } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?> <p/> <hr/> Attempting subscribe -<br> <?php if ($conn) { $sub = $conn->subscribe($queuename);
<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 { if ($conn2->errno == SAM_RECEIVE_TIMEDOUT) { echo '<font color="green"><b>OK</b></font>' . " ({$conn2->errno}) {$conn2->error}"; } else { echo '<font color="red"><b>FAILED</b></font> XMS receive failure! ' . " ({$conn2->errno}) {$conn2->error}"; } } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?> <p/> <hr/>
<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 { echo '<font color="red"><b>FAILED</b></font> Should not have received a message!'; } } else { echo '<font color="red"><b>FAILED</b></font> previous connection create test failed!'; } ?> <p/> <hr/> Attempting subscribe -<br> <?php if ($conn) { $sub = $conn->subscribe($queuename);