Esempio n. 1
0
 /**
  * 
  * @param string $action
  * @return ResponseWrapperImpl
  * @throws Exception
  */
 private function execute($action)
 {
     /**
      * @var ResourceWrapper
      */
     $resourceWrapper = new ResourceWrapper($this->_resource, $action);
     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ($socket === FALSE) {
         $fatalMsg = "socket_create, reason : " . socket_strerror(socket_last_error());
         self::$_log->fatal($fatalMsg);
         throw new Exception($fatalMsg);
     }
     if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
         $fatalMsg = "socket_set_option, reason : " . socket_strerror(socket_last_error());
         self::$_log->fatal($fatalMsg);
         throw new Exception($fatalMsg);
     }
     self::$_log->info("socket created successfully...");
     $result = socket_connect($socket, $this->_clientConnection->getServerAddr(), $this->_clientConnection->getServerPort());
     if ($result === FALSE) {
         $fatalMsg = "socket_connect, reason : " . socket_strerror(socket_last_error());
         self::$_log->fatal($fatalMsg);
         throw new Exception($fatalMsg);
     }
     self::$_log->info("socket connected successfully...");
     $clientRequest = self::CLIENT_HEADER . serialize($resourceWrapper);
     $result2 = socket_write($socket, $clientRequest, strlen($clientRequest));
     if ($result2 === FALSE) {
         $fatalMsg = "socket_write, reason : " . socket_strerror(socket_last_error());
         self::$_log->fatal($fatalMsg);
         throw new Exception($fatalMsg);
     }
     self::$_log->info("socket wrote successfully...");
     self::$_log->debug("client wrote this <ResourceWrapper> : " . print_r($resourceWrapper, TRUE));
     $serverResponse = socket_read($socket, $this->_clientConnection->getMaxResourceSerializedLength());
     if ($serverResponse === FALSE) {
         $fataMsg = "socket_read,reason : " . socket_strerror(socket_last_error($socket));
         self::$_log->error($fataMsg);
         throw new Exception($fataMsg);
     }
     /**
      * var ResponseWrapperImpl
      */
     $responseResourceWrapper = unserialize($serverResponse);
     self::$_log->debug("response received from server : " . print_r($responseResourceWrapper, TRUE));
     // @param2 - 0 => block reading in socket ...
     socket_shutdown($socket, 0);
     socket_close($socket);
     unset($socket);
     self::$_log->info("connection terminated with server ...");
     return $responseResourceWrapper;
 }
Esempio n. 2
0
 public static function main($args)
 {
     $clientConnection = ClientConnectionImpl::newClient();
     $identifier = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");
     $anyemClient = new AnyemClientImpl($clientConnection, $identifier);
     for ($i = 0; $i < $args[0]; $i++) {
         usleep(1000);
         try {
             $responseWrapper = $anyemClient->read();
         } catch (Exception $e) {
             print $e->getMessage() . "\n";
             continue;
         }
         print sprintf("variable [a] contains: %s\n", $responseWrapper->getResource()->getData());
     }
 }
Esempio n. 3
0
 public static function main($args)
 {
     $clientConnection = ClientConnectionImpl::newClient();
     $identifier = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");
     $client = new ClientImpl($clientConnection, $identifier);
     for ($i = 0; $i < $args[0]; $i++) {
         $a = 0;
         /**
          * @var ResponseWrapperImpl
          */
         $responseWrapper = $client->get($a);
         $a = $responseWrapper->getResource()->getData();
         $client->put(++$a);
     }
     print "THESE DATA ARE PROVIDED FROM ANYEM SERVER\n";
     printf("after %s repetition, [a] variable contains : %s", $args[0], $a);
 }
Esempio n. 4
0
 public function testGetPut()
 {
     $expected = $iteration = 2000;
     $clientConnection = ClientConnectionImpl::newClient();
     $identifier = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");
     $anyemClient = new AnyemClientImpl($clientConnection, $identifier);
     $a = 0;
     for ($i = 0; $i < $iteration; $i++) {
         try {
             $responseWrapper = $anyemClient->get($a, 10, 300000);
         } catch (Exception $e) {
             print $e->getMessage() . "\n";
             continue;
         }
         $a = $responseWrapper->getResource()->getData();
         $anyemClient->put(++$a);
     }
     $this->assertEquals($expected, $a);
 }
 private static function init()
 {
     if (self::$_initialized === TRUE) {
         return;
     }
     self::$_clientConnection = ClientConnectionImpl::newClient();
     self::$_identifier = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");
     self::$_initialized = TRUE;
 }