00001 #ifndef CROMP_CLIENT_CLASS 00002 #define CROMP_CLIENT_CLASS 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : cromp_client * 00007 * Author : Chris Koeritz * 00008 * * 00009 * Purpose: * 00010 * * 00011 * Supplies primitive operations for requesting services of a CROMP-based * 00012 * server application. Tentacles in cromp clients are only used for * 00013 * restoring original objects, but they are required for that. Keep in mind * 00014 * that for communication to be possible, a cromp server and its cromp client * 00015 * must possess tentacles that grok the same infotons. They do not need to * 00016 * be the same objects and probably shouldn't be. It makes sense to * 00017 * implement an unpacking / cloning tentacle and then derive the full-service * 00018 * request processing tentacle from it. * 00019 * * 00020 ******************************************************************************* 00021 * Copyright (c) 2000-$now By Author. This program is free software; you can * 00022 * redistribute it and/or modify it under the terms of the GNU General Public * 00023 * License as published by the Free Software Foundation; either version 2 of * 00024 * the License or (at your option) any later version. This is online at: * 00025 * http://www.fsf.org/copyleft/gpl.html * 00026 * Please send any updates to: fred@gruntose.com * 00027 \*****************************************************************************/ 00028 00029 #include "cromp_common.h" 00030 00031 // forward: 00032 class asynch_connection_thread; 00033 class blank_entity_registry; 00034 class blowfish_crypto; 00035 class encryption_tentacle; 00036 class encryption_wrapper; 00037 class int_roller; 00038 class internet_address; 00039 class time_stamp; 00040 00041 class CROMP_CLASS_STYLE cromp_client : public cromp_common 00042 { 00043 public: 00044 enum constraints { 00045 DEFAULT_MAX_CONNECT_WAIT = 28 * SECOND_ms, 00046 // the server had better connect within this time limit or it will 00047 // be considered missing in action. 00048 DEFAULT_MAX_RESPONSE_WAIT = 4 * MINUTE_ms 00049 // a server should be able to answer in this interval or something is 00050 // really wrong with the system. 00051 }; 00052 00053 cromp_client(const internet_address &destination, 00054 int connection_wait = DEFAULT_MAX_CONNECT_WAIT, 00055 int max_per_ent = DEFAULT_MAX_ENTITY_QUEUE); 00056 // will connect to a cromp_server on the host specified by "destination". 00057 00058 virtual ~cromp_client(); 00059 00060 IMPLEMENT_CLASS_NAME("cromp_client"); 00061 00062 const octopus_entity &entity() const; 00063 // returns our identity within the octopus server. 00064 00065 void enable_encryption(); 00066 // this turns on the encryption. this should be done before the first 00067 // connect or login invocations. once it's enabled, it stays enabled. 00068 00069 void reset(const internet_address &destination, 00070 int connection_wait = DEFAULT_MAX_CONNECT_WAIT, 00071 int max_per_ent = DEFAULT_MAX_ENTITY_QUEUE); 00072 // disconnects from any previous server and reconstructs this client. 00073 // the client will be left in an unconnected and unauthenticated state. 00074 // any pre-existing tentacles will still be hooked up to the object. 00075 // use connect() to re-establish the connection to the server. 00076 00077 outcome connect(); 00078 // attempts to connect to the server. OKAY is returned if this succeeds. 00079 00080 outcome asynch_connect(); 00081 // this is a non-blocking connect. it behaves like connect(), but should 00082 // never take more than a few milliseconds to return. if the client is 00083 // already connected, then nothing is done. otherwise no operations will 00084 // be permitted until the reconnection has succeeded. a call to regular 00085 // connect() cancels the asynchronous connect. 00086 00087 bool connected() const; 00088 // returns true if we think we are connected to the server. 00089 00090 outcome disconnect(); 00091 // disconnects from the cromp server and releases all connection resources. 00092 00093 virtual outcome login(); 00094 // attempts to log in to the server. we must already have connected 00095 // when this is called. 00096 00098 00099 outcome submit(const infoton &request, octopus_request_id &item_id, 00100 int max_tries = 80); 00101 // requests a transaction from the cromp_server described by "request". 00102 // the return value is OKAY if the request was successfully sent or it 00103 // will be another outcome that indicates a failure of transmission. 00104 // the "max_tries" is the number of times to try getting the send out; 00105 // if asynchronous sends are allowed to accumulate, then 1 works here. 00106 00107 outcome acquire(infoton * &response, const octopus_request_id &cmd_id, 00108 int timeout = DEFAULT_MAX_RESPONSE_WAIT); 00109 // attempts to receive a "response" to a previously submitted request 00110 // with the "cmd_id". if "timeout" is non-zero, then the response will 00111 // be awaited for "timeout" milliseconds. otherwise the function returns 00112 // immediately. note that "response" will only be generated properly given 00113 // a tentacle that knows the infoton type received. this requires the 00114 // cromp_client to have tentacles registered for all of the types to be 00115 // exchanged. this can be used for asynchronous retrieval if the timeout 00116 // is zero and one knows a previously requested "cmd_id". 00117 00119 00120 // grittier functions... 00121 00122 outcome synchronous_request(const infoton &to_send, infoton * &received, 00123 octopus_request_id &item_id, 00124 int timeout = DEFAULT_MAX_RESPONSE_WAIT); 00125 // submits the infoton "to_send" and waits for the reply. if there is 00126 // a reply in the allotted time, then "received" is set to that. the 00127 // "item_id" is set to the request id assigned to the request. 00128 00129 octopus_request_id next_id(); 00130 // generates the next identifier. this can be used as a unique identifier 00131 // for derived objects since the id is issued from our server. it is 00132 // not unique across different types of cromp servers though, nor across 00133 // cromp servers running on different hosts. 00134 00135 void decrypt_package_as_needed(outcome &to_return, infoton * &response, 00136 const octopus_request_id &cmd_id); 00137 // this ensures that if the "response" is encrypted, it will be decrypted 00138 // and replaced with the real object intended. this method is invoked 00139 // automatically by acquire(), but if the cromp_common retrieve methods are 00140 // used directly, this should be done to the response before using it. 00141 00142 void keep_alive_pause(int duration = 0, int interval = 40); 00143 // pauses this thread but keeps calling into the cromp support to ensure 00144 // items get delivered or received when possible. the call will snooze 00145 // for at least "duration" milliseconds with individual sleeps being 00146 // allowed the "interval" milliseconds. 00147 00148 private: 00149 bool _encrypting; // true if this connection should be encrypted. 00150 int _connection_wait; // the length of time we'll allow a connection. 00151 mutex *_lock; // protects our data members below. 00152 octopus_entity *_ent; // our identity within the octopus server. 00153 int_roller *_req_id; // the numbering of our nth request is depicted here. 00154 bool _identified; // true if our initial identifier verification succeeded. 00155 bool _authorized; // true if our login was successful. 00156 bool _disallowed; // nothing is allowed right now except asynch connecting. 00157 friend class asynch_connection_thread; // solely so it can use r_p_c method. 00158 asynch_connection_thread *_asynch_connector; // b-ground connection thread. 00159 #ifndef OMIT_CRYPTO_SUPPORT 00160 bool _channel_secured; // true if an encrypted connection has been made. 00161 blowfish_crypto *_crypto; // tracks our key, once we have one. 00162 encryption_tentacle *_encrypt_arm; // processes encryption for us. 00163 #endif 00164 blank_entity_registry *_guardian; // simple security support. 00165 00166 void stop_asynch_thread(); // stops the background connector. 00167 00168 outcome locked_connect(); 00169 // called by the other types of connection processes to get the work done. 00170 00171 octopus_entity randomize_entity() const; 00172 // provides a junk entity for our temporary identifier that we'll use 00173 // until the server gives us a new one. 00174 00175 outcome locked_disconnect(); 00176 // assumes that the client is locked. this is the real disconnect. 00177 00178 #ifndef OMIT_CRYPTO_SUPPORT 00179 bool wrap_infoton(const infoton &request, encryption_wrapper &wrapped); 00180 // wraps an infoton for sending over an encrypted connection. 00181 #endif 00182 }; 00183 00184 #endif 00185
1.5.1