php做推送服务端实现android消息推送

来源:互联网 发布:淘宝心等级查询 编辑:程序博客网 时间:2024/04/29 18:20

 

 

下载tokudu-PhpMQTTClient-ba4e494.zip

下载rsmb_1.2.0.zip(windows环境下要开\windows\broker.exe 切记切记

解压tokudu-PhpMQTTClient-ba4e494.zip

www下边新建目录androidpush

把解压文件tokudu-PhpMQTTClient-ba4e494中的SAM文件夹copy到androidpush目录下

把解压文件tokudu-PhpMQTTClient-ba4e494中的send_mqtt.php文件copy到androidpush目录下

结构如图:

send_mqtt.php主文件

[php] view plaincopyprint?
  1. <?php
  2. require('SAM/php_sam.php');
  3. //create a new connection object
  4. //创建一个新的连接对象
  5. $conn =new SAMConnection();
  6. //start initialise the connection
  7. //开始初始化连接 SAM_HOST服务器host路径 ;SAM_PORT服务器port端口号
  8. $conn->connect(SAM_MQTT,array(SAM_HOST =>'127.0.0.1',
  9. SAM_PORT => 1883));
  10. //create a new MQTT message with the output of the shell command as the body
  11. //建立一个新的MQTT shell命令的输出消息以作为主体($msgCpu是通知内容)
  12. //new SAMMessage() 参数一般写为json格式
  13. $msgCpu =new SAMMessage("测试通知");
  14. //send the message on the topic cpu
  15. //发送该信息的主体
  16. //860173018344139是设备号,每一台android手机对应一个唯一的设备号
  17. //$msgCpu通知内容
  18. //send()推送通知
  19. $conn->send('topic://'.'860173018344139',$msgCpu);
  20. //关闭连接
  21. $conn->disconnect();
  22. echo 'MQTT Message to ' .'860173018344139' .' sent: ' .'测试通知';
  23. ?>


SAM目录

SAM/MQTT/sam_mqtt.php

[php] view plaincopyprint?
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Copyright IBM Corporation 2006, 2007. |
  5. | All Rights Reserved. |
  6. +----------------------------------------------------------------------+
  7. | |
  8. | Licensed under the Apache License, Version 2.0 (the "License"); you |
  9. | may not use this file except in compliance with the License. You may |
  10. | obtain a copy of the License at |
  11. | http://www.apache.org/licenses/LICENSE-2.0 |
  12. | |
  13. | Unless required by applicable law or agreed to in writing, software |
  14. | distributed under the License is distributed on an "AS IS" BASIS, |
  15. | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  16. | implied. See the License for the specific language governing |
  17. | permissions and limitations under the License. |
  18. +----------------------------------------------------------------------+
  19. | Author: Dave Renshaw |
  20. +----------------------------------------------------------------------+
  21. $Id: sam_mqtt.php,v 1.1 2007/02/02 15:36:46 dsr Exp $
  22. */
  23. define("SAM_MQTT_CLEANSTART","SAM_MQTT_CLEANSTART");
  24. define("SAM_MQTT_QOS","SAM_MQTT_QOS");
  25. define("SAM_MQTT_SUB_SEPARATOR","#-#");
  26. /* ---------------------------------
  27. SAMConnection
  28. --------------------------------- */
  29. class SAMConnection_MQTT {
  30. var$debug = false;
  31. var$errno = 0;
  32. var$error ='';
  33. /*
  34. Info we need to keep between calls...
  35. */
  36. var$sub_id ='';
  37. var$port ='';
  38. var$host ='';
  39. var$cleanstart = false;
  40. var$virtualConnected = false;
  41. var$connected = false;
  42. /*
  43. Our current open socket...
  44. */
  45. var$sock;
  46. /*
  47. Table of available operations using the MQTT protocol...
  48. */
  49. var$operations =array("MQTT_CONNECT" => 1,
  50. "MQTT_CONNACK" => 2,
  51. "MQTT_PUBLISH" => 3,
  52. "MQTT_PUBACK" => 4,
  53. "MQTT_PUBREC" => 5,
  54. "MQTT_PUBREL" => 6,
  55. "MQTT_PUBCOMP" => 7,
  56. "MQTT_SUBSCRIBE" => 8,
  57. "MQTT_SUBACK" => 9,
  58. "MQTT_UNSUBSCRIBE" => 10,
  59. "MQTT_UNSUBACK" => 11,
  60. "MQTT_PINGREC" => 12,
  61. "MQTT_PINGRESP" => 13,
  62. "MQTT_DISCONNECT" => 14);
  63. /* ---------------------------------
  64. Constructor
  65. --------------------------------- */
  66. function SAMConnection_MQTT() {
  67. if ($this->debug) e('SAMConnection_MQTT()');
  68. if ($this->debug) x('SAMConnection_MQTT()');
  69. }
  70. /* ---------------------------------
  71. Commit
  72. --------------------------------- */
  73. function Commit() {
  74. if ($this->debug) e('SAMConnection_MQTT.Commit()');
  75. $errno = 100;
  76. $error ='Unsupported operation for MQTT protocol!';
  77. $rc = false;
  78. if ($this->debug) x("SAMConnection_MQTT.Commit() rc=$rc");
  79. return$rc;
  80. }
  81. /* ---------------------------------
  82. Connect
  83. --------------------------------- */
  84. function Connect($proto,$options=array()) {
  85. if ($this->debug) e('SAMConnection_MQTT.Connect()');
  86. /* Check our optional parameter array for the necessary bits... */
  87. if ($options[SAM_PORT] =='') {
  88. $this->port = 1883;
  89. } else {
  90. $this->port =$options[SAM_PORT];
  91. }
  92. if ($options[SAM_HOST] =='') {
  93. $this->host ='localhost';
  94. } else {
  95. $this->host =$options[SAM_HOST];
  96. }
  97. $this->cleanstart = in_array(SAM_MQTT_CLEANSTART,$options);
  98. if ($this->debug) t("SAMConnection_MQTT.Connect() host=$this->host, port=$this->port, cleanstart=$this->cleanstart");
  99. if ($this->checkHost($this->host,$this->port)) {
  100. $this->virtualConnected = true;
  101. } else {
  102. $this->virtualConnected = false;
  103. }
  104. if ($this->debug) x("SAMConnection_MQTT.Connect() rc=$this->virtualConnected");
  105. return$this->virtualConnected;
  106. }
  107. /* ---------------------------------
  108. Disconnect
  109. --------------------------------- */
  110. function Disconnect() {
  111. if ($this->debug) e('SAMConnection_MQTT.Disconnect()');
  112. $rc = false;
  113. if ($this->virtualConnected) {
  114. if ($this->connected) {
  115. $msg =$this->fixed_header("MQTT_DISCONNECT").pack('C', 0);
  116. fwrite($this->sock,$msg);
  117. $response =fgets($this->sock, 128);
  118. if ($this->debug) t('SAMConnection_MQTT.Disconnect() response is '.strlen($response).' bytes');
  119. if (strlen($response) == 0) {
  120. fclose($this->sock);
  121. $this->sock = NULL;
  122. }
  123. }
  124. $this->virtualConnected = false;
  125. $this->connected = false;
  126. $rc = true;
  127. }
  128. if ($this->debug) x("SAMConnection_MQTT.Disconnect() rc=$rc");
  129. return$rc;
  130. }
  131. /* ---------------------------------
  132. IsConnected
  133. --------------------------------- */
  134. function IsConnected() {
  135. if ($this->debug) e('SAMConnection_MQTT.IsConnected()');
  136. $rc = false;
  137. if ($this->connected) {
  138. $rc = true;
  139. }
  140. if ($this->debug) x("SAMConnection_MQTT.IsConnected() rc=$rc");
  141. return$rc;
  142. }
  143. /* ---------------------------------
  144. Peek
  145. --------------------------------- */
  146. function Peek() {
  147. if ($this->debug) e('SAMConnection_MQTT.Peek()');
  148. $errno = 100;
  149. $error ='Unsupported operation for MQTT protocol!';
  150. $rc = false;
  151. if ($this->debug) x("SAMConnection_MQTT.Peek() rc=$rc");
  152. return$rc;
  153. }
  154. /* ---------------------------------
  155. PeekAll
  156. --------------------------------- */
  157. function PeekAll() {
  158. if ($this->debug) e('SAMConnection_MQTT.PeekAll()');
  159. $errno = 100;
  160. $error ='Unsupported operation for MQTT protocol!';
  161. $rc = false;
  162. if ($this->debug) x("SAMConnection_MQTT.PeekAll() rc=$rc");
  163. return$rc;
  164. }
  165. /* ---------------------------------
  166. Receive
  167. --------------------------------- */
  168. function Receive($sub_id,$options=array()) {
  169. if ($this->debug) e('SAMConnection_MQTT.Receive()');
  170. $rc = false;
  171. /* strip the topic from the rear of the subscription id... */
  172. $x =strpos($sub_id, SAM_MQTT_SUB_SEPARATOR);
  173. if (!$x) {
  174. $this->errno = 279;
  175. $this->error ='Specified subscription id ('.$sub_id.') is not valid!';
  176. return false;
  177. }
  178. $topic =substr($sub_id,$x + strlen(SAM_MQTT_SUB_SEPARATOR));
  179. $si =substr($sub_id, 0,$x);
  180. /* Are we already connected? */
  181. if (!$this->connected) {
  182. if ($this->debug) t('SAMConnection_MQTT.Receive() Not connected.');
  183. /* No, so open up the connection... */
  184. $this->sub_id =$si;
  185. $rc =$this->do_connect_now();
  186. } else {
  187. /* We are already connected. Are we using the right subscriber id? */
  188. if ($this->sub_id !=$si) {
  189. if ($this->debug) t('SAMConnection_MQTT.Receive() Connected with wrong sub_id.');
  190. /* No, We better reconnect then... */
  191. $this->disconnect();
  192. $this->sub_id =$si;
  193. $rc =$this->do_connect_now();
  194. } else {
  195. if ($this->debug) t('SAMConnection_MQTT.Receive() Connected OK.');
  196. $rc = true;
  197. }
  198. }
  199. if ($rc) {
  200. /* have we got a timeout specified? */
  201. if ($options[SAM_WAIT] > 1) {
  202. $m =$options[SAM_WAIT] % 1000;
  203. $s = ($options[SAM_WAIT] -$m) /1000;
  204. if ($this->debug) t('SAMConnection_MQTT.Receive() timeout='.$options[SAM_WAIT]." ($s secs $m millisecs)");
  205. stream_set_timeout($this->sock,$s, $m);
  206. if ($this->debug) t('SAMConnection_MQTT.Receive() timeout set.');
  207. } else {
  208. if ($this->debug) t('SAMConnection_MQTT.Receive() no timeout value found!');
  209. }
  210. $hdr =$this->read_fixed_header($this->sock);
  211. if (!$hdr) {
  212. $this->errno = 500;
  213. $this->error ='Receive request failed, timed out with no data!';
  214. $rc = false;
  215. } else {
  216. if ($hdr['mtype'] == $this->operations['MQTT_PUBLISH']) {
  217. $len =$this->read_remaining_length($this->sock);
  218. if ($len > 1) {
  219. /* read the topic length... */
  220. $topic =$this->read_topic($this->sock);
  221. if (!$topic) {
  222. $this->errno = 303;
  223. $this->error ='Receive request failed, message format invalid!';
  224. $rc = false;
  225. } else {
  226. if ($this->debug) t('SAMConnection_MQTT.Receive() topic='.$topic);
  227. $len -= (strlen($topic) + 2);
  228. /* If QoS 1 or 2 then read the message id... */
  229. if ($hdr['qos'] > 0) {
  230. $idb =fread($this->sock, 2);
  231. $len -= 2;
  232. $fields = unpack('na',$idb);
  233. $mid =$fields['a'];
  234. if ($this->debug) t('SAMConnection_MQTT.Receive() mid='.$mid);
  235. }
  236. $payload =fread($this->sock,$len);
  237. if ($this->debug) t('SAMConnection_MQTT.Receive() payload='.$payload);
  238. $rc =new SAMMessage();
  239. $rc->body =$payload;
  240. $rc->header->SAM_MQTT_TOPIC ='topic://'.$topic;
  241. $rc->header->SAM_MQTT_QOS =$hdr['qos'];
  242. $rc->header->SAM_TYPE ='SAM_BYTES';
  243. }
  244. } else {
  245. $this->errno = 303;
  246. $this->error ='Receive request failed, received message too short! No topic data';
  247. $rc = false;
  248. }
  249. } else {
  250. if ($this->debug) t('SAMConnection_MQTT.Receive() Receive failed response mtype = '.$mtype);
  251. $rc = false;
  252. }
  253. }
  254. }
  255. if ($this->debug) x("SAMConnection_MQTT.Receive() rc=$rc");
  256. return$rc;
  257. }
  258. /* ---------------------------------
  259. Remove
  260. --------------------------------- */
  261. function Remove() {
  262. if ($this->debug) e('SAMConnection_MQTT.Remove()');
  263. $errno = 100;
  264. $error ='Unsupported operation for MQTT protocol!';
  265. $rc = false;
  266. if ($this->debug) x("SAMConnection_MQTT.Remove() rc=$rc");
  267. return$rc;
  268. }
  269. /* ---------------------------------
  270. Rollback
  271. --------------------------------- */
  272. function Rollback() {
  273. if ($this->debug) e('SAMConnection_MQTT.Rollback()');
  274. $errno = 100;
  275. $error ='Unsupported operation for MQTT protocol!';
  276. $rc = false;
  277. if ($this->debug) x("SAMConnection_MQTT.Rollback() rc=$rc");
  278. return$rc;
  279. }
  280. /* ---------------------------------
  281. Send
  282. --------------------------------- */
  283. function Send($topic,$message,$options=array()) {
  284. if ($this->debug) e('SAMConnection_MQTT.Send()');
  285. $rc = true;
  286. /* check the format of the topic... */
  287. if (strncmp($topic,'topic://', 8) == 0) {
  288. $t =substr($topic, 8);
  289. } else {
  290. $this->errno = 279;
  291. $this->error ='Specified target ('.$topic.') is not a valid topic!';
  292. return false;
  293. }
  294. if (in_array(SAM_MQTT_QOS,$options)) {
  295. $qos =$options[SAM_MQTT_QOS];
  296. } else {
  297. $qos = 0;
  298. }
  299. /* Are we already connected? */
  300. if (!$this->connected) {
  301. /* No, so open up the connection... */
  302. $this->do_connect_now();
  303. }
  304. $mid = rand();
  305. $variable =$this->utf($t);
  306. if ($qos > 0) {
  307. $variable .= pack('n',$mid);
  308. }
  309. $payload =$message->body;
  310. // add in the remaining length field and fix it together
  311. $msg =$this->fixed_header("MQTT_PUBLISH", 0,$qos) .$this->remaining_length(strlen($variable)+strlen($payload)) . $variable .$payload;
  312. fwrite($this->sock,$msg);
  313. if ($qos > 0) {
  314. $hdr =$this->read_fixed_header($this->sock);
  315. if ($hdr) {
  316. /* is this a QoS level 1 message being sent? */
  317. if ($qos == 1) {
  318. /* Yup, so we should get a PUBACK response message... */
  319. if ($hdr['mtype'] == $this->operations['MQTT_PUBACK']) {
  320. $len =$this->read_remaining_length($this->sock);
  321. if ($len > 0) {
  322. $response =fread($this->sock,$len);
  323. }
  324. if ($len < 2) {
  325. if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");
  326. $this->errno = 302;
  327. $this->error ='Send request failed!';
  328. $rc = false;
  329. } else {
  330. $rc = true;
  331. }
  332. } else {
  333. if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype.' Expected PUBREC!');
  334. $rc = false;
  335. }
  336. } else {
  337. /* lets assume it's QoS level 2... */
  338. /* We should get a PUBREC response message... */
  339. if ($hdr['mtype'] == $this->operations['MQTT_PUBREC']) {
  340. $len =$this->read_remaining_length($this->sock);
  341. if ($len > 0) {
  342. $response =fread($this->sock,$len);
  343. }
  344. if ($len < 2) {
  345. if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");
  346. $this->errno = 302;
  347. $this->error ='Send request failed!';
  348. $rc = false;
  349. } else {
  350. $rc = true;
  351. /* Now we can send a PUBREL message... */
  352. $variable = pack('n',$mid);
  353. $msg =$this->fixed_header("MQTT_PUBREL").$this->remaining_length(strlen($variable)).$variable;
  354. fwrite($this->sock,$msg);
  355. /* get a response... */
  356. $hdr =$this->read_fixed_header($this->sock);
  357. if ($hdr['mtype'] == $this->operations['MQTT_PUBCOMP']) {
  358. $len =$this->read_remaining_length($this->sock);
  359. if ($len > 0) {
  360. $response =fread($this->sock,$len);
  361. }
  362. if ($len < 2) {
  363. if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");
  364. $this->errno = 302;
  365. $this->error ='Send request failed!';
  366. $rc = false;
  367. } else {
  368. $rc = true;
  369. }
  370. } else {
  371. if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype.' Expected PUBCOMP!');
  372. $rc = false;
  373. }
  374. }
  375. } else {
  376. if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype);
  377. $rc = false;
  378. }
  379. }
  380. }
  381. }
  382. if ($this->debug) x("SAMConnection_MQTT.Send() rc=$rc");
  383. return$rc;
  384. }
  385. /* ---------------------------------
  386. SetDebug
  387. --------------------------------- */
  388. function SetDebug($option=false) {
  389. $this->debug =$option;
  390. return;
  391. }
  392. /* ---------------------------------
  393. Subscribe
  394. --------------------------------- */
  395. function Subscribe($topic,$options=array()) {
  396. if ($this->debug) e("SAMConnection_MQTT.Subscribe($topic)");
  397. $rc = true;
  398. /* check the format of the topic... */
  399. if (strncmp($topic,'topic://', 8) == 0) {
  400. $t =substr($topic, 8);
  401. } else {
  402. $this->errno = 279;
  403. $this->error ='Specified target ('.$topic.') is not a valid topic!';
  404. return false;
  405. }
  406. if (in_array(SAM_MQTT_QOS,$options)) {
  407. $qos =$options[SAM_MQTT_QOS];
  408. } else {
  409. $qos = 0;
  410. }
  411. /* Are we already connected? */
  412. if (!$this->connected) {
  413. /* No, so open up the connection... */
  414. if (!$this->do_connect_now()) {
  415. return false;
  416. }
  417. }
  418. // variable header: message id (16 bits)
  419. $x = rand(1, 16000);
  420. $variable = pack('n',$x);
  421. // payload: client ID
  422. $payload =$this->utf($t).pack('C',$qos);
  423. // add in the remaining length field and fix it together
  424. $msg =$this->fixed_header("MQTT_SUBSCRIBE", 0, 1) .$this->remaining_length(strlen($variable)+strlen($payload)) . $variable .$payload;
  425. fwrite($this->sock,$msg);
  426. $hdr =$this->read_fixed_header($this->sock);
  427. if (!$hdr) {
  428. if ($this->debug) t("SAMConnection_MQTT.Subscribe() subscribe failed, no response from broker!");
  429. $this->errno = 301;
  430. $this->error ='Subscribe request failed, no response from broker!';
  431. $rc = false;
  432. } else {
  433. if ($hdr['mtype'] == $this->operations['MQTT_SUBACK']) {
  434. $len =$this->read_remaining_length($this->sock);
  435. if ($len > 0) {
  436. $response =fread($this->sock,$len);
  437. /* Return the subscription id with the topic appended to it so we can unsubscribe easily... */
  438. $rc =$this->sub_id.SAM_MQTT_SUB_SEPARATOR.$t;
  439. }
  440. if ($len < 3) {
  441. if ($this->debug) t("SAMConnection_MQTT.Subscribe() subscribe failed, incorrect length response ($len) received!");
  442. $this->errno = 301;
  443. $this->error ='Subscribe request failed, incorrect length response ($len) received!';
  444. $rc = false;
  445. }
  446. } else {
  447. if ($this->debug) t('SAMConnection_MQTT.Subscribe() subscribe failed response mtype = '.$mtype);
  448. $rc = false;
  449. }
  450. }
  451. if ($this->debug) x("SAMConnection_MQTT.Subscribe() rc=$rc");
  452. return$rc;
  453. }
  454. /* ---------------------------------
  455. Unsubscribe
  456. --------------------------------- */
  457. function Unsubscribe($sub_id) {
  458. if ($this->debug) e("SAMConnection_MQTT.Unsubscribe($sub_id)");
  459. /* Detach the topic from the rear of the subscription id... */
  460. $x =strpos($sub_id, SAM_MQTT_SUB_SEPARATOR);
  461. if (!$x) {
  462. $this->errno = 279;
  463. $this->error ='Specified subscription id ('.$sub_id.') is not valid!';
  464. return false;
  465. }
  466. $topic =substr($sub_id,$x + strlen(SAM_MQTT_SUB_SEPARATOR));
  467. $si =substr($sub_id, 0,$x);
  468. /* Are we already connected? */
  469. if (!$this->connected) {
  470. if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Not connected.');
  471. /* No, so open up the connection... */
  472. $this->sub_id =$si;
  473. $rc =$this->do_connect_now();
  474. } else {
  475. /* We are already connected. Are we using the right subscriber id? */
  476. if ($this->sub_id !=$si) {
  477. if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Connected with wrong sub_id.');
  478. /* No, We better reconnect then... */
  479. $this->disconnect();
  480. $this->sub_id =$si;
  481. $rc =$this->do_connect_now();
  482. } else {
  483. if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Connected OK.');
  484. $rc = true;
  485. }
  486. }
  487. /* variable header: message id (16 bits) */
  488. $x = rand(1, 16000);
  489. $variable = pack('n',$x);
  490. /* payload: client ID */
  491. $payload =$this->utf($topic);
  492. /* add in the remaining length field and fix it together */
  493. $msg =$this->fixed_header("MQTT_UNSUBSCRIBE", 0, 1) .$this->remaining_length(strlen($variable)+strlen($payload)) . $variable .$payload;
  494. fwrite($this->sock,$msg);
  495. $hdr =$this->read_fixed_header($this->sock);
  496. if (!$hdr) {
  497. if ($this->debug) t("SAMConnection_MQTT.Unsubscribe() unsubscribe failed, no response from broker!");
  498. $this->errno = 302;
  499. $this->error ='Unsubscribe request failed, no response from broker!';
  500. $rc = false;
  501. } else {
  502. if ($hdr['mtype'] == $this->operations['MQTT_UNSUBACK']) {
  503. $len =$this->read_remaining_length($this->sock);
  504. if ($len > 0) {
  505. $response =fread($this->sock,$len);
  506. $rc =$this->sub_id;
  507. }
  508. if ($len != 2) {
  509. if ($this->debug) t("SAMConnection_MQTT.Unsubscribe() unsubscribe failed, incorrect length response ($len) received!");
  510. $this->errno = 301;
  511. $this->error ="Unsubscribe request failed, incorrect length response ($len) received!";
  512. $rc = false;
  513. }
  514. } else {
  515. if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() unsubscribe failed response mtype = '.$hdr['mtype']);
  516. $rc = false;
  517. }
  518. }
  519. if ($this->debug) x("SAMConnection_MQTT.Unsubscribe() rc=$rc");
  520. return$rc;
  521. }
  522. function remaining_length($l) {
  523. /* return the remaining length field bytes for an integer input parameter */
  524. if ($this->debug) t("SAMConnection_MQTT.remaining_length() l=$l");
  525. $rlf ='';
  526. do {
  527. $digit =$l % 128;
  528. $l = ($l -$digit)/128;
  529. if ($this->debug) t("SAMConnection_MQTT.remaining_length() digit=$digit l=$l");
  530. # if there are more digits to encode, set the top bit of this digit
  531. if ($l > 0 ) {
  532. $digit += 128;
  533. }
  534. $digit = pack('C',$digit);
  535. $rlf .=$digit;
  536. if ($this->debug) t("SAMConnection_MQTT.remaining_length() rlf=$rlf");
  537. } while ($l > 0);
  538. return$rlf;
  539. }
  540. function utf($s) {
  541. /* return the UTF-8 encoded version of the parameter */
  542. $l =strlen($s);
  543. $b1 = pack('C',$l/256);
  544. $b2 = pack('C',$l%256);
  545. $rc =$b1.$b2.$s;
  546. return$rc;
  547. }
  548. function fixed_header($operation,$dup=0,$qos=0,$retain=0) {
  549. /* fixed header: msg type (4) dup (1) qos (2) retain (1) */
  550. return pack('C', ($this->operations[$operation] * 16) + ($dup * 4) + ($qos * 2) +$retain);
  551. }
  552. function checkHost($hostname,$port) {
  553. if ($this->debug) e("SAMConnection_MQTT.checkHost($hostname)");
  554. $rc = false;
  555. $fp =fsockopen($hostname,$port);
  556. if (!$fp) {
  557. $rc = false;
  558. } else {
  559. $this->sock =$fp;
  560. $rc = true;
  561. }
  562. if ($this->debug) x("SAMConnection_MQTT.checkHost(rc=$rc)");
  563. return$rc;
  564. }
  565. function do_connect_now() {
  566. $rc = true;
  567. /* Do we have a client/subscriber id yet? */
  568. if ($this->sub_id =='') {
  569. /* No, so create a unique one... */
  570. $this->sub_id = uniqid('', true);
  571. if ($this->debug) t("SAMConnection_MQTT.do_connect_now() sub_id=$this->sub_id");
  572. } else {
  573. if ($this->debug) t("SAMConnection_MQTT.do_connect_now() using existing sub_id=$this->sub_id");
  574. }
  575. if ($this->cleanstart) {
  576. $x ="\x03";
  577. } else {
  578. $x ="\x00";
  579. }
  580. $variable =$this->utf('MQIsdp')."\x03$x\x00\x00";
  581. /* payload is subscriber id */
  582. $payload =$this->utf($this->sub_id);
  583. /* add in the remaining length field and fix it together */
  584. $msg =$this->fixed_header("MQTT_CONNECT") .$this->remaining_length(strlen($variable)+strlen($payload)) . $variable .$payload;
  585. $errno = 0;
  586. $errstr ='';
  587. if (!$this->virtualConnected) {
  588. $fp =fsockopen($this->host,$this->port,$errno,$errstr);
  589. if (!$fp) {
  590. if ($this->debug) t("SAMConnection_MQTT.do_connect_now() fsockopen failed! ($errno) $errstr");
  591. $this->errno = 208;
  592. $this->error ='Unable to open socket to broker!';
  593. $this->sock = NULL;
  594. return false;
  595. } else {
  596. $this->virtualConnected = true;
  597. $this->sock =$fp;
  598. }
  599. }
  600. stream_set_timeout($this->sock, 10);
  601. fwrite($this->sock,$msg);
  602. $hdr =$this->read_fixed_header($this->sock);
  603. if ($hdr) {
  604. if ($hdr['mtype'] == $this->operations['MQTT_CONNACK']) {
  605. $len =$this->read_remaining_length($this->sock);
  606. if ($len < 2) {
  607. if ($this->debug) t("SAMConnection_MQTT.do_connect_now() connect failed, incorrect length response ($len) received!");
  608. $this->errno = 218;
  609. $this->error ='Unable to open connection to broker!';
  610. $rc = false;
  611. } else {
  612. $response =fread($this->sock,$len);
  613. $fields = unpack('Ccomp/Cretcode',$response);
  614. if ($fields['retcode'] == 0) {
  615. $rc =$this->sock;
  616. $this->connected = true;
  617. $rc = true;
  618. if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connected OK');
  619. } else {
  620. if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connect failed retcode = '.$fields['retcode']);
  621. $rc = false;
  622. if ($fields['retcode'] == 2) {
  623. $this->sub_id ='';
  624. $this->errno = 279;
  625. $this->error ='Invalid subscription id!';
  626. }
  627. }
  628. }
  629. } else {
  630. if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connect failed response mtype = '.$mtype);
  631. $rc = false;
  632. }
  633. }
  634. if (!$rc) {
  635. fclose($this->sock);
  636. $this->sock = NULL;
  637. $this->virtualConnected = false;
  638. }
  639. return$rc;
  640. }
  641. function read_fixed_header($conn) {
  642. $rc = false;
  643. $response =fread($conn, 1);
  644. if (strlen($response) > 0) {
  645. $fields = unpack('Cbyte1',$response);
  646. $x =$fields['byte1'];
  647. $ret =$x % 2;
  648. $x -=$ret;
  649. $qos = ($x % 8) / 2;
  650. $x -= ($qos * 2);
  651. $dup = ($x % 16) / 8;
  652. $x -= ($dup * 8);
  653. $mtype =$x / 16;
  654. if ($this->debug) t("SAMConnection_MQTT.read_fixed_header() mtype=$mtype, dup=$dup, qos=$qos, retain=$ret");
  655. $rc =array('mtype' =>$mtype,'dup' =>$dup, 'qos' => $qos, 'retain' =>$ret);
  656. }
  657. return$rc;
  658. }
  659. function read_remaining_length($conn) {
  660. $rc = 0;
  661. $m = 1;
  662. while (!feof($conn)) {
  663. $byte =fgetc($conn);
  664. $fields = unpack('Ca',$byte);
  665. $x =$fields['a'];
  666. if ($this->debug) t('SAMConnection_MQTT.read_remaining_length() byte ('.strlen($byte).') = '.$x);
  667. if ($x < 128) {
  668. $rc +=$x * $m;
  669. break;
  670. } else {
  671. $rc += (($x - 128) *$m);
  672. }
  673. $m *= 128;
  674. }
  675. if ($this->debug) t('SAMConnection_MQTT.read_remaining_length() remaining length = '.$rc);
  676. return$rc;
  677. }
  678. function read_topic($conn) {
  679. if ($this->debug) e('SAMConnection_MQTT.read_topic()');
  680. $rc = false;
  681. while (!feof($conn)) {
  682. $tlen =fread($conn, 2);
  683. $fields = unpack('na',$tlen);
  684. if ($this->debug) t('SAMConnection_MQTT.read_topic() topic length='.$fields['a']);
  685. $rc =fread($conn,$fields['a']);
  686. break;
  687. }
  688. if ($this->debug) x("SAMConnection_MQTT.read_topic(rc=$rc)");
  689. return$rc;
  690. }
  691. }
  692. ?>


