Go to the documentation of this file.00001
00002
00003 #ifndef ACE_HTTP_SESSION_CPP
00004 #define ACE_HTTP_SESSION_CPP
00005
00006 #include "ace/INet/HTTP_Session.h"
00007 #include "ace/INet/INet_Log.h"
00008 #include "ace/INet/IOS_util.h"
00009 #include "ace/INet/HTTP_URL.h"
00010 #include "ace/INET_Addr.h"
00011 #include "ace/Event_Handler.h"
00012 #include "ace/Connector.h"
00013 #include "ace/String_Base.h"
00014 #include <istream>
00015 #include <ostream>
00016
00017 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00018
00019 namespace ACE
00020 {
00021 namespace HTTP
00022 {
00023
00024 template <ACE_SYNCH_DECL>
00025 Session_T<ACE_SYNCH_USE>::Session_T (bool keep_alive)
00026 : SessionBase (URL::HTTP_PORT, keep_alive),
00027 connection_ (0),
00028 sock_stream_ (0)
00029 {
00030 INET_TRACE ("ACE_HTTP_Session - ctor");
00031 }
00032
00033 template <ACE_SYNCH_DECL>
00034 Session_T<ACE_SYNCH_USE>::Session_T (const ACE_Time_Value& timeout,
00035 bool keep_alive,
00036 const ACE_Time_Value* alive_timeout)
00037 : SessionBase (URL::HTTP_PORT, timeout, keep_alive, alive_timeout),
00038 connection_ (0),
00039 sock_stream_ (0)
00040 {
00041 INET_TRACE ("ACE_HTTP_Session - ctor");
00042 }
00043
00044 template <ACE_SYNCH_DECL>
00045 Session_T<ACE_SYNCH_USE>::~Session_T ()
00046 {
00047 INET_TRACE ("ACE_HTTP_Session - dtor");
00048 this->close_streams ();
00049 this->close_connection ();
00050 }
00051
00052 template <ACE_SYNCH_DECL>
00053 bool Session_T<ACE_SYNCH_USE>::is_connected () const
00054 {
00055 return this->connection_ && this->connection_->is_connected ();
00056 }
00057
00058 template <ACE_SYNCH_DECL>
00059 bool Session_T<ACE_SYNCH_USE>::connect_i (const ACE_Synch_Options& sync_opt)
00060 {
00061 INET_TRACE ("ACE_HTTP_Session::connect_i");
00062
00063 typedef ACE_Connector<connection_type, ACE_SOCK_CONNECTOR> connector_type;
00064
00065 connector_type connector;
00066
00067 connection_type* new_connection = 0;
00068 ACE_NEW_RETURN (new_connection,
00069 connection_type(sync_opt),
00070 false);
00071 if (connector.connect (new_connection,
00072 ACE_INET_Addr (this->port_,
00073 this->host_.c_str ()),
00074 ACE_Synch_Options (0,this->http_timeout_)) == -1)
00075 {
00076 INET_ERROR (1, (LM_ERROR, DLINFO
00077 ACE_TEXT ("(%d) ACE_HTTP_Session::connect_i - ")
00078 ACE_TEXT ("failed to connect; host=%C, port=%d\n"),
00079 ACE_OS::last_error (), this->host_.c_str (), this->port_));
00080
00081
00082
00083 return false;
00084 }
00085
00086 this->connection_ = new_connection;
00087 this->connection_->reference_counting_policy ().value (
00088 ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
00089
00090 ACE_NEW_NORETURN (this->sock_stream_,
00091 sock_stream_type (this->connection_));
00092 if (this->sock_stream_)
00093 {
00094 this->cannot_reconnect_ = false;
00095 this->reactive_ = sync_opt[ACE_Synch_Options::USE_REACTOR];
00096
00097
00098 this->reconnect_timer_ = this->keep_alive_timeout_;
00099 this->reconnect_countdown_.start ();
00100
00101 return true;
00102 }
00103 else
00104 {
00105 this->close ();
00106 return false;
00107 }
00108 }
00109
00110 template <ACE_SYNCH_DECL>
00111 bool Session_T<ACE_SYNCH_USE>::attach_connection (connection_type* connection)
00112 {
00113 INET_TRACE ("ACE_HTTP_Session::attach_connection");
00114
00115 if (!connection->is_connected ())
00116 return false;
00117
00118 this->close ();
00119
00120 ACE_INET_Addr remote;
00121 connection->peer ().get_remote_addr (remote);
00122 this->host_ = remote.get_host_name ();
00123 this->port_ = remote.get_port_number ();
00124
00125 this->connection_ = connection;
00126 this->connection_->add_reference ();
00127
00128 ACE_NEW_NORETURN (this->sock_stream_,
00129 sock_stream_type (this->connection_));
00130
00131 if (this->sock_stream_)
00132 {
00133 this->keep_alive_ = true;
00134 this->keep_alive_timeout_ = ACE_Time_Value::zero;
00135 this->cannot_reconnect_ = true;
00136 return true;
00137 }
00138 else
00139 {
00140 this->close ();
00141 return false;
00142 }
00143 }
00144
00145 template <ACE_SYNCH_DECL>
00146 void Session_T<ACE_SYNCH_USE>::close_connection ()
00147 {
00148 if (this->sock_stream_)
00149 {
00150 delete this->sock_stream_;
00151 this->sock_stream_ = 0;
00152 }
00153
00154 if (this->connection_)
00155 {
00156
00157
00158 this->connection_->remove_reference ();
00159 this->connection_ = 0;
00160 }
00161 }
00162
00163 template <ACE_SYNCH_DECL>
00164 void Session_T<ACE_SYNCH_USE>::close_i ()
00165 {
00166 INET_TRACE ("ACE_HTTP_Session::close_i");
00167
00168 this->close_connection ();
00169 }
00170
00171 template <ACE_SYNCH_DECL>
00172 std::iostream& Session_T<ACE_SYNCH_USE>::sock_stream ()
00173 {
00174 return *this->sock_stream_;
00175 }
00176
00177 }
00178 }
00179
00180 ACE_END_VERSIONED_NAMESPACE_DECL
00181
00182 #endif