srv-hosts.h

Go to the documentation of this file.
00001 /*
00002  * srv-hosts.h
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * Manages host connections.
00031  */
00032 
00033 #ifndef AESOP_SRV_HOSTS_H__
00034 #define AESOP_SRV_HOSTS_H__
00035 
00036 // includes --------------------------------------------------------------------
00037 #include "aesop-base/aesop-base.h"
00038 #include "crypto/crypto.h"
00039 #include "netlib/netlib.h"
00040 #include "netrq/netrq.h"
00041 #include "xdrbuf/xdrbuf.h"
00042 
00043 
00044 namespace aesop {
00045 
00046 
00047 /// \ingroup aesop_srv
00048 /*@{*/
00049 
00050 
00051 /// \defgroup aesop_hosts Host and Connection Management
00052 /*@{*/
00053 
00054 typedef netlib::conn_id_t conn_id_t;
00055 
00056 
00057 /// single struct that contains all (exposed) remote host state
00058 struct host_rec_t {
00059         host_rec_t(void) throw() { this->clear(); }
00060         void clear(void) throw() {
00061                         mode = eHostMode_Invalid;
00062                         udpPort = 0;
00063                         udpConn = 0;
00064                         tcpConn = 0;
00065                         udpToken = 0;
00066                         lastUdpReceived = 0;
00067                         publicKey = NULL;
00068                         desKey = NULL;
00069                         outbuf = NULL;
00070                         netQueue = NULL;
00071                 }
00072         bool isValid(void) const throw() {
00073                         // NOTE: can be valid without TCP connection!
00074                         return (eHostMode_Invalid != mode &&
00075                                 udpConn &&
00076                                 udpToken &&
00077                                 publicKey &&
00078                                 desKey &&
00079                                 outbuf &&
00080                                 netQueue);
00081                 }
00082 
00083         // data fields
00084         eHostMode       mode;           // current view of this host
00085         int             udpPort;        // remote UDP listening port
00086         conn_id_t       udpConn;        // connection to remote UDP
00087         conn_id_t       tcpConn;        // connection to remote TCP
00088         long            udpToken;       // token we use to communicate
00089         dword_t         lastUdpReceived;// last client clock we saw
00090         smart_ptr<crypto::RSAPublicKey> publicKey; // client's public key
00091         smart_ptr<crypto::DESKey> desKey; // des key associated with client
00092         smart_ptr<xdrbuf::Output> outbuf; // output xdr buffer
00093         smart_ptr<netrq::Queue> netQueue; // network request queue
00094 };
00095 
00096 
00097 /// Object that helps the server manage host connections.
00098 /// NOTE: (to implementers) all methods should be threadsafe!
00099 class HostManager {
00100 public:
00101         // helper classes ------------------------------------------------------
00102         struct iterator_t {
00103         private:
00104                 byte_t          opaque[10 * sizeof(long)];
00105         };
00106 
00107         // virtual destructor --------------------------------------------------
00108         virtual ~HostManager(void) throw();
00109 
00110         // aesop::HostManager class interface methods --------------------------
00111         virtual void addHost(IN conn_id_t udpConnID,
00112                                 IN long udpToken,       ///< client's UDP token
00113                                 IN const char * publicKey) = 0;
00114 
00115         /// This is the most efficient lookup (by UDP connection ID)
00116         virtual bool getHostByUdpConnectionID(IN conn_id_t udpConnId,
00117                                 OUT host_rec_t& hr) = 0;
00118 
00119         virtual bool getHostByTcpConnectionID(IN conn_id_t tcpConnId,
00120                                 OUT host_rec_t& hr) = 0;
00121 
00122         virtual void updateHost(IN host_rec_t& hr) = 0;
00123 
00124         virtual bool removeHost(IN conn_id_t hostId) = 0;
00125 
00126         /// requests a new iterator.
00127         virtual void getIterator(OUT iterator_t& iterator) = 0;
00128 
00129         /// returns the host for this iteration, and increments iterator
00130         virtual bool getNextHost(IO iterator_t& iterator,
00131                                 OUT host_rec_t& hr) = 0;
00132 
00133         // static factory methods ----------------------------------------------
00134         static smart_ptr<HostManager> create(IN int udpSize);
00135 };
00136 
00137 
00138 
00139 };      // aesop namespace
00140 
00141 #endif  // AESOP_SRV_HOSTS_H__
00142