00001 #ifndef CROMP_SERVER_CLASS 00002 #define CROMP_SERVER_CLASS 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : cromp_server * 00007 * Author : Chris Koeritz * 00008 * * 00009 * Purpose: * 00010 * * 00011 * Services request commands from CROMP clients. Derived objects supply * 00012 * the specific services that a CROMP server implements by providing a set * 00013 * of tentacles that handle the requests. These are added directly to the * 00014 * server's octopus base class. * 00015 * * 00016 ******************************************************************************* 00017 * Copyright (c) 2000-$now By Author. This program is free software; you can * 00018 * redistribute it and/or modify it under the terms of the GNU General Public * 00019 * License as published by the Free Software Foundation; either version 2 of * 00020 * the License or (at your option) any later version. This is online at: * 00021 * http://www.fsf.org/copyleft/gpl.html * 00022 * Please send any updates to: fred@gruntose.com * 00023 \*****************************************************************************/ 00024 00025 #include "cromp_common.h" 00026 00027 #include <octopus/octopus.h> 00028 00029 // forward. 00030 class client_dropping_thread; 00031 class connection_management_thread; 00032 class cromp_client_list; 00033 class cromp_client_record; 00034 class cromp_security; 00035 class cromp_transaction; 00036 class encryption_tentacle; 00037 class internet_address; 00038 class ithread; 00039 class thread_cabinet; 00040 class time_stamp; 00041 00042 class CROMP_CLASS_STYLE cromp_server : public cromp_common 00043 { 00044 public: 00045 cromp_server(const internet_address &where, 00046 int accepting_threads = DEFAULT_ACCEPTERS(), 00047 bool instantaneous = true, 00048 int max_per_entity = DEFAULT_MAX_ENTITY_QUEUE); 00049 // creates a server that will open servers on the location "where". the 00050 // number of connections that can be simultaneously handled is passed in 00051 // "accepting_threads". if the "instantaneous" parameter is true, then 00052 // any infotons that need to be handled will be passed to the octopus for 00053 // immediate handling rather than being handled later on a thread. 00054 00055 virtual ~cromp_server(); 00056 00057 IMPLEMENT_CLASS_NAME("cromp_server"); 00058 00059 int clients() const; // returns number of active clients. 00060 00061 static internet_address any_address(int port); 00062 // returns an internet_address that should work on any interface that a 00063 // host has. 00064 00065 internet_address location() const; 00066 // returns the network location where this server is supposed to reside, 00067 // passed in the constructor. 00068 00069 bool instantaneous() const { return _instantaneous; } 00070 // reports whether this server uses immediate handling or delayed handling. 00071 00072 bool enabled() const { return _enabled; } 00073 // reports whether this server has been cranked up or not yet. 00074 00075 void enable_servers(bool encrypt, cromp_security *security = NIL); 00076 // this must be called after construction to start up the object before it 00077 // will accept client requests. if "encrypt" is on, then packets will 00078 // be encrypted and no unencrypted packets will be allowed. if the 00079 // "security" is passed as NIL, then a default security manager is created 00080 // and used. otherwise, the specified "security" object is used and will 00081 // _not_ be destroyed when this object goes away. 00082 00083 void disable_servers(); 00084 // shuts down the server sockets that this object owns and disables the 00085 // operation of this object overall. the next step is destruction. 00086 00087 bool find_entity(const octopus_entity &id, internet_address &found); 00088 // given an "id" that is currently connected, find the network address 00089 // where it originated and put it in "found". true is returned if the 00090 // entity was located. 00091 00092 outcome send_to_client(const octopus_request_id &id, infoton *data); 00093 // blasts a command back to the client identified by the _entity member in 00094 // the "id". this allows the controlling object of the server object to 00095 // asynchronously inject data into the client's incoming stream. the 00096 // client had better know what's going on or this will just lead to 00097 // ignored data that eventually gets trashed. 00098 00099 outcome get_from_client(const octopus_request_id &id, infoton * &data, 00100 int timeout); 00101 // attempts to locate a command with the request "id". if it is found 00102 // within the timeout period, then "data" is set to the command. the 00103 // "data" pointer must be deleted after use. 00104 00105 bool get_sizes(const octopus_entity &id, int &items, int &bytes); 00106 // promoted from our octopus' entity_data_bin in order to provide some 00107 // accounting of what is stored for the "id". 00108 00109 istring responses_text_form() const; 00110 // returns a printout of the responses being held here. 00111 00112 static int DEFAULT_ACCEPTERS(); 00113 // the default number of listening threads. 00114 00115 // internal use only... 00116 00117 void look_for_clients(ithread &requester); 00118 // meant to be called by an arbitrary number of threads that can block 00119 // on accepting a new client. control flow will not return from this 00120 // method until the thread's cancel() method has been called. 00121 00122 void drop_dead_clients(); 00123 // called by a thread to manage dead or dying connections. 00124 00125 bool encrypting() const { return !!_encrypt_arm; } 00126 // true if this object is encrypting transmissions. 00127 00128 infoton *wrap_infoton(infoton * &request, const octopus_entity &ent); 00129 // when we're encrypting, this turns "request" into an encryption_wrapper. 00130 // if NIL is returned, then nothing needed to happen to the "request". 00131 00132 private: 00133 cromp_client_list *_clients; // the active set of clients. 00134 thread_cabinet *_accepters; // the list of accepting threads. 00135 mutex *_list_lock; // protects our lists. 00136 time_stamp *_next_droppage; // times cleanup for dead clients. 00137 bool _instantaneous; // true if the octopus gets driven hard. 00138 internet_address *_where; // where does the server live. 00139 int _accepting_threads; // the number of accepters we keep going. 00140 client_dropping_thread *_dropper; // cleans our lists. 00141 bool _enabled; // records if this is running or not. 00142 encryption_tentacle *_encrypt_arm; // the handler for encryption. 00143 cromp_security *_default_security; // used in lieu of other provider. 00144 00145 outcome accept_one_client(bool wait); 00146 // tries to get just one accepted client. if "wait" is true, then the 00147 // routine will pause until the socket accept returns. 00148 }; 00149 00150 #endif 00151 00152
1.5.1