MessageSegmentHandler.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 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 /* $Id: MessageSegmentHandler.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class MessageSegmentHandler implements SegmentHandler
00017     {
00018         private $id = null;
00019         
00020         public function __construct($segmentId)
00021         {
00022             $this->id = $segmentId;
00023         }
00024         
00025         public function touch($key)
00026         {
00027             try {
00028                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
00029             } catch (BaseException $e) {
00030                 // race
00031                 return false;
00032             }
00033             
00034             try {
00035                 return msg_send($q, $key, 1, false, false);
00036             } catch (BaseException $e) {
00037                 // queue is full
00038                 $msg = $type = null;
00039 
00040                 msg_receive($q, -(PHP_INT_MAX), $type, 2, $msg, false);
00041                 
00042                 try {
00043                     return msg_send($q, $key, 1, false, false);
00044                 } catch (BaseException $e) {
00045                     // still full
00046                     return false;
00047                 }
00048             }
00049             
00050             Assert::isUnreachable();
00051         }
00052         
00053         public function unlink($key)
00054         {
00055             try {
00056                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
00057             } catch (BaseException $e) {
00058                 // race
00059                 return false;
00060             }
00061             
00062             $type = $msg = null;
00063             
00064             return msg_receive($q, $key, $type, 2, $msg, false, MSG_IPC_NOWAIT);
00065         }
00066         
00067         public function ping($key)
00068         {
00069             try {
00070                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
00071             } catch (BaseException $e) {
00072                 // race
00073                 return false;
00074             }
00075             
00076             $type = $msg = null;
00077             
00078             // YANETUT
00079             if (msg_receive($q, $key, $type, 2, $msg, false, MSG_IPC_NOWAIT)) {
00080                 try {
00081                     msg_send($q, $key, 1, false, false);
00082                 } catch (BaseException $e) {/* lost key due to race */}
00083                 
00084                 return true;
00085             }
00086             
00087             return false;
00088         }
00089         
00090         public function drop()
00091         {
00092             try {
00093                 $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
00094             } catch (BaseException $e) {
00095                 // removed in race
00096                 return true;
00097             }
00098             
00099             if (!msg_remove_queue($q)) {
00100                 // trying to flush manually
00101                 $type = $msg = null;
00102                 
00103                 while (msg_receive($q, 0, $type, 2, $msg, false, MSG_IPC_NOWAIT)) {
00104                     // do nothing
00105                 }
00106             }
00107             
00108             return true;
00109         }
00110     }
00111 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4