connection.cpp

Go to the documentation of this file.
00001 #ifndef CONNECTION_IMPLEMENTATION_FILE
00002 #define CONNECTION_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : connection                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "connection.h"
00019 
00020 #include <basis/istring.h>
00021 #include <basis/mutex.h>
00022 
00023 #undef LOG
00024 #define LOG(to_print) CLASS_EMERGENCY_LOG(diagnostic, to_print)
00025 
00026 connection::connection()
00027 : local_transport_id(0),
00028   real_transport_id(0),
00029   remote_host(),
00030   remote_transport_id(0),
00031   remote_connection_id(0),
00032   alive(false),
00033   liveness(),
00034   _the_state(DISCONNECTED),
00035   _state_change_time()
00036 {
00037   low_level[PRIMARY_ID] = 0;
00038   low_level[SECONDARY_ID] = 0;
00039 }
00040 
00041 time_stamp connection::last_state_change() const { return _state_change_time; }
00042 
00043 connection::states connection::state() const { return _the_state; }
00044 
00045 void connection::conn_id(const connection_id &new_id)
00046 { assign_unique_id(new_id.raw_id()); }
00047 
00048 istring connection::catalogable_name() const
00049 { return istring(istring::SPRINTF, "connection %d", conn_id().raw_id()); }
00050 
00051 const char *connection::connection_state_name(states to_name)
00052 {
00053   switch (to_name) {
00054     case DISCONNECTED: return "DISCONNECTED";
00055     case AWAITING_A_CLIENT: return "AWAITING_A_CLIENT";
00056     case REACHING_SERVER: return "REACHING_SERVER";
00057     case WHO_IS_CLIENT: return "WHO_IS_CLIENT";
00058     case HELLO_TO_SERVER: return "HELLO_TO_SERVER";
00059     case CONNECTED: return "CONNECTED";
00060     case SENDING_CLOSE: return "SENDING_CLOSE";
00061     case SENDING_CLOSE_ACK: return "SENDING_CLOSE_ACK";
00062     case DONE_CLOSING: return "DONE_CLOSING";
00063     default: return "UnknownState";
00064   }
00065 }
00066 
00067 void connection::state(states to_set)
00068 {
00069   states old_state = _the_state;
00070   _the_state = to_set;
00071   _state_change_time.reset();  // reset time when state changed.
00072   // if they're saying we're connected now and we were not before, then we
00073   // reset the connected_since time stamp.
00074   if ( (to_set == CONNECTED) && (old_state != CONNECTED) )
00075     _connected_since = time_stamp();
00076 }
00077 
00078 time_stamp connection::connected_since() const { return _connected_since; }
00079 
00080 istring connection::text_form() const
00081 {
00082   return istring(istring::SPRINTF, "[%d] %s %s "
00083       "state=%s lowlev_conn_id=%d/%d local_transport_id=%d "
00084       "remote_transport_id=%d real_transport_id=%d state_change=%s addr=",
00085       conn_id().raw_id(), alive? "live" : "dead", 
00086       connected()? (istring("connected ") + _connected_since.text_form()).s()
00087           : "unconnected",
00088       connection_state_name(state()), low_level[PRIMARY_ID],
00089       low_level[SECONDARY_ID], local_transport_id.raw_id(),
00090       remote_transport_id.raw_id(), real_transport_id.raw_id(),
00091       _state_change_time.text_form().s())
00092     + remote_host.text_form();
00093 }
00094 
00095 bool connection::connected() const
00096 {
00097   switch (state()) {
00098     case CONNECTED: return true;
00099 
00100     case SENDING_CLOSE:  // intentional fall-through.
00101     case SENDING_CLOSE_ACK:  // intentional fall-through.
00102     case DISCONNECTED:  // intentional fall-through.
00103     case HELLO_TO_SERVER:  // intentional fall-through.
00104     case WHO_IS_CLIENT:  // intentional fall-through.
00105     case DONE_CLOSING:  // intentional fall-through.
00106     default:
00107       return false;
00108   }
00109 }
00110 
00111 
00112 #endif //CONNECTION_IMPLEMENTATION_FILE
00113 

Generated on Thu Nov 20 04:28:46 2008 for HOOPLE Libraries by  doxygen 1.5.1