Skip to content

Commit

Permalink
More or less working/updated PHP RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glebushka committed Dec 20, 2011
1 parent cd33bbb commit cc75ef8
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 182 deletions.
159 changes: 75 additions & 84 deletions php/lib/Back.php
Original file line number Diff line number Diff line change
@@ -1,103 +1,94 @@
<?php
include_once dirname(__FILE__) . '/Future.php';

class MessagePackRPC_Back
{
public $errorMessage01 = 'Network error';
public $errorMessage02 = 'Objects error';
public $size;

public function __construct($size = 1024)
{
$this->size = $size;
}

public function clientCallObject($code, $func, $args)
{
$data = array();
$data[0] = 0;
$data[1] = $code;
$data[2] = $func;
$data[3] = $args;

return $data;
}

public function clientConnection($host, $port, $call)
{
$size = $this->size;
$send = $this->msgpackEncode($call);
$sock = fsockopen($host, $port);
if ($sock === FALSE) throw new Exception($this->errorMessage01);
$puts = fputs($sock, $send);
if ($puts === FALSE) throw new Exception($this->errorMessage01);
$read = fread($sock, $size);
if ($read === FALSE) throw new Exception($this->errorMessage01);
$end = fclose($sock);

return $read;
}

public function clientRecvObject($recv)
{
$data = $this->msgpackDecode($recv);

$type = $data[0];
$code = $data[1];
$sets = $data[2];
$errs = $data[3];

if ($type != 1) {
throw new Exception($this->errorMessage02);
class MessagePackRPC_Back {
public $errorMessage01 = 'Network error';
public $errorMessage02 = 'Objects error';
public $size;

public function __construct($size = 1024) {
$this->size = $size;
}

$feature = new MessagePackRPC_Future();
$feature->setErrors($errs);
$feature->setResult($sets);
public function clientCallObject($code, $func, $args) {
$data = array();
$data[0] = 0;
$data[1] = $code;
$data[2] = $func;
$data[3] = $args;

return $data;
}

return $feature;
}
public function clientConnection($host, $port, $call) {
$size = $this->size;
$send = $this->msgpackEncode($call);
$sock = @fsockopen($host, $port);
if ($sock === FALSE) throw new Exception($this->errorMessage01);
$puts = fputs($sock, $send);
if ($puts === FALSE) throw new Exception($this->errorMessage01);
$read = fread($sock, $size);
if ($read === FALSE) throw new Exception($this->errorMessage01);
$end = fclose($sock);

return $read;
}

public function serverSendObject($code, $sets, $errs)
{
$data = array();
$data[0] = 1;
$data[1] = $code;
$data[2] = $sets;
$data[3] = $errs;
public function clientRecvObject($recv) {
$data = $this->msgpackDecode($recv);

$send = $this->msgpackEncode($data);
$type = $data[0];
$code = $data[1];
$errs = $data[2];
$sets = $data[3];

return $send;
}
if ($type != 1) {
throw new Exception($this->errorMessage02);
}

public function serverRecvObject($recv)
{
$data = $this->msgpackDecode($recv);
$feature = new MessagePackRPC_Future();
$feature->setErrors($errs);
$feature->setResult($sets);

if (count($data) != 4) {
throw new Exception($this->errorMessage02);
return $feature;
}

$type = $data[0];
$code = $data[1];
$func = $data[2];
$args = $data[3];
public function serverSendObject($code, $sets, $errs) {
$data = array();
$data[0] = 1;
$data[1] = $code;
$data[2] = $errs;
$data[3] = $sets;

$send = $this->msgpackEncode($data);

if ($type != 0) {
throw new Exception($this->errorMessage02);
return $send;
}

return array($code, $func, $args);
}
public function serverRecvObject($recv) {
$data = $this->msgpackDecode($recv);

if (count($data) != 4) {
throw new Exception($this->errorMessage02);
}

public function msgpackDecode($data)
{
return msgpack_unpack($data);
}
$type = $data[0];
$code = $data[1];
$func = $data[2];
$args = $data[3];

public function msgpackEncode($data)
{
return msgpack_pack($data);
}
if ($type != 0) {
throw new Exception($this->errorMessage02);
}

return array($code, $func, $args);
}

public function msgpackDecode($data) {
return msgpack_unpack($data);
}

public function msgpackEncode($data) {
return msgpack_pack($data);
}
}
55 changes: 25 additions & 30 deletions php/lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
<?php
include_once dirname(__FILE__) . '/Back.php';

class MessagePackRPC_Client
{
public $back = null;
public $host = null;
public $port = null;
class MessagePackRPC_Client {
public $back = null;
public $host = null;
public $port = null;

public function __construct($host, $port, $back = null)
{
$this->back = $back == null ? new MessagePackRPC_Back() : $back;
$this->host = $host;
$this->port = $port;
}
public function __construct($host, $port, $back = null) {
$this->back = $back == null ? new MessagePackRPC_Back() : $back;
$this->host = $host;
$this->port = $port;
}

public function send($func, $args)
{
$host = $this->host;
$port = $this->port;
$code = 0;
$call = $this->back->clientCallObject($code, $func, $args);
$send = $this->back->clientConnection($host, $port, $call);
$feature = $this->back->clientRecvObject($send);
public function send($func, $args) {
$host = $this->host;
$port = $this->port;
$code = 0;
$call = $this->back->clientCallObject($code, $func, $args);
$send = $this->back->clientConnection($host, $port, $call);
$feature = $this->back->clientRecvObject($send);

$result = $feature->getResult();
$errors = $feature->getErrors();
$result = $feature->getResult();
$errors = $feature->getErrors();
if (0 < strlen($errors)) {
throw new Exception($errors);
}

if (0 < strlen($errors)) {
throw new Exception($errors);
return $result;
}

return $result;
}

public function call($func, $args)
{
return $this->send($func, $args);
}
public function call($func, $args) {
return $this->send($func, $args);
}
}
45 changes: 20 additions & 25 deletions php/lib/Future.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
<?php
class MessagePackRPC_Future
{
public $result = null;
public $errors = null;
class MessagePackRPC_Future {
public $result = null;
public $errors = null;

public function __construct()
{
}
public function __construct() {
}

public function setResult($result)
{
$this->result = $result;
}
public function setResult($result) {
$this->result = $result;
}

public function getResult()
{
$result = $this->result;
return $result;
}
public function getResult() {
$result = $this->result;
return $result;
}

public function setErrors($errors)
{
$this->errors = $errors;
}
public function setErrors($errors) {
$this->errors = $errors;
}

public function getErrors()
{
$errors = $this->errors;
return $errors;
}
public function getErrors() {
$errors = $this->errors;
return $errors;
}
}
83 changes: 40 additions & 43 deletions php/lib/Server.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
<?php
class MessagePackRPC_Server
{
public $port = null;
public $back = null;
public $hand = null;
class MessagePackRPC_Server {
public $port = null;
public $back = null;
public $hand = null;

public function __construct($port, $hand, $back = null)
{
$this->back = $back == null ? new MessagePackRPC_Back() : $back;
$this->port = $port;
$this->hand = $hand;
}
public function __construct($port, $hand, $back = null) {
$this->back = $back == null ? new MessagePackRPC_Back() : $back;
$this->port = $port;
$this->hand = $hand;
}

public function recv()
{
try {
$sockItem = socket_create_listen($this->port);
$sockList = array($sockItem);
public function recv() {
try {
$sockItem = socket_create_listen($this->port);
$sockList = array($sockItem);

if ($sockItem === FALSE) {
throw new Exception(); // TODO:
}
if ($sockItem === FALSE) {
throw new Exception(); // TODO:
}

// TODO : Server connection check
// TODO : Server connection outer
while (TRUE) {
$moveList = $sockList;
$moveNums = socket_select($moveList, $w = null, $e = null, null);
foreach ($moveList as $moveItem) {
if ($moveItem == $sockItem) {
$acptItem = socket_accept($sockItem);
$sockList[] = $acptItem;
} else {
$data = socket_read($moveItem, $this->back->size);
// TODO : Server connection check
// TODO : Server connection outer
while (TRUE) {
$moveList = $sockList;
$moveNums = socket_select($moveList, $w = null, $e = null, null);
foreach ($moveList as $moveItem) {
if ($moveItem == $sockItem) {
$acptItem = socket_accept($sockItem);
$sockList[] = $acptItem;
} else {
$data = socket_read($moveItem, $this->back->size);

list($code, $func, $args) = $this->back->serverRecvObject($data);
$hand = $this->hand;
$send = $this->back->serverSendObject($code, call_user_func_array(array($hand, $func), $args), "");
socket_write($moveItem, $send);
list($code, $func, $args) = $this->back->serverRecvObject($data);
$hand = $this->hand;
$send = $this->back->serverSendObject($code, call_user_func_array(array($hand, $func), $args), "");
socket_write($moveItem, $send);

$move = unset($sockList[array_search($moveItem, $sockList)]);
socket_close($moveItem);
}
}
}
unset($sockList[array_search($moveItem, $sockList)]);
socket_close($moveItem);
}
}
}

socket_close($sockItem);
} catch (Exception $e) {
// TODO:
socket_close($sockItem);
} catch (Exception $e) {
// TODO:
}
}
}
}

0 comments on commit cc75ef8

Please sign in to comment.