# MessageSegmentHandler.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007-2008 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     final class MessageSegmentHandler implements SegmentHandler
    00016     {
    00017         private $id = null;
    00018
    00019         public function __construct($segmentId)
    00020         {
    00021             $this->id = $segmentId;
    00022         }
    00023
    00024         public function touch($key)
    00025         {
    00026             try {
    00027                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
    00028             } catch (BaseException $e) {
    00029                 // race
    00030                 return false;
    00031             }
    00032
    00033             try {
    00034                 return msg_send($q, $key, 1, false, false);
    00035             } catch (BaseException $e) {
    00036                 // queue is full, rotate it.
    00037                 return msg_remove_queue($q);
    00038             }
    00039
    00040             Assert::isUnreachable();
    00041         }
    00042
    00043         public function unlink($key)
    00044         {
    00045             try {
    00046                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
    00047             } catch (BaseException $e) {
    00048                 // race
    00049                 return false;
    00050             }
    00051
    00052             $type = $msg = null;
    00053
    00054             return msg_receive($q, $key, $type, 2, $msg, false, MSG_IPC_NOWAIT);
    00055         }
    00056
    00057         public function ping($key)
    00058         {
    00059             try {
    00060                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
    00061             } catch (BaseException $e) {
    00062                 // race
    00063                 return false;
    00064             }
    00065
    00066             $type = $msg = null;
    00067
    00068             // YANETUT
    00069             if (msg_receive($q, $key, $type, 2, $msg, false, MSG_IPC_NOWAIT)) {
    00070                 try {
    00071                     msg_send($q, $key, 1, false, false);
    00072                 } catch (BaseException $e) {/* lost key due to race */}
    00073
    00074                 return true;
    00075             }
    00076
    00077             return false;
    00078         }
    00079
    00080         public function drop()
    00081         {
    00082             try {
    00083                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
    00084             } catch (BaseException $e) {
    00085                 // removed in race
    00086                 return true;
    00087             }
    00088
    00089             if (!msg_remove_queue($q)) {
    00090                 // trying to flush manually
    00091                 $type = $msg = null;
    00092
    00093                 while (msg_receive($q, 0, $type, 2, $msg, false, MSG_IPC_NOWAIT)) {
    00094                     // do nothing
    00095                 }
    00096             }
    00097
    00098             return true;
    00099         }
    00100     }
    00101 ?>