public static function decodeResponse($bert) { $response = Bert::decode($bert); if ($response[0] == 'reply') { return $response[1]; } elseif ($response[0] == 'noreply') { return null; } elseif ($response[0] == 'error') { self::_error($response[1]); } else { throw new Exception('Unknown response type'); } }
public function testConvertRecursive() { $this->assertEqual(Bert_Encoder::convert(Bert::t(Bert::t(true), false)), Bert::t(Bert::t(Bert::t(Bert::a('bert'), Bert::a('true'))), Bert::t(Bert::a('bert'), Bert::a('false')))); }
public function testDecodeDict() { $bert = Bert::encode(array('a' => 'b')); $this->assertEqual(Bert_Decoder::decode($bert), array('a' => 'b')); }
public static function start() { $input = fopen("php://fd/3", 'r'); $output = fopen("php://fd/4", 'w'); while (true) { $obj = self::readBerp($input); if (!isset($obj)) { echo "Could not read BERP length header. Ernie server may have gone away. Exiting now.\n"; exit(1); } if (count($obj) == 4 && $obj[0] == 'call') { $mod = $obj[1]; $fun = $obj[2]; $args = $obj[3]; try { $result = self::dispatch($mod, $fun, $args); $response = Bert::t(Bert::a('reply'), $result); self::writeBerp($output, $response); } catch (Ernie_ServerError $e) { $response = Bert::t(Bert::a('error'), Bert::t(Bert::a('server'), 0, get_class($e), $e->getMessage(), $e->getTrace())); self::writeBerp($output, $response); } catch (Exception $e) { $response = Bert::t(Bert::a('error'), Bert::t(Bert::a('user'), 0, get_class($e), $e->getMessage(), $e->getTrace())); self::writeBerp($output, $response); } } elseif (count($obj) == 4 && $obj[0] == 'cast') { $mod = $obj[1]; $fun = $obj[2]; $args = $obj[3]; try { $result = self::dispatch($mod, $fun, $args); } catch (Exception $e) { // ignore } self::writeBerp($output, Bert::t(Bert::a('noreply'))); } else { $outObj = Bert::t(Bert::a('error'), array(Bert::a('server'), 0, "Invalid request: " . print_r($obj, true))); self::writeBerp($output, $outObj); } } }
<?php Bert::registerAutoload(); class Bert { public static function encode($obj) { return Bert_Encoder::encode($obj); } public static function decode($bert) { return Bert_Decoder::decode($bert); } public static function ebin($str) { $bytes = unpack('C*', $str); return '<<' . implode(',', $bytes) . '>>'; } public static function a($str) { return new Bert_Atom($str); } public static function t() { return new Bert_Tuple(func_get_args()); } // -- public static function autoload($class) { if (0 !== strpos($class, 'Bert')) { return false;
public function readComplexType() { $val = $this->readAnyRaw(); if ($val == Bert::a('nil')) { return null; } elseif ($val == Bert::a('true')) { return true; } elseif ($val == Bert::a('false')) { return false; } elseif ($val == Bert::a('time')) { return new Bert_Time($this->readAnyRaw(), $this->readAnyRaw(), $this->readAnyRaw()); } elseif ($val == Bert::a('regex')) { $source = $this->readAnyRaw(); $opts = $this->readAnyRaw(); $options = array(); foreach ($opts as $name) { $options[] = "{$name}"; } return new Bert_Regex($source, $options); } elseif ($val == Bert::a('dict')) { return $this->readDict(); } else { return null; } }
public function cast($options = null) { $this->_verifyOptions($options); return new Bert_Rpc_Request($this, Bert::a('cast'), $options); }
public function execute() { $bertRequest = Bert_Rpc_Action_Encodes::encodeRequest(Bert::t($this->_req->kind, $this->_mod, $this->_fun, $this->_args)); $bertResponse = $this->_transaction($bertRequest); return Bert_Rpc_Action_Encodes::decodeResponse($bertResponse); }