You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ch...@apache.org on 2006/04/19 20:08:15 UTC

svn commit: r395331 - /incubator/activemq/trunk/stomp/php/Stomp.php

Author: chirino
Date: Wed Apr 19 11:08:15 2006
New Revision: 395331

URL: http://svn.apache.org/viewcvs?rev=395331&view=rev
Log:
Add \n after the frame terminator \0

Modified:
    incubator/activemq/trunk/stomp/php/Stomp.php

Modified: incubator/activemq/trunk/stomp/php/Stomp.php
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/stomp/php/Stomp.php?rev=395331&r1=395330&r2=395331&view=diff
==============================================================================
--- incubator/activemq/trunk/stomp/php/Stomp.php (original)
+++ incubator/activemq/trunk/stomp/php/Stomp.php Wed Apr 19 11:08:15 2006
@@ -26,15 +26,15 @@
  * @version $Revision$
  */
 class StompFrame {
-	var $command;
-	var $headers;
-	var $body;
-	
-	function StompFrame($command = null, $headers=null, $body=null) {
-		$this->command = $command;
-		$this->headers = $headers;
-		$this->body = $body;
-	}
+    var $command;
+    var $headers;
+    var $body;
+    
+    function StompFrame($command = null, $headers=null, $body=null) {
+        $this->command = $command;
+        $this->headers = $headers;
+        $this->body = $body;
+    }
 }
 
 /**
@@ -49,152 +49,161 @@
  */
 class StompConnection {
 
-	var $socket;
+    var $socket;
 
-	function StompConnection($host, $port = 61613) {
-		$this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
-		$result = socket_connect($this->socket, $host, $port) or die("Could not connect to server\n");
-	}
-
-
-	function connect($userName="", $password="") {
-		$this->writeFrame( new StompFrame("CONNECT", array("login"=>$userName, "passcode"=> $password ) ) );
-		return $this->readFrame();
-	}
-
-	function send($destination, $body, $properties=null) {
-		$headers = array();
-		if( isset($properties) ) {
-			foreach ($properties as $name => $value) {
-	            $headers[$name] = $value;
-			}
-		}
-		$headers["destination"] = $destination ;
-		$this->writeFrame( new StompFrame("SEND", $headers, $body) );
-	}
-	
-	function subscribe($destination, $properties=null) {
-		$headers = array("ack"=>"client");
-		if( isset($properties) ) {
-			foreach ($properties as $name => $value) {
-	            $headers[$name] = $value;
-			}
-		}
-		$headers["destination"] = $destination ;
-		$this->writeFrame( new StompFrame("SUBSCRIBE", $headers) );
-	}
-	
-	function unsubscribe($destination, $properties=null) {
-		$headers = array();
-		if( isset($properties) ) {
-			foreach ($properties as $name => $value) {
-	            $headers[$name] = $value;
-			}
-		}
-		$headers["destination"] = $destination ;
-		$this->writeFrame( new StompFrame("UNSUBSCRIBE", $headers) );
-	}
-
-	function begin($transactionId=null) {
-		$headers = array();
-		if( isset($transactionId) ) {
+    function StompConnection($host, $port = 61613) {
+        $this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
+        $result = socket_connect($this->socket, $host, $port) or die("Could not connect to server\n");
+    }
+
+
+    function connect($userName="", $password="") {
+        $this->writeFrame( new StompFrame("CONNECT", array("login"=>$userName, "passcode"=> $password ) ) );
+        return $this->readFrame();
+    }
+
+    function send($destination, $body, $properties=null) {
+        $headers = array();
+        if( isset($properties) ) {
+            foreach ($properties as $name => $value) {
+                $headers[$name] = $value;
+            }
+        }
+        $headers["destination"] = $destination ;
+        $this->writeFrame( new StompFrame("SEND", $headers, $body) );
+    }
+    
+    function subscribe($destination, $properties=null) {
+        $headers = array("ack"=>"client");
+        if( isset($properties) ) {
+            foreach ($properties as $name => $value) {
+                $headers[$name] = $value;
+            }
+        }
+        $headers["destination"] = $destination ;
+        $this->writeFrame( new StompFrame("SUBSCRIBE", $headers) );
+    }
+    
+    function unsubscribe($destination, $properties=null) {
+        $headers = array();
+        if( isset($properties) ) {
+            foreach ($properties as $name => $value) {
+                $headers[$name] = $value;
+            }
+        }
+        $headers["destination"] = $destination ;
+        $this->writeFrame( new StompFrame("UNSUBSCRIBE", $headers) );
+    }
+
+    function begin($transactionId=null) {
+        $headers = array();
+        if( isset($transactionId) ) {
             $headers["transaction"] = $transactionId;
-		}
-		$this->writeFrame( new StompFrame("BEGIN", $headers) );
-	}
-	
-	function commit($transactionId=null) {
-		$headers = array();
-		if( isset($transactionId) ) {
+        }
+        $this->writeFrame( new StompFrame("BEGIN", $headers) );
+    }
+    
+    function commit($transactionId=null) {
+        $headers = array();
+        if( isset($transactionId) ) {
             $headers["transaction"] = $transactionId;
-		}
-		$this->writeFrame( new StompFrame("COMMIT", $headers) );
-	}
-
-	function abort($transactionId=null) {
-		$headers = array();
-		if( isset($transactionId) ) {
+        }
+        $this->writeFrame( new StompFrame("COMMIT", $headers) );
+    }
+
+    function abort($transactionId=null) {
+        $headers = array();
+        if( isset($transactionId) ) {
             $headers["transaction"] = $transactionId;
-		}
-		$this->writeFrame( new StompFrame("ABORT", $headers) );
-	}
-	
-	function acknowledge($messageId, $transactionId=null) {
-		$headers = array();
-		if( isset($transactionId) ) {
+        }
+        $this->writeFrame( new StompFrame("ABORT", $headers) );
+    }
+    
+    function acknowledge($messageId, $transactionId=null) {
+        $headers = array();
+        if( isset($transactionId) ) {
             $headers["transaction"] = $transactionId;
-		}
-		$headers["message-id"] = $messageId ;
-		$this->writeFrame( new StompFrame("ABORT", $headers) );
-	}
-	
-	function disconnect() {
-		$this->writeFrame( new StompFrame("DISCONNECT") );
-		socket_close($this->socket);
-	}
-	
-	function writeFrame($stompFrame) {
-		$data = $stompFrame->command . "\n";		
-		if( isset($stompFrame->headers) ) {
-			foreach ($stompFrame->headers as $name => $value) {
-	            $data .= $name . ": " . $value . "\n";
-			}
-		}
-		$data .= "\n";
-		if( isset($stompFrame->body) ) {
-			$data .= $stompFrame->body;
-		}
-		$l1 = strlen($data);
-		$data .= "\x00";
-		$l2 = strlen($data);
-		
-		socket_write($this->socket, $data, strlen($data)) or die("Could not send stomp frame to server\n");
-	}
-	
-	function readFrame() {
-
-		$rc = socket_recv($this->socket, &$b, 1, 0);
-		
-		// I think this EOF
-		if( $rc == 0 ) {
-			return null;
-		}
-		// I think this is no data.
-		if( $rc == false ) {
-			return null;
-		}		
-		
-		// Read until end of frame.
-		while( ord($b) != 0  ) {
-
-			$data .= $b;
-			$t = ord($b);
-								
-			$rc = socket_recv($this->socket,&$b,1,0);
-			
-			// I think this EOF
-			if( $rc == 0 ) {
-				return null;
-			}
-			
-		}
-
-		list($header, $body) = explode("\n\n", $data, 2);
-		$header = explode("\n", $header);
-		$headers = array();
-		
-		$command = null;
-		foreach ($header as $v) {
-		   if( isset($command) ) {
-				list($name, $value) = explode(':', $v, 2);
-		   		$headers[$name]=$value;
-		   } else {
-		   		$command = $v;
-		   }
-		}
-		
-		return new StompFrame($command, $headers, $body);		
-	}
+        }
+        $headers["message-id"] = $messageId ;
+        $this->writeFrame( new StompFrame("ABORT", $headers) );
+    }
+    
+    function disconnect() {
+        $this->writeFrame( new StompFrame("DISCONNECT") );
+        socket_close($this->socket);
+    }
+    
+    function writeFrame($stompFrame) {
+        $data = $stompFrame->command . "\n";        
+        if( isset($stompFrame->headers) ) {
+            foreach ($stompFrame->headers as $name => $value) {
+                $data .= $name . ": " . $value . "\n";
+            }
+        }
+        $data .= "\n";
+        if( isset($stompFrame->body) ) {
+            $data .= $stompFrame->body;
+        }
+        $l1 = strlen($data);
+        $data .= "\x00\n";
+        $l2 = strlen($data);
+        
+        socket_write($this->socket, $data, strlen($data)) or die("Could not send stomp frame to server\n");
+    }
+    
+    function readFrame() {
+
+        $rc = socket_recv($this->socket, &$b, 1, 0);
+        
+        // I think this EOF
+        if( $rc == 0 ) {
+            return null;
+        }
+        // I think this is no data.
+        if( $rc == false ) {
+            return null;
+        }       
+        
+        // Read until end of frame.
+        while( ord($b) != 0  ) {
+
+            $data .= $b;
+            $t = ord($b);
+                                
+            $rc = socket_recv($this->socket,&$b,1,0);
+            
+            // I think this EOF
+            if( $rc == 0 ) {
+                return null;
+            }
+            
+        }
+        
+        # Read in the \n that comes after the \0
+        $rc = socket_recv($this->socket,&$b,1,0);
+        if( $rc == 0 ) {
+            return null;
+        }
+        if( ord($b) != 10 ) {
+            return null;
+        }
+
+        list($header, $body) = explode("\n\n", $data, 2);
+        $header = explode("\n", $header);
+        $headers = array();
+        
+        $command = null;
+        foreach ($header as $v) {
+           if( isset($command) ) {
+                list($name, $value) = explode(':', $v, 2);
+                $headers[$name]=$value;
+           } else {
+                $command = $v;
+           }
+        }
+        
+        return new StompFrame($command, $headers, $body);       
+    }
 }
 
 ?>