status = false; try { $this->logger = new gacErrorLogger(); } catch (TypeError $t) { consoleLog($this->res, CON_ERROR, ERROR_TYPE_EXCEPTION . $t->getMessage()); } $this->queueName = $_queueName; $this->validQueues = [ BROKER_QUEUE_R, BROKER_QUEUE_W, BROKER_QUEUE_AI, BROKER_QUEUE_AO, BROKER_QUEUE_M, BROKER_QUEUE_WH, BROKER_QUEUE_U, BROKER_QUEUE_S, BROKER_QUEUE_C ]; if (!in_array($this->queueName, $this->validQueues)) { $this->logger->warn(ERROR_INVALID_QUEUE_NAME . $this->queueName); } else { switch ($this->queueName) { case BROKER_QUEUE_R : case BROKER_QUEUE_W : case BROKER_QUEUE_M : $resource = RESOURCE_BROKER; break; case BROKER_QUEUE_AI : case BROKER_QUEUE_AO : $resource = RESOURCE_ADMIN; break; case BROKER_QUEUE_WH : case BROKER_QUEUE_C : $resource = RESOURCE_SEGUNDO; break; case BROKER_QUEUE_U : case BROKER_QUEUE_S : $resource = RESOURCE_TERCERO; break; default : $msg = ERROR_RESOURCE_404 . $_queueName; $this->logger->info($msg); consoleLog($this->res, CON_SYSTEM, $msg); return; break; } try { $this->rabbitConnection = gasResourceManager::fetchResource($resource); if (is_null($this->rabbitConnection)) return; $this->rabbitChannel = $this->rabbitConnection->channel(); $label = uniqid('gacBrokerClient<' . $_tag . '>:'); list($this->rabbitCallbackQueue, ,) = $this->rabbitChannel->queue_declare($label, false, false, false, true); // was: f, f, f, t $this->rabbitChannel->basic_consume($this->rabbitCallbackQueue, '', false, false, false, false, array($this, BROKER_CLIENT_RESPONSE)); $this->status = true; } catch (AMQPRuntimeException | AMQPTimeoutException | Throwable $t) { $hdr = basename(__METHOD__) . AT . __LINE__ . COLON; consoleLog($this->res, CON_ERROR, $hdr . ERROR_THROWABLE_EXCEPTION); consoleLog($this->res, CON_ERROR, $hdr . $t->getMessage()); } } return; } /** * @noinspection PhpUnused * client_response -- public method * * this method checks to see if the current response, based on the correlation (request) ID, is the one it's * waiting for from the remote (vault) service. * * When it receives the awaited response, it stores the response into a member variable and exits. * * * @author mikegivingassistant.org * @version 1.0 * @param $_response - the class object created in the constructor * * * HISTORY: * ======== * 06-15-17 mks original coding * 09-18-19 mks DB-136: exception wrapped this code * */ public function client_response(AMQPMessage $_response) { try { if ($_response->get(BROKER_CORRELATION_ID) == $this->rabbitCorrelationID) { $this->rabbitResponse = $_response->body; } } catch (AMQPTimeoutException | AMQPRuntimeException | Throwable $t) { $hdr = basename(__METHOD__) . AT . __LINE__ . COLON; consoleLog($this->res, CON_ERROR, $hdr . ERROR_EXCEPTION); consoleLog($this->res, CON_ERROR, $hdr . $t->getMessage()); } } /** * call() -- public method * * This method is invoked outside of the class and is the entry point for publishing a message request to the * broker. It creates a new AMQP message and publishes it to the queue (defined in the constructor), and then * blocks-and-waits for a response from the remote (vault) service. * * Publishing a message is exception trapped and will generate a log message at the warn level if tripped. * * The "raw" response is returned directly to the calling client and will be processed at that level. * * @author mike@givingassistant.org * @version 1.0 * * @param $_data * @return null * * HISTORY: * ======== * 06-15-17 mks original coding * 05-31-18 mks CORE-1011: update for new XML broker services configuration * */ public function call($_data) { $this->rabbitResponse = null; $this->rabbitCorrelationID = uniqid(); $rabbitMessage = new AMQPMessage((string)$_data, [BROKER_CORRELATION_ID => $this->rabbitCorrelationID, BROKER_REPLY_TO => $this->rabbitCallbackQueue]); try { $this->rabbitChannel->basic_publish($rabbitMessage, '', (gasConfig::$settings[CONFIG_BROKER_SERVICES][CONFIG_BROKER_QUEUE_TAG] . $this->queueName)); while (!$this->rabbitResponse) { $this->rabbitChannel->wait(); } } catch (AMQPTimeoutException | AMQPRuntimeException | Throwable $e) { $hdr = basename(__METHOD__) . AT . __LINE__ . COLON; $this->logger->fatal($hdr . ERROR_BROKER_EXCEPTION_TIMEOUT); $this->logger->fatal($hdr . $e->getMessage()); consoleLog('_BTC: ', CON_ERROR, $hdr . $e->getMessage()); } return ($this->rabbitResponse); } public function __destruct() { // As of PHP 5.3.10 destructors are not run on shutdown caused by fatal errors. // // destructor is registered shut-down function in constructor -- so any recovery // efforts should go in this method. try { if (!is_null($this->rabbitChannel)) $this->rabbitChannel->close(); if (!is_null($this->rabbitConnection)) $this->rabbitConnection->close(); } catch (Throwable $t) { $hdr = basename(__METHOD__) . AT . __LINE__ . COLON; consoleLog($this->res, CON_ERROR, $hdr . ERROR_THROWABLE_EXCEPTION); consoleLog($this->res, CON_ERROR, $hdr. $t->getMessage()); } } }