SAM/php_sam.php

[php] view plaincopyprint?
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Copyright IBM Corporation 2006, 2007. |
  5. | All Rights Reserved. |
  6. +----------------------------------------------------------------------+
  7. | |
  8. | Licensed under the Apache License, Version 2.0 (the "License"); you |
  9. | may not use this file except in compliance with the License. You may |
  10. | obtain a copy of the License at |
  11. | http://www.apache.org/licenses/LICENSE-2.0 |
  12. | |
  13. | Unless required by applicable law or agreed to in writing, software |
  14. | distributed under the License is distributed on an "AS IS" BASIS, |
  15. | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  16. | implied. See the License for the specific language governing |
  17. | permissions and limitations under the License. |
  18. +----------------------------------------------------------------------+
  19. | Author: Dave Renshaw |
  20. +----------------------------------------------------------------------+
  21. $Id: php_sam.php,v 1.1 2007/02/02 15:38:53 dsr Exp $
  22. */
  23. /* Debugging flags and functions available to sub packages... */
  24. $eol ="\n";
  25. if (isset($_SERVER['REQUEST_URI'])) {
  26. $eol ='<br>';
  27. }
  28. function e($s) {global$eol;echo'-->'.$s."$eol";}
  29. function t($s) {global$eol;echo' '.$s."$eol";}
  30. function x($s) {global$eol;echo'<--'.$s."$eol";}
  31. define('SAM_MQTT','mqtt');
  32. /* ---------------------------------
  33. SAMConnection
  34. --------------------------------- */
  35. class SAMConnection {
  36. // var $debug = true;
  37. var$debug = false;
  38. var$errno = 0;
  39. var$error ='';
  40. var$connection;
  41. /* ---------------------------------
  42. Create
  43. --------------------------------- */
  44. function Create($proto) {
  45. if ($this->debug) e("SAMConnection.Create(proto=$proto)");
  46. $rc = false;
  47. /* search the PHP config for a factory to use... */
  48. $x = get_cfg_var('sam.factory.'.$proto);
  49. if ($this->debug) t('SAMConnection.Create() get_cfg_var() "'.$x.'"');
  50. /* If there is no configuration (php.ini) entry for this protocol, default it. */
  51. if (strlen($x) == 0) {
  52. /* for every protocol other than MQTT assume we will use XMS */
  53. if ($proto !='mqtt') {
  54. $x ='xms';
  55. } else {
  56. $x ='mqtt';
  57. }
  58. }
  59. /* Invoke the chosen factory to create a real connection object... */
  60. $x ='sam_factory_'.$x.'.php';
  61. if ($this->debug) t("SAMConnection.Create() calling factory - $x");
  62. $rc =include$x;
  63. if ($this->debug &&$rc) t('SAMConnection.Create() rc = '.get_class($rc));
  64. if ($this->debug) x('SAMConnection.Create()');
  65. return$rc;
  66. }
  67. /* ---------------------------------
  68. Constructor
  69. --------------------------------- */
  70. function SAMConnection() {
  71. if ($this->debug) e('SAMConnection()');
  72. if ($this->debug) x('SAMConnection()');
  73. }
  74. /* ---------------------------------
  75. Commit
  76. --------------------------------- */
  77. function Commit() {
  78. if ($this->debug) e('SAMConnection.Commit()');
  79. $rc = true;
  80. if (!$this->connection) {
  81. $errno = 106;
  82. $error ='No active connection!';
  83. $rc = false;
  84. } else {
  85. /* Call the method on the underlying connection object... */
  86. $rc =$this->connection->commit($target,$options);
  87. $this->errno =$this->connection->errno;
  88. $this->error =$this->connection->error;
  89. if (!$rc) {
  90. if ($this->debug) t("SAMConnection.Commit() commit failed ($this->errno) $this->error");
  91. $rc = false;
  92. }
  93. }
  94. if ($this->debug) x("SAMConnection.Commit() rc=$rc");
  95. return$rc;
  96. }
  97. /* ---------------------------------
  98. Connect
  99. --------------------------------- */
  100. function Connect($proto='',$options=array()) {
  101. if ($this->debug) e('SAMConnection.Connect()');
  102. $rc = false;
  103. if ($proto =='') {
  104. $errno = 101;
  105. $error ='Incorrect number of parameters on connect call!';
  106. $rc = false;
  107. } else {
  108. $this->connection =$this->create($proto);
  109. if (!$this->connection) {
  110. $errno = 102;
  111. $error ='Unsupported protocol!';
  112. $rc = false;
  113. } else {
  114. if ($this->debug) t("SAMConnection.Connect() connection created for protocol $proto");
  115. $this->connection->setdebug($this->debug);
  116. /* Call the connect method on the newly created connection object... */
  117. $rc =$this->connection->connect($proto,$options);
  118. $this->errno =$this->connection->errno;
  119. $this->error =$this->connection->error;
  120. if (!$rc) {
  121. if ($this->debug) t("SAMConnection.Connect() connect failed ($this->errno) $this->error");
  122. } else {
  123. $rc = true;
  124. }
  125. }
  126. }
  127. if ($this->debug) x("SAMConnection.Connect() rc=$rc");
  128. return$rc;
  129. }
  130. /* ---------------------------------
  131. Disconnect
  132. --------------------------------- */
  133. function Disconnect() {
  134. if ($this->debug) e('SAMConnection.Disconnect()');
  135. $rc = true;
  136. if (!$this->connection) {
  137. $errno = 106;
  138. $error ='No active connection!';
  139. $rc = false;
  140. } else {
  141. /* Call the method on the underlying connection object... */
  142. $rc =$this->connection->Disconnect();
  143. $this->errno =$this->connection->errno;
  144. $this->error =$this->connection->error;
  145. if (!$rc) {
  146. if ($this->debug) t("SAMConnection.Disconnect() Disconnect failed ($this->errno) $this->error");
  147. } else {
  148. $rc = true;
  149. $this->connection = false;
  150. }
  151. }
  152. if ($this->debug) x("SAMConnection.Disconnect() rc=$rc");
  153. return$rc;
  154. }
  155. /* ---------------------------------
  156. IsConnected
  157. --------------------------------- */
  158. function IsConnected() {
  159. if ($this->debug) e('SAMConnection.IsConnected()');
  160. $rc = true;
  161. if (!$this->connection) {
  162. $errno = 106;
  163. $error ='No active connection!';
  164. $rc = false;
  165. } else {
  166. /* Call the method on the underlying connection object... */
  167. $rc =$this->connection->isconnected();
  168. $this->errno =$this->connection->errno;
  169. $this->error =$this->connection->error;
  170. if (!$rc) {
  171. if ($this->debug) t("SAMConnection.IsConnected() isconnected failed ($this->errno) $this->error");
  172. $rc = false;
  173. }
  174. }
  175. if ($this->debug) x("SAMConnection.IsConnected() rc=$rc");
  176. return$rc;
  177. }
  178. /* ---------------------------------
  179. Peek
  180. --------------------------------- */
  181. function Peek($target,$options=array()) {
  182. if ($this->debug) e('SAMConnection.Peek()');
  183. $rc = true;
  184. if (!$this->connection) {
  185. $errno = 106;
  186. $error ='No active connection!';
  187. $rc = false;
  188. } else {
  189. /* Call the method on the underlying connection object... */
  190. $rc =$this->connection->peek($target,$options);
  191. $this->errno =$this->connection->errno;
  192. $this->error =$this->connection->error;
  193. if (!$rc) {
  194. if ($this->debug) t("SAMConnection.Peek() peek failed ($this->errno) $this->error");
  195. $rc = false;
  196. }
  197. }
  198. if ($this->debug) x("SAMConnection.Peek() rc=$rc");
  199. return$rc;
  200. }
  201. /* ---------------------------------
  202. PeekAll
  203. --------------------------------- */
  204. function PeekAll($target,$options=array()) {
  205. if ($this->debug) e('SAMConnection.PeekAll()');
  206. $rc = true;
  207. if (!$this->connection) {
  208. $errno = 106;
  209. $error ='No active connection!';
  210. $rc = false;
  211. } else {
  212. /* Call the method on the underlying connection object... */
  213. $rc =$this->connection->peekall($target,$options);
  214. $this->errno =$this->connection->errno;
  215. $this->error =$this->connection->error;
  216. if (!$rc) {
  217. if ($this->debug) t("SAMConnection.PeekAll() peekall failed ($this->errno) $this->error");
  218. $rc = false;
  219. }
  220. }
  221. if ($this->debug) x("SAMConnection.PeekAll() rc=$rc");
  222. return$rc;
  223. }
  224. /* ---------------------------------
  225. Receive
  226. --------------------------------- */
  227. function Receive($target,$options=array()) {
  228. if ($this->debug) e('SAMConnection.Receive()');
  229. $rc = true;
  230. if (!$this->connection) {
  231. $errno = 106;
  232. $error ='No active connection!';
  233. $rc = false;
  234. } else {
  235. /* Call the receive method on the underlying connection object... */
  236. $rc =$this->connection->receive($target,$options);
  237. $this->errno =$this->connection->errno;
  238. $this->error =$this->connection->error;
  239. if (!$rc) {
  240. if ($this->debug) t("SAMConnection.Receive() receive failed ($this->errno) $this->error");
  241. }
  242. }
  243. if ($this->debug) x("SAMConnection.Receive() rc=$rc");
  244. return$rc;
  245. }
  246. /* ---------------------------------
  247. Remove
  248. --------------------------------- */
  249. function Remove($target,$options=array()) {
  250. if ($this->debug) e('SAMConnection.Remove()');
  251. $rc = true;
  252. if (!$this->connection) {
  253. $errno = 106;
  254. $error ='No active connection!';
  255. $rc = false;
  256. } else {
  257. /* Call the method on the underlying connection object... */
  258. $rc =$this->connection->remove($target,$options);
  259. $this->errno =$this->connection->errno;
  260. $this->error =$this->connection->error;
  261. if (!$rc) {
  262. if ($this->debug) t("SAMConnection.Remove() remove failed ($this->errno) $this->error");
  263. $rc = false;
  264. }
  265. }
  266. if ($this->debug) x("SAMConnection.Remove() rc=$rc");
  267. return$rc;
  268. }
  269. /* ---------------------------------
  270. Rollback
  271. --------------------------------- */
  272. function Rollback() {
  273. if ($this->debug) e('SAMConnection.Rollback()');
  274. $rc = true;
  275. if (!$this->connection) {
  276. $errno = 106;
  277. $error ='No active connection!';
  278. $rc = false;
  279. } else {
  280. /* Call the method on the underlying connection object... */
  281. $rc =$this->connection->rollback($target,$options);
  282. $this->errno =$this->connection->errno;
  283. $this->error =$this->connection->error;
  284. if (!$rc) {
  285. if ($this->debug) t("SAMConnection.Rollback() rollback failed ($this->errno) $this->error");
  286. $rc = false;
  287. }
  288. }
  289. if ($this->debug) x("SAMConnection.Rollback() rc=$rc");
  290. return$rc;
  291. }
  292. /* ---------------------------------
  293. Send
  294. --------------------------------- */
  295. function Send($target,$msg, $options=array()) {
  296. if ($this->debug) e('SAMConnection.Send()');
  297. $rc = true;
  298. if (!$this->connection) {
  299. $errno = 106;
  300. $error ='No active connection!';
  301. $rc = false;
  302. } else {
  303. /* Call the send method on the underlying connection object... */
  304. $rc =$this->connection->send($target,$msg, $options);
  305. $this->errno =$this->connection->errno;
  306. $this->error =$this->connection->error;
  307. if (!$rc) {
  308. if ($this->debug) t("SAMConnection.Send() send failed ($this->errno) $this->error");
  309. $rc = false;
  310. }
  311. }
  312. if ($this->debug) x("SAMConnection.Send() rc=$rc");
  313. return$rc;
  314. }
  315. /* ---------------------------------
  316. SetDebug
  317. --------------------------------- */
  318. function SetDebug($option=false) {
  319. if ($this->debug) e("SAMConnection.SetDebug($option)");
  320. $this->debug =$option;
  321. if ($this->connection) {
  322. $this->connection->setdebug($option);
  323. }
  324. if ($this->debug) x('SAMConnection.SetDebug()');
  325. return;
  326. }
  327. /* ---------------------------------
  328. Subscribe
  329. --------------------------------- */
  330. function Subscribe($topic,$options=array()) {
  331. if ($this->debug) e("SAMConnection.Subscribe($topic)");
  332. $rc = true;
  333. if (!$this->connection) {
  334. $errno = 106;
  335. $error ='No active connection!';
  336. $rc = false;
  337. } else {
  338. /* Call the subscribe method on the underlying connection object... */
  339. $rc =$this->connection->subscribe($topic,$options);
  340. $this->errno =$this->connection->errno;
  341. $this->error =$this->connection->error;
  342. if (!$rc) {
  343. if ($this->debug) t("SAMConnection.Subscribe() subscribe failed ($this->errno) $this->error");
  344. $rc = false;
  345. }
  346. }
  347. if ($this->debug) x("SAMConnection.Subscribe() rc=$rc");
  348. return$rc;
  349. }
  350. /* ---------------------------------
  351. Unsubscribe
  352. --------------------------------- */
  353. function Unsubscribe($sub_id) {
  354. if ($this->debug) e("SAMConnection.Unsubscribe($sub_id)");
  355. $rc = true;
  356. if (!$this->connection) {
  357. $errno = 106;
  358. $error ='No active connection!';
  359. $rc = false;
  360. } else {
  361. /* Call the subscribe method on the underlying connection object... */
  362. $rc =$this->connection->unsubscribe($sub_id);
  363. $this->errno =$this->connection->errno;
  364. $this->error =$this->connection->error;
  365. if (!$rc) {
  366. if ($this->debug) t("SAMConnection.Unsubscribe() unsubscribe failed ($this->errno) $this->error");
  367. $rc = false;
  368. }
  369. }
  370. if ($this->debug) x("SAMConnection.Unsubscribe() rc=$rc");
  371. return$rc;
  372. }
  373. }
  374. /* ---------------------------------
  375. SAMMessage
  376. --------------------------------- */
  377. class SAMMessage {
  378. /* ---------------------------------
  379. Constructor
  380. --------------------------------- */
  381. function SAMMessage($body='') {
  382. if ($body !='') {
  383. $this->body =$body;
  384. }
  385. }
  386. }
  387. ?>


