public function testGetChannelname() { $g1 = InstructionControl::getChannelSetId(genSaneGuid()); $n = genSaneGuid(); $chanId = InstructionControl::getChannelId($g1, $n); $this->assertEquals($n, InstructionControl::getChannelname($chanId)); $this->assertEquals(null, InstructionControl::getChannelname(-100)); }
/** * Adds an Instruction to a Channel. * * Body of output document takes the form: * * <code> * {"position":"12","channel":"main","data":{"k"=>"v pairs"},"object_type":"Car","communication_key":"g4c667c80947"} * </code> * * @param array $params Expected in the form: * `array('channelset'=>'name','channel'=>'name','object_type'=>'type of object','data'=>array('k'='v pairs')) */ public function restPostChannelsetChannel($params) { InstructionControl::startTransaction(); $channelsetId = $this->getChannelSetId($params['channelset']); $userRec = $this->getUser($channelsetId); $channelId = InstructionControl::getChannelId($channelsetId, $params['channel']); $instructionId = InstructionControl::reserveInstructionId($userRec['id'], $channelId, $params['object_type']); $data = array_key_exists('data', $_REQUEST) ? $_REQUEST['data'] : array(); InstructionControl::addInstructionData($instructionId, $data); InstructionControl::completeTransaction(); $position = InstructionControl::getPositionForInstructionId($instructionId); $return = InstructionControl::generateReturnDocument('INSTRUCTION', array(), array(), array(), array(), array('position' => $position, 'channel' => $params['channel'], 'data' => $data, 'object_type' => $params['object_type'], 'communication_key' => $userRec['communication_key'])); header('HTTP/1.1 201 Created'); echo json_encode($return); if (INSTRUCTIONCONTROL__NOTIFY_OTHER_CLIENTS == 'APE') { InstructionControl::notifyOtherClientsApe($params['channelset'], $channelId, $return); } return true; }
/** * This function will send an Instruction (properties) to the other clients * on the webpage. * * @param string $channelset The Channelset name * @param string $channelId The Id of the Channel * @param array The output from InstructionControl::generateReturnDocument() * * @see InstructionControl::generateReturnDocument() */ public static function notifyOtherClientsApe($channelset, $channelId, $data) { $APEserver = 'http://' . self::getOption('APE_CONFIG_SERVER') . '/?'; $APEPassword = '******'; if ($channelId === null) { $channelsetId = InstructionControl::getChannelsetId($channelset); $channelId = InstructionControl::getChannelId($channelsetId, 'main'); } $channelName = self::getChannelname($channelId); $cmd = array(array('cmd' => 'inlinepush', 'params' => array('password' => $APEPassword, 'raw' => 'instruction', 'channel' => $channelset . '_' . $channelName, 'data' => $data))); $url = $APEserver . rawurlencode(json_encode($cmd)); $answerJson = file_get_contents($url); $answer = array_pop(json_decode($answerJson, true)); if (!$answer || $answer['data']['value'] != 'ok') { throw new Exception("Could not notify other clients " . print_r($cmd, 1) . print_r($answer, 1)); } }