/home/aconway/svn/qpid/cpp/src/tests/MockConnectionInputHandler.h

00001 #ifndef _tests_MockConnectionInputHandler_h
00002 #define _tests_MockConnectionInputHandler_h
00003 
00004 /*
00005  *
00006  * Copyright (c) 2006 The Apache Software Foundation
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *    http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  *
00020  */
00021 
00022 #include "qpid/sys/ConnectionInputHandler.h"
00023 #include "qpid/sys/ConnectionInputHandlerFactory.h"
00024 #include "qpid/sys/Monitor.h"
00025 #include "qpid/framing/ProtocolInitiation.h"
00026 
00027 struct MockConnectionInputHandler : public qpid::sys::ConnectionInputHandler {
00028 
00029     MockConnectionInputHandler() : state(START) {}
00030 
00031     ~MockConnectionInputHandler() {}
00032     
00033     void initiated(const qpid::framing::ProtocolInitiation& pi) {
00034         qpid::sys::Monitor::ScopedLock l(monitor);
00035         init = pi;
00036         setState(GOT_INIT);
00037     }
00038 
00039     void received(qpid::framing::AMQFrame* framep) {
00040         qpid::sys::Monitor::ScopedLock l(monitor);
00041         frame = *framep;
00042         setState(GOT_FRAME);
00043     }
00044 
00045     qpid::framing::ProtocolInitiation waitForProtocolInit() {        
00046         waitFor(GOT_INIT);
00047         return init;
00048     }
00049 
00050     qpid::framing::AMQFrame waitForFrame() {        
00051         waitFor(GOT_FRAME);
00052         return frame;
00053     }
00054 
00055     void waitForClosed() {
00056         waitFor(CLOSED);
00057     }
00058     
00059     void closed() {
00060         qpid::sys::Monitor::ScopedLock l(monitor);
00061         setState(CLOSED);
00062     }
00063 
00064     void idleOut() {}
00065     void idleIn() {}
00066 
00067   private:
00068     typedef enum { START, GOT_INIT, GOT_FRAME, CLOSED } State;
00069 
00070     void setState(State s) {
00071         state = s;
00072         monitor.notify();
00073     }
00074     
00075     void waitFor(State s) {
00076         qpid::sys::Monitor::ScopedLock l(monitor);
00077         qpid::sys::Time deadline = qpid::sys::now() + 10*qpid::sys::TIME_SEC; 
00078         while (state != s)
00079             CPPUNIT_ASSERT(monitor.wait(deadline));
00080     }
00081 
00082     qpid::sys::Monitor  monitor;
00083     State state;
00084     qpid::framing::ProtocolInitiation init;
00085     qpid::framing::AMQFrame frame;
00086 };
00087 
00088 
00089 struct MockConnectionInputHandlerFactory : public qpid::sys::ConnectionInputHandlerFactory {
00090     MockConnectionInputHandlerFactory() : handler(0) {}
00091 
00092     qpid::sys::ConnectionInputHandler* create(qpid::sys::ConnectionOutputHandler*) {
00093         qpid::sys::Monitor::ScopedLock lock(monitor);
00094         handler = new MockConnectionInputHandler();
00095         monitor.notifyAll();
00096         return handler;
00097     }
00098 
00099     void waitForHandler() {
00100         qpid::sys::Monitor::ScopedLock lock(monitor);
00101         qpid::sys::Time deadline =
00102             qpid::sys::now() + 500 * qpid::sys::TIME_SEC;
00103         while (handler == 0)
00104             CPPUNIT_ASSERT(monitor.wait(deadline));
00105     }
00106     
00107     MockConnectionInputHandler* handler;
00108     qpid::sys::Monitor monitor;
00109 };
00110 
00111 
00112 
00113 #endif  

Generated on Tue Apr 17 14:22:03 2007 for Qpid by  doxygen 1.4.7