SAM/sam_factory_mqtt.php

[php] view plaincopyprint?
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Copyright IBM Corporation 2006, 2007. |
  5. | All Rights Reserved. |
  6. +----------------------------------------------------------------------+
  7. | |
  8. | Licensed under the Apache License, Version 2.0 (the "License"); you |
  9. | may not use this file except in compliance with the License. You may |
  10. | obtain a copy of the License at |
  11. | http://www.apache.org/licenses/LICENSE-2.0 |
  12. | |
  13. | Unless required by applicable law or agreed to in writing, software |
  14. | distributed under the License is distributed on an "AS IS" BASIS, |
  15. | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  16. | implied. See the License for the specific language governing |
  17. | permissions and limitations under the License. |
  18. +----------------------------------------------------------------------+
  19. | Author: Dave Renshaw |
  20. +----------------------------------------------------------------------+
  21. $Id: sam_factory_mqtt.php,v 1.1 2007/02/02 15:40:41 dsr Exp $
  22. */
  23. require_once('MQTT/sam_mqtt.php');
  24. returnnew SAMConnection_MQTT();
  25. ?>


SAM/sam_factory_xms.php

[php] view plaincopyprint?
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Copyright IBM Corporation 2006, 2007. |
  5. | All Rights Reserved. |
  6. +----------------------------------------------------------------------+
  7. | |
  8. | Licensed under the Apache License, Version 2.0 (the "License"); you |
  9. | may not use this file except in compliance with the License. You may |
  10. | obtain a copy of the License at |
  11. | http://www.apache.org/licenses/LICENSE-2.0 |
  12. | |
  13. | Unless required by applicable law or agreed to in writing, software |
  14. | distributed under the License is distributed on an "AS IS" BASIS, |
  15. | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  16. | implied. See the License for the specific language governing |
  17. | permissions and limitations under the License. |
  18. +----------------------------------------------------------------------+
  19. | Author: Dave Renshaw |
  20. +----------------------------------------------------------------------+
  21. $Id: sam_factory_xms.php,v 1.1 2007/02/02 15:40:00 dsr Exp $
  22. */
  23. if (!class_exists('SAMXMSConnection')) {
  24. global$eol;
  25. $l = (strstr(PHP_OS,'WIN') ?'php_' :'').'sam_xms.'.(strstr(PHP_OS,'WIN') ?'dll' :'so');
  26. echo $eol.'<font color="red"><b>Unable to access SAM XMS capabilities. Ensure the '.$l.' library is defined as an extension.</b></font>'.$eol;
  27. return false;
  28. } else {
  29. returnnew SAMXMSConnection();
  30. }
  31. ?>
0 0
原创粉丝点击