/[drupal]/contributions/modules/msgqueue/stomp_sender.inc
ViewVC logotype

Contents of /contributions/modules/msgqueue/stomp_sender.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 13 22:42:21 2007 UTC (23 months, 2 weeks ago) by larham
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
File MIME type: text/x-php
add v.0.8
1 <?php
2
3 // $Id:$
4
5 /**
6 * @file Send HTTP messages using the Stomp protocol, http://stomp.codehaus.org/Protocol
7 * @author Larry Hamel, Codeguild.com
8 */
9
10 /**
11 * A StompMsg is a simple message object
12 *
13 */
14 class StompMsg {
15 var $cmd;
16 var $headers;
17 var $txtmsg;
18
19 /**
20 * constructor
21 */
22 function StompMsg($cmd = NULL, $headers=NULL, $txtmsg=NULL) {
23 $this->cmd = $cmd;
24 $this->headers = $headers;
25 $this->txtmsg = $txtmsg;
26 }
27 }
28
29 /**
30 * Stomp Connection
31 *
32 * Keeps connection so that command, headers and txtmsg can be transmitted for
33 * any number of independent messages to the same destination.
34 *
35 */
36 class StompConn {
37
38 var $socket;
39
40 /**
41 * constructor connects to socket
42 */
43 function StompConn($host, $port = 61613) {
44 $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Cannot create socket\n");
45 $result = socket_connect($this->socket, $host, $port) or die("Cannot connect to server\n");
46 }
47
48 /**
49 * write a CONNECT message with credentials
50 * @return the result read from connection
51 */
52 function login($userName="", $password="") {
53 $this->_writeMsg( new StompMsg("CONNECT", array("login"=>$userName, "passcode"=> $password ) ) );
54 return $this->readMsg();
55 }
56
57 /**
58 * send a message that should be directed to the queue indicated by the $dest param
59 * @param dest the queue or other named channel
60 * @param txtmsg the payload
61 * @param properties an optional array of headers
62 */
63 function send($dest, $txtmsg, $properties=NULL) {
64 $headers = array();
65 if( isset($properties) ) {
66 foreach ($properties as $name => $value) {
67 $headers[$name] = $value;
68 }
69 }
70 $headers["destination"] = $dest ;
71 $this->_writeMsg( new StompMsg("SEND", $headers, $txtmsg) );
72 }
73
74 /**
75 * write out the given message to the socket
76 */
77 function _writeMsg($stompMsg) {
78 $data = $stompMsg->cmd . "\n";
79 if( isset($stompMsg->headers) ) {
80 foreach ($stompMsg->headers as $name => $value) {
81 $data .= $name . ": " . $value . "\n";
82 }
83 }
84 $data .= "\n";
85 if( isset($stompMsg->txtmsg) ) {
86 $data .= $stompMsg->txtmsg;
87 }
88 $l1 = strlen($data);
89 $data .= "\x00\n";
90 $l2 = strlen($data);
91
92 socket_write($this->socket, $data, strlen($data)) or die("Cannot send stomp message to server\n");
93 }
94
95 /**
96 * read message from connection
97 */
98 function readMsg() {
99 $buffer = "";
100 $in = socket_recv($this->socket, $buffer, 1, 0);
101
102 if( $in == 0 ) {
103 return NULL;
104 }
105
106 if( $in == false ) {
107 return NULL;
108 }
109
110 $instring = NULL;
111
112 while( ord($buffer) != 0 ) {
113
114 $instring .= $buffer;
115 $t = ord($buffer);
116
117 $in = socket_recv($this->socket,$buffer,1,0);
118
119 if( $in == 0 ) {
120 return NULL;
121 }
122
123 }
124
125 # get past the newline that follows the 0 delimitter
126 $in = socket_recv($this->socket,$buffer,1,0);
127 if( $in == 0 ) {
128 return NULL;
129 }
130 if( ord($buffer) != 10 ) {
131 return NULL;
132 }
133
134 list($header, $txtmsg) = explode("\n\n", $instring, 2);
135 $header = explode("\n", $header);
136 $allheaders = array();
137
138 $cmd = NULL;
139 foreach ($header as $oneheader) {
140 if( isset($cmd) ) {
141 list($name, $myval) = explode(':', $oneheader, 2);
142 $allheaders[$name]=$myval;
143 } else {
144 $cmd = $oneheader;
145 }
146 }
147
148 return new StompMsg($cmd, $allheaders, $txtmsg);
149 }
150 }
151
152

  ViewVC Help
Powered by ViewVC 1.1.2