/** * Handle the incoming message. * <P> * Normal messages are enqueued.</P> * <P> * The FLUSH message type tells the Queue to write all * stored messages to the ouptut PipeFitting, then * return to normal enqueing operation.</P> * <P> * The SORT message type tells the Queue to sort all * <I>subsequent</I> incoming messages by priority. If there * are unflushed messages in the queue, they will not be * sorted unless a new message is sent before the next FLUSH. * Sorting-by-priority behavior continues even after a FLUSH, * and can be turned off by sending a FIFO message, which is * the default behavior for enqueue/dequeue.</P> * * @param IPipeMessage $message * @return bool */ public function write(IPipeMessage $message) { $success = true; switch ($message->getType()) { // Store normal messages case Message::NORMAL: $this->store($message); break; // Flush the queue // Flush the queue case QueueControlMessage::FLUSH: $success = $this->flush(); break; // Put Queue into Priority Sort or FIFO mode // Subsequent messages written to the queue // will be affected. Sorted messages cannot // be put back into FIFO order! // Put Queue into Priority Sort or FIFO mode // Subsequent messages written to the queue // will be affected. Sorted messages cannot // be put back into FIFO order! case QueueControlMessage::SORT: case QueueControlMessage::FIFO: $this->mode = $message->getType(); break; } return $success; }
/** * Handle the incoming message. * <P> * If message type is normal, filter the message (unless in BYPASS mode) * and write the result to the output pipe fitting if the filter * operation is successful.</P> * * <P> * The FilterControlMessage.SET_PARAMS message type tells the Filter * that the message class is FilterControlMessage, which it * casts the message to in order to retrieve the filter parameters * object if the message is addressed to this filter.</P> * * <P> * The FilterControlMessage.SET_FILTER message type tells the Filter * that the message class is FilterControlMessage, which it * casts the message to in order to retrieve the filter function.</P> * * <P> * The FilterControlMessage.BYPASS message type tells the Filter * that it should go into Bypass mode operation, passing all normal * messages through unfiltered.</P> * * <P> * The FilterControlMessage.FILTER message type tells the Filter * that it should go into Filtering mode operation, filtering all * normal normal messages before writing out. This is the default * mode of operation and so this message type need only be sent to * cancel a previous BYPASS message.</P> * * <P> * The Filter only acts on the control message if it is targeted * to this named filter instance. Otherwise it writes through to the * output.</P> * * @param IPipeMessage $message * @return Boolean True if the filter process does not throw an error and subsequent operations * in the pipeline succede. */ public function write(IPipeMessage $message) { $outputMessage = null; $success = true; // Filter normal messages switch ($message->getType()) { case Message::NORMAL: try { if ($this->mode == FilterControlMessage::FILTER) { $outputMessage = $this->applyFilter($message); } else { $outputMessage = $message; } $success = $this->output->write($outputMessage); } catch (Exception $e) { $success = false; } break; // Accept parameters from control message // Accept parameters from control message case FilterControlMessage::SET_PARAMS: if ($this->isTarget($message)) { $this->setParams($message->getParams()); } else { $success = $this->output->write($outputMessage); } break; // Accept filter function from control message // Accept filter function from control message case FilterControlMessage::SET_FILTER: if ($this->isTarget($message)) { $this->setFilter($message->getFilter()); } else { $success = $this->output->write($outputMessage); } break; // Toggle between Filter or Bypass operational modes // Toggle between Filter or Bypass operational modes case FilterControlMessage::BYPASS: case FilterControlMessage::FILTER: if ($this->isTarget($message)) { $this->mode = $message->getType(); } else { $success = $this->output->write($outputMessage); } break; // Write control messages for other fittings through // Write control messages for other fittings through default: $success = $this->output->write($outputMessage); } return $success; }