/opt/workspace/qpid/tags/M2/Final/cpp/lib/common/sys/posix/EventChannel.h

00001 #ifndef _sys_EventChannel_h
00002 #define _sys_EventChannel_h
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 
00025 #include <SharedObject.h>
00026 #include <ExceptionHolder.h>
00027 #include <boost/function.hpp>
00028 #include <memory>
00029 
00030 namespace qpid {
00031 namespace sys {
00032 
00033 class Event;
00034 class EventHandler;
00035 class EventChannel;
00036 
00040 class Event
00041 {
00042   public:
00044     typedef boost::function0<void> Callback;
00045 
00052     Event(Callback cb = 0) : callback(cb) {}
00053 
00054     virtual ~Event();
00055 
00057     void dispatch();
00058 
00060     bool hasError() const;
00061 
00063     void throwIfError() throw(Exception);
00064 
00065   protected:
00066     virtual void prepare(EventHandler&);
00067     virtual Event* complete(EventHandler&);
00068     void setError(const ExceptionHolder& e);
00069 
00070     Callback callback;
00071     ExceptionHolder error;
00072 
00073   friend class EventChannel;
00074   friend class EventHandler;
00075 };
00076 
00077 template <class BufT>
00078 class IOEvent : public Event {
00079   public:
00080     void getDescriptor() const { return descriptor; }
00081     size_t getSize() const { return size; }
00082     BufT getBuffer() const { return buffer; }
00083   
00084   protected:
00085     IOEvent(int fd, Callback cb, size_t sz, BufT buf) :
00086         Event(cb), descriptor(fd), buffer(buf), size(sz) {}
00087 
00088     int descriptor;
00089     BufT buffer;
00090     size_t size;
00091 };
00092 
00094 class ReadEvent : public IOEvent<void*>
00095 {
00096   public:
00097     explicit ReadEvent(int fd=-1, void* buf=0, size_t sz=0, Callback cb=0) :
00098         IOEvent<void*>(fd, cb, sz, buf), received(0) {}
00099 
00100   private:
00101     void prepare(EventHandler&);
00102     Event* complete(EventHandler&);
00103     ssize_t doRead();
00104 
00105     size_t received;
00106 };
00107 
00109 class WriteEvent : public IOEvent<const void*>
00110 {
00111   public:
00112     explicit WriteEvent(int fd=-1, const void* buf=0, size_t sz=0,
00113                         Callback cb=0) :
00114         IOEvent<const void*>(fd, cb, sz, buf), written(0) {}
00115 
00116   protected:
00117     void prepare(EventHandler&);
00118     Event* complete(EventHandler&);
00119 
00120   private:
00121     ssize_t doWrite();
00122     size_t written;
00123 };
00124 
00126 class AcceptEvent : public Event
00127 {
00128   public:
00130     explicit AcceptEvent(int fd=-1, Callback cb=0) :
00131         Event(cb), descriptor(fd), accepted(0) {}
00132 
00134     int getAcceptedDesscriptor() const { return accepted; }
00135 
00136   private:
00137     void prepare(EventHandler&);
00138     Event* complete(EventHandler&);
00139 
00140     int descriptor;
00141     int accepted;
00142 };
00143 
00144 
00145 class QueueSet;
00146 
00150 class EventChannel : public qpid::SharedObject<EventChannel>
00151 {
00152   public:
00153     static shared_ptr create();
00154     
00155     ~EventChannel();
00156     
00158     void postEvent(Event& event);
00159 
00161     void postEvent(Event* event) { postEvent(*event); }
00162         
00167     Event* getEvent();
00168 
00169   private:
00170     EventChannel();
00171     boost::shared_ptr<EventHandler> handler;
00172 };
00173 
00174 
00175 }}
00176 
00177 
00178 
00179 #endif  

Generated on Mon Nov 26 19:13:17 2007 for Qpid by  doxygen 1.5.1