00001 /* -*- c++ -*- */
00002 /**************************************************************************
00003 Copyright (c) 2000-2001, Tony Garnock-Jones
00004 All rights reserved.
00005
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are
00008 met:
00009
00010 * Redistributions of source code must retain the above copyright
00011 notice, this list of conditions and the following disclaimer.
00012
00013 * Redistributions in binary form must reproduce the above
00014 copyright notice, this list of conditions and the following
00015 disclaimer in the documentation and/or other materials provided
00016 with the distribution.
00017
00018 * Neither the names of the copyright holders nor the names of this
00019 software's contributors may be used to endorse or promote
00020 products derived from this software without specific prior
00021 written permission.
00022
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00027 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00029 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 **************************************************************************/
00035
00036 #ifndef MPS_Connection_H
00037 #define MPS_Connection_H
00038
00039 namespace MPS {
00040
00041 /**
00042 * Manages a connection over a Transport to an MPS server. Allows
00043 * message reception and transmission. */
00044 class Connection: public Referenceable {
00045 public:
00046 /// A callback - subclass, and use with setCallback.
00047 class Callback: public Referenceable {
00048 public:
00049 typedef void (Callback::*Method)(InputStream &input);
00050 };
00051
00052 private:
00053 Address address; ///< the address of the remote object
00054 ref<Callback> callback; ///< currently-registered callback.
00055 Callback::Method callbackMethod; ///< method to invoke on registered callback.
00056
00057 protected:
00058 /// Constructor for subclasses.
00059 Connection(Address const &a)
00060 : address(a),
00061 callback(0),
00062 callbackMethod(0)
00063 {}
00064
00065 public:
00066 virtual ~Connection() {}
00067
00068 /// Extract the address of the object we're connected to.
00069 Address const &getRemoteAddress() const { return address; }
00070
00071 /**
00072 * Obtain an OutputStream to use to send a message to the remote
00073 * object. The caller must call releaseOutputStream() when it's
00074 * finished with the stream.
00075 *
00076 * @return an OutputStream which, when flushed, will send a message
00077 * to the remote object. */
00078 virtual OutputStream *getOutputStream() = 0;
00079
00080 /**
00081 * Signals that the caller is finished with an OutputStream obtained
00082 * via the getOutputStream() method.
00083 *
00084 * @param stream the stream to release */
00085 virtual void releaseOutputStream(OutputStream *stream) = 0;
00086
00087 /**
00088 * Obtain an InputStream to use to receive a message from the remote
00089 * object. The caller must call releaseInputStream() when it's
00090 * finished with the stream.
00091 *
00092 * @return an InputStream which allows reading of the most recent
00093 * message sent back from the remote object. */
00094 virtual InputStream *getInputStream() = 0;
00095
00096 /**
00097 * Signals that the caller is finished with an InputStream obtained
00098 * via the getInputStream() method.
00099 *
00100 * @param stream the stream to release */
00101 virtual void releaseInputStream(InputStream *stream) = 0;
00102
00103 /**
00104 * Registers a callback. Override this if you need to do special
00105 * processing on callback registration (unlikely).
00106 *
00107 * @see Proxy::_setCallback */
00108 virtual void setCallback(ref<Callback> _callback, Callback::Method _method) {
00109 callback = _callback;
00110 callbackMethod = _method;
00111 }
00112
00113 /**
00114 * If a callback is registered, deregisters it, and calls it. Note
00115 * that callbacks should be called when it's safe to read a complete
00116 * message from this connection (ie. it won't block to do so). This
00117 * method should be called from somewhere within Transports' main loops.
00118 *
00119 * @see Proxy::_setCallback */
00120 void fireCallback(InputStream &input) {
00121 if (callback.valid()) {
00122 // Save the existing callback...
00123 ref<Callback> cb = callback;
00124 Callback::Method meth = callbackMethod;
00125
00126 // Clear it out, so there's no registered callback anymore...
00127 callback = 0;
00128 callbackMethod = 0;
00129
00130 // ... and finally call our saved callback.
00131 (cb->*meth)(input);
00132 } else {
00133 // Do nothing if no callback is registered.
00134 }
00135 }
00136 };
00137
00138 }
00139
00140 #endif
1.2.6 written by Dimitri van Heesch,
© 1997-2001