web/lib/Zend/Wildfire/Protocol/JsonStream.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Wildfire
       
    17  * @subpackage Protocol
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: JsonStream.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /** Zend_Wildfire_Plugin_Interface */
       
    24 require_once 'Zend/Wildfire/Plugin/Interface.php';
       
    25 
       
    26 /** Zend_Wildfire_Channel_Interface */
       
    27 require_once 'Zend/Wildfire/Channel/Interface.php';
       
    28 
       
    29 /** Zend_Json */
       
    30 require_once 'Zend/Json.php';
       
    31 
       
    32 /**
       
    33  * Encodes messages into the Wildfire JSON Stream Communication Protocol.
       
    34  *
       
    35  * @category   Zend
       
    36  * @package    Zend_Wildfire
       
    37  * @subpackage Protocol
       
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    40  */
       
    41 class Zend_Wildfire_Protocol_JsonStream
       
    42 {
       
    43     /**
       
    44      * The protocol URI for this protocol
       
    45      */
       
    46     const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
       
    47 
       
    48     /**
       
    49      * All messages to be sent.
       
    50      * @var array
       
    51      */
       
    52     protected $_messages = array();
       
    53 
       
    54     /**
       
    55      * Plugins that are using this protocol
       
    56      * @var array
       
    57      */
       
    58     protected $_plugins = array();
       
    59 
       
    60     /**
       
    61      * Register a plugin that uses this protocol
       
    62      *
       
    63      * @param Zend_Wildfire_Plugin_Interface $plugin The plugin to be registered
       
    64      * @return boolean Returns TRUE if plugin was registered, false if it was already registered
       
    65      */
       
    66     public function registerPlugin(Zend_Wildfire_Plugin_Interface $plugin)
       
    67     {
       
    68         if (in_array($plugin,$this->_plugins)) {
       
    69             return false;
       
    70         }
       
    71         $this->_plugins[] = $plugin;
       
    72         return true;
       
    73     }
       
    74 
       
    75     /**
       
    76      * Record a message with the given data in the given structure
       
    77      *
       
    78      * @param Zend_Wildfire_Plugin_Interface $plugin The plugin recording the message
       
    79      * @param string $structure The structure to be used for the data
       
    80      * @param array $data The data to be recorded
       
    81      * @return boolean Returns TRUE if message was recorded
       
    82      */
       
    83     public function recordMessage(Zend_Wildfire_Plugin_Interface $plugin, $structure, $data)
       
    84     {
       
    85         if(!isset($this->_messages[$structure])) {
       
    86             $this->_messages[$structure] = array();
       
    87         }
       
    88 
       
    89         $uri = $plugin->getUri();
       
    90 
       
    91         if(!isset($this->_messages[$structure][$uri])) {
       
    92             $this->_messages[$structure][$uri] = array();
       
    93         }
       
    94 
       
    95         $this->_messages[$structure][$uri][] = $this->_encode($data);
       
    96         return true;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Remove all qued messages
       
   101      *
       
   102      * @param Zend_Wildfire_Plugin_Interface $plugin The plugin for which to clear messages
       
   103      * @return boolean Returns TRUE if messages were present
       
   104      */
       
   105     public function clearMessages(Zend_Wildfire_Plugin_Interface $plugin)
       
   106     {
       
   107         $uri = $plugin->getUri();
       
   108 
       
   109         $present = false;
       
   110         foreach ($this->_messages as $structure => $messages) {
       
   111 
       
   112             if(!isset($this->_messages[$structure][$uri])) {
       
   113                 continue;
       
   114             }
       
   115 
       
   116             $present = true;
       
   117 
       
   118             unset($this->_messages[$structure][$uri]);
       
   119 
       
   120             if (!$this->_messages[$structure]) {
       
   121                 unset($this->_messages[$structure]);
       
   122             }
       
   123         }
       
   124         return $present;
       
   125     }
       
   126 
       
   127     /**
       
   128      * Get all qued messages
       
   129      *
       
   130      * @return mixed Returns qued messages or FALSE if no messages are qued
       
   131      */
       
   132     public function getMessages()
       
   133     {
       
   134         if (!$this->_messages) {
       
   135             return false;
       
   136         }
       
   137         return $this->_messages;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Use the JSON encoding scheme for the value specified
       
   142      *
       
   143      * @param mixed $value The value to be encoded
       
   144      * @return string  The encoded value
       
   145      */
       
   146     protected function _encode($value)
       
   147     {
       
   148         return Zend_Json::encode($value, true, array('silenceCyclicalExceptions'=>true));
       
   149     }
       
   150 
       
   151     /**
       
   152      * Retrieves all formatted data ready to be sent by the channel.
       
   153      *
       
   154      * @param Zend_Wildfire_Channel_Interface $channel The instance of the channel that will be transmitting the data
       
   155      * @return mixed Returns the data to be sent by the channel.
       
   156      * @throws Zend_Wildfire_Exception
       
   157      */
       
   158     public function getPayload(Zend_Wildfire_Channel_Interface $channel)
       
   159     {
       
   160         if (!$channel instanceof Zend_Wildfire_Channel_HttpHeaders) {
       
   161             require_once 'Zend/Wildfire/Exception.php';
       
   162             throw new Zend_Wildfire_Exception('The '.get_class($channel).' channel is not supported by the '.get_class($this).' protocol.');
       
   163         }
       
   164 
       
   165         if ($this->_plugins) {
       
   166             foreach ($this->_plugins as $plugin) {
       
   167                 $plugin->flushMessages(self::PROTOCOL_URI);
       
   168             }
       
   169         }
       
   170 
       
   171         if (!$this->_messages) {
       
   172             return false;
       
   173         }
       
   174 
       
   175         $protocol_index = 1;
       
   176         $structure_index = 1;
       
   177         $plugin_index = 1;
       
   178         $message_index = 1;
       
   179 
       
   180         $payload = array();
       
   181 
       
   182         $payload[] = array('Protocol-'.$protocol_index, self::PROTOCOL_URI);
       
   183 
       
   184         foreach ($this->_messages as $structure_uri => $plugin_messages ) {
       
   185 
       
   186             $payload[] = array($protocol_index.'-Structure-'.$structure_index, $structure_uri);
       
   187 
       
   188             foreach ($plugin_messages as $plugin_uri => $messages ) {
       
   189 
       
   190                 $payload[] = array($protocol_index.'-Plugin-'.$plugin_index, $plugin_uri);
       
   191 
       
   192                 foreach ($messages as $message) {
       
   193 
       
   194                     $parts = explode("\n",chunk_split($message, 5000, "\n"));
       
   195 
       
   196                     for ($i=0 ; $i<count($parts) ; $i++) {
       
   197 
       
   198                         $part = $parts[$i];
       
   199                         if ($part) {
       
   200 
       
   201                             $msg = '';
       
   202 
       
   203                             if (count($parts)>2) {
       
   204                                 $msg = (($i==0)?strlen($message):'')
       
   205                                        . '|' . $part . '|'
       
   206                                        . (($i<count($parts)-2)?'\\':'');
       
   207                             } else {
       
   208                                 $msg = strlen($part) . '|' . $part . '|';
       
   209                             }
       
   210 
       
   211                             $payload[] = array($protocol_index . '-'
       
   212                                                . $structure_index . '-'
       
   213                                                . $plugin_index . '-'
       
   214                                                . $message_index,
       
   215                                                $msg);
       
   216 
       
   217                             $message_index++;
       
   218 
       
   219                             if ($message_index > 99999) {
       
   220                                 require_once 'Zend/Wildfire/Exception.php';
       
   221                                 throw new Zend_Wildfire_Exception('Maximum number (99,999) of messages reached!');
       
   222                             }
       
   223                         }
       
   224                     }
       
   225                 }
       
   226                 $plugin_index++;
       
   227             }
       
   228             $structure_index++;
       
   229         }
       
   230 
       
   231         return $payload;
       
   232     }
       
   233 
       
   234 }
       
   235