/examples/thrift/packages/moodle_message/moodle_message.php

https://bitbucket.org/hhteam/moodle_thrift_tools · PHP · 364 lines · 327 code · 30 blank · 7 comment · 42 complexity · a15b7b9209045b709db64ee01a37ee31 MD5 · raw file

  1. <?php
  2. namespace moodle_message;
  3. /**
  4. * Autogenerated by Thrift Compiler (0.9.0-dev)
  5. *
  6. * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
  7. * @generated
  8. */
  9. use Thrift\Base\TBase;
  10. use Thrift\Type\TType;
  11. use Thrift\Type\TMessageType;
  12. use Thrift\Exception\TException;
  13. use Thrift\Exception\TProtocolException;
  14. use Thrift\Protocol\TProtocol;
  15. use Thrift\Exception\TApplicationException;
  16. interface moodle_messageIf {
  17. public function send_instantmessages($messages);
  18. }
  19. class moodle_messageClient implements \moodle_message\moodle_messageIf {
  20. protected $input_ = null;
  21. protected $output_ = null;
  22. protected $seqid_ = 0;
  23. public function __construct($input, $output=null) {
  24. $this->input_ = $input;
  25. $this->output_ = $output ? $output : $input;
  26. }
  27. public function send_instantmessages($messages)
  28. {
  29. $this->send_send_instantmessages($messages);
  30. return $this->recv_send_instantmessages();
  31. }
  32. public function send_send_instantmessages($messages)
  33. {
  34. $args = new \moodle_message\moodle_message_send_instantmessages_args();
  35. $args->messages = $messages;
  36. $bin_accel = ($this->output_ instanceof TProtocol::$TBINARYPROTOCOLACCELERATED) && function_exists('thrift_protocol_write_binary');
  37. if ($bin_accel)
  38. {
  39. thrift_protocol_write_binary($this->output_, 'send_instantmessages', TMessageType::CALL, $args, $this->seqid_, $this->output_->isStrictWrite());
  40. }
  41. else
  42. {
  43. $this->output_->writeMessageBegin('send_instantmessages', TMessageType::CALL, $this->seqid_);
  44. $args->write($this->output_);
  45. $this->output_->writeMessageEnd();
  46. $this->output_->getTransport()->flush();
  47. }
  48. }
  49. public function recv_send_instantmessages()
  50. {
  51. $bin_accel = ($this->input_ instanceof TProtocol::$TBINARYPROTOCOLACCELERATED) && function_exists('thrift_protocol_read_binary');
  52. if ($bin_accel) $result = thrift_protocol_read_binary($this->input_, '\moodle_message\moodle_message_send_instantmessages_result', $this->input_->isStrictRead());
  53. else
  54. {
  55. $rseqid = 0;
  56. $fname = null;
  57. $mtype = 0;
  58. $this->input_->readMessageBegin($fname, $mtype, $rseqid);
  59. if ($mtype == TMessageType::EXCEPTION) {
  60. $x = new TApplicationException();
  61. $x->read($this->input_);
  62. $this->input_->readMessageEnd();
  63. throw $x;
  64. }
  65. $result = new \moodle_message\moodle_message_send_instantmessages_result();
  66. $result->read($this->input_);
  67. $this->input_->readMessageEnd();
  68. }
  69. if ($result->success !== null) {
  70. return $result->success;
  71. }
  72. if ($result->error !== null) {
  73. throw $result->error;
  74. }
  75. throw new \Exception("send_instantmessages failed: unknown result");
  76. }
  77. }
  78. // HELPER FUNCTIONS AND STRUCTURES
  79. class moodle_message_send_instantmessages_args {
  80. static $_TSPEC;
  81. public $messages = null;
  82. public function __construct($vals=null) {
  83. if (!isset(self::$_TSPEC)) {
  84. self::$_TSPEC = array(
  85. 1 => array(
  86. 'var' => 'messages',
  87. 'type' => TType::LST,
  88. 'etype' => TType::STRUCT,
  89. 'elem' => array(
  90. 'type' => TType::STRUCT,
  91. 'class' => '\moodle_message\SendInstantmessagesArgsStruct',
  92. ),
  93. ),
  94. );
  95. }
  96. if (is_array($vals)) {
  97. if (isset($vals['messages'])) {
  98. $this->messages = $vals['messages'];
  99. }
  100. }
  101. }
  102. public function getName() {
  103. return 'moodle_message_send_instantmessages_args';
  104. }
  105. public function read($input)
  106. {
  107. $xfer = 0;
  108. $fname = null;
  109. $ftype = 0;
  110. $fid = 0;
  111. $xfer += $input->readStructBegin($fname);
  112. while (true)
  113. {
  114. $xfer += $input->readFieldBegin($fname, $ftype, $fid);
  115. if ($ftype == TType::STOP) {
  116. break;
  117. }
  118. switch ($fid)
  119. {
  120. case 1:
  121. if ($ftype == TType::LST) {
  122. $this->messages = array();
  123. $_size9 = 0;
  124. $_etype12 = 0;
  125. $xfer += $input->readListBegin($_etype12, $_size9);
  126. for ($_i13 = 0; $_i13 < $_size9; ++$_i13)
  127. {
  128. $elem14 = null;
  129. $elem14 = new \moodle_message\SendInstantmessagesArgsStruct();
  130. $xfer += $elem14->read($input);
  131. $this->messages []= $elem14;
  132. }
  133. $xfer += $input->readListEnd();
  134. } else {
  135. $xfer += $input->skip($ftype);
  136. }
  137. break;
  138. default:
  139. $xfer += $input->skip($ftype);
  140. break;
  141. }
  142. $xfer += $input->readFieldEnd();
  143. }
  144. $xfer += $input->readStructEnd();
  145. return $xfer;
  146. }
  147. public function write($output) {
  148. $xfer = 0;
  149. $xfer += $output->writeStructBegin('moodle_message_send_instantmessages_args');
  150. if ($this->messages !== null) {
  151. if (!is_array($this->messages)) {
  152. throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA);
  153. }
  154. $xfer += $output->writeFieldBegin('messages', TType::LST, 1);
  155. {
  156. $output->writeListBegin(TType::STRUCT, count($this->messages));
  157. {
  158. foreach ($this->messages as $iter15)
  159. {
  160. $xfer += $iter15->write($output);
  161. }
  162. }
  163. $output->writeListEnd();
  164. }
  165. $xfer += $output->writeFieldEnd();
  166. }
  167. $xfer += $output->writeFieldStop();
  168. $xfer += $output->writeStructEnd();
  169. return $xfer;
  170. }
  171. }
  172. class moodle_message_send_instantmessages_result {
  173. static $_TSPEC;
  174. public $success = null;
  175. public $error = null;
  176. public function __construct($vals=null) {
  177. if (!isset(self::$_TSPEC)) {
  178. self::$_TSPEC = array(
  179. 0 => array(
  180. 'var' => 'success',
  181. 'type' => TType::LST,
  182. 'etype' => TType::STRUCT,
  183. 'elem' => array(
  184. 'type' => TType::STRUCT,
  185. 'class' => '\moodle_message\SendInstantmessagesRetStruct',
  186. ),
  187. ),
  188. 1 => array(
  189. 'var' => 'error',
  190. 'type' => TType::STRUCT,
  191. 'class' => '\moodle_message\ApiError',
  192. ),
  193. );
  194. }
  195. if (is_array($vals)) {
  196. if (isset($vals['success'])) {
  197. $this->success = $vals['success'];
  198. }
  199. if (isset($vals['error'])) {
  200. $this->error = $vals['error'];
  201. }
  202. }
  203. }
  204. public function getName() {
  205. return 'moodle_message_send_instantmessages_result';
  206. }
  207. public function read($input)
  208. {
  209. $xfer = 0;
  210. $fname = null;
  211. $ftype = 0;
  212. $fid = 0;
  213. $xfer += $input->readStructBegin($fname);
  214. while (true)
  215. {
  216. $xfer += $input->readFieldBegin($fname, $ftype, $fid);
  217. if ($ftype == TType::STOP) {
  218. break;
  219. }
  220. switch ($fid)
  221. {
  222. case 0:
  223. if ($ftype == TType::LST) {
  224. $this->success = array();
  225. $_size16 = 0;
  226. $_etype19 = 0;
  227. $xfer += $input->readListBegin($_etype19, $_size16);
  228. for ($_i20 = 0; $_i20 < $_size16; ++$_i20)
  229. {
  230. $elem21 = null;
  231. $elem21 = new \moodle_message\SendInstantmessagesRetStruct();
  232. $xfer += $elem21->read($input);
  233. $this->success []= $elem21;
  234. }
  235. $xfer += $input->readListEnd();
  236. } else {
  237. $xfer += $input->skip($ftype);
  238. }
  239. break;
  240. case 1:
  241. if ($ftype == TType::STRUCT) {
  242. $this->error = new \moodle_message\ApiError();
  243. $xfer += $this->error->read($input);
  244. } else {
  245. $xfer += $input->skip($ftype);
  246. }
  247. break;
  248. default:
  249. $xfer += $input->skip($ftype);
  250. break;
  251. }
  252. $xfer += $input->readFieldEnd();
  253. }
  254. $xfer += $input->readStructEnd();
  255. return $xfer;
  256. }
  257. public function write($output) {
  258. $xfer = 0;
  259. $xfer += $output->writeStructBegin('moodle_message_send_instantmessages_result');
  260. if ($this->success !== null) {
  261. if (!is_array($this->success)) {
  262. throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA);
  263. }
  264. $xfer += $output->writeFieldBegin('success', TType::LST, 0);
  265. {
  266. $output->writeListBegin(TType::STRUCT, count($this->success));
  267. {
  268. foreach ($this->success as $iter22)
  269. {
  270. $xfer += $iter22->write($output);
  271. }
  272. }
  273. $output->writeListEnd();
  274. }
  275. $xfer += $output->writeFieldEnd();
  276. }
  277. if ($this->error !== null) {
  278. $xfer += $output->writeFieldBegin('error', TType::STRUCT, 1);
  279. $xfer += $this->error->write($output);
  280. $xfer += $output->writeFieldEnd();
  281. }
  282. $xfer += $output->writeFieldStop();
  283. $xfer += $output->writeStructEnd();
  284. return $xfer;
  285. }
  286. }
  287. class moodle_messageProcessor {
  288. protected $handler_ = null;
  289. public function __construct($handler) {
  290. $this->handler_ = $handler;
  291. }
  292. public function process($input, $output) {
  293. $rseqid = 0;
  294. $fname = null;
  295. $mtype = 0;
  296. $input->readMessageBegin($fname, $mtype, $rseqid);
  297. $methodname = 'process_'.$fname;
  298. if (!method_exists($this, $methodname)) {
  299. $input->skip(TType::STRUCT);
  300. $input->readMessageEnd();
  301. $x = new TApplicationException('Function '.$fname.' not implemented.', TApplicationException::UNKNOWN_METHOD);
  302. $output->writeMessageBegin($fname, TMessageType::EXCEPTION, $rseqid);
  303. $x->write($output);
  304. $output->writeMessageEnd();
  305. $output->getTransport()->flush();
  306. return;
  307. }
  308. $this->$methodname($rseqid, $input, $output);
  309. return true;
  310. }
  311. protected function process_send_instantmessages($seqid, $input, $output) {
  312. $args = new \moodle_message\moodle_message_send_instantmessages_args();
  313. $args->read($input);
  314. $input->readMessageEnd();
  315. $result = new \moodle_message\moodle_message_send_instantmessages_result();
  316. try {
  317. $result->success = $this->handler_->send_instantmessages($args->messages);
  318. } catch (\moodle_message\ApiError $error) {
  319. $result->error = $error;
  320. }
  321. $bin_accel = ($output instanceof TProtocol::$TBINARYPROTOCOLACCELERATED) && function_exists('thrift_protocol_write_binary');
  322. if ($bin_accel)
  323. {
  324. thrift_protocol_write_binary($output, 'send_instantmessages', TMessageType::REPLY, $result, $seqid, $output->isStrictWrite());
  325. }
  326. else
  327. {
  328. $output->writeMessageBegin('send_instantmessages', TMessageType::REPLY, $seqid);
  329. $result->write($output);
  330. $output->writeMessageEnd();
  331. $output->getTransport()->flush();
  332. }
  333. }
  334. }