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_Transport_Inet_H 00037 #define MPS_Transport_Inet_H 00038 00039 #include <mps/mps.h> 00040 #include <FileDescriptor.h> 00041 #include <SocketDescriptor.h> 00042 00043 namespace MPS { 00044 00045 /** 00046 * Implements a TCP/IP-socket Transport class for accessing and 00047 * serving MPS objects. Manages all server and client sockets for this 00048 * process. Dispatches incoming work to the appropriate objects. */ 00049 class InetTransport: public MPS::Transport { 00050 private: 00051 static InetTransport *instance; ///< The single, per-application instance of InetTransport 00052 00053 //--------------------------------------------------------------------------- 00054 00055 class InetIOStream; ///< Subclass of MPS::InputStream and MPS::OutputStream 00056 class InetConnection; ///< Subclass of MPS::Connection - holds demux, oid 00057 class InetDemux; ///< Reads OID from an incoming message and dispatches to callback 00058 class InetDispatcher; ///< Callback for dispatching to a given MPS::Server 00059 00060 typedef map< string, ref<InetDemux> > demuxMap_t; ///< Map of inet addr -> demux 00061 00062 //--------------------------------------------------------------------------- 00063 00064 ref<FileDescriptorManager> fdMgr; ///< All FileDescriptors we are responsible for. 00065 00066 demuxMap_t clientMap; ///< Find a client demux for a given ipaddr:port 00067 demuxMap_t serverMap; ///< Find a server demux for a given ipaddr:port 00068 ref<InetDemux> defaultDemux; ///< Demultiplexor for the server socket 00069 int nextOid; ///< Next OID available for registration here. 00070 00071 //--------------------------------------------------------------------------- 00072 00073 InetTransport(string const &hostName, 00074 int portNumber); 00075 00076 ref<InetDemux> registerServerSocket(ref<ServerSocketDescriptor> const &serverSock); 00077 00078 protected: 00079 virtual ref<Connection> connectTo(Address const &connectionSpec); 00080 virtual string registerServer(Server *server, Address const &spec); 00081 virtual string deregisterServer(Server *server, Address const &spec); 00082 00083 ref<FileDescriptor> socketFor(Address const &connectionSpec); 00084 00085 public: 00086 /** 00087 * Creates the single per-application instance of InetTransport, and 00088 * registers it using Transport::registerTransport(). 00089 * 00090 * @param namingServerLocation resolved address of the naming server to use 00091 * @param hostName either "" (for all) or the name of the local interface to bind to 00092 * @param portNumber either 0 (for any) or the number of the TCP port to bind to */ 00093 static void initialise(string const &hostName, 00094 int portNumber) { 00095 if (instance == 0) { 00096 instance = new InetTransport(hostName, 00097 portNumber); 00098 } 00099 } 00100 00101 /** 00102 * Once you have access to the FileDescriptorManager, you can call 00103 * the mainloop(), processFdSet() or poll() methods on it to service 00104 * requests managed by this Transport. */ 00105 static ref<FileDescriptorManager> const &getFdMgr() { 00106 return instance->fdMgr; 00107 } 00108 }; 00109 00110 } 00111 00112 #endif