Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / protocols / ace / INet / HTTP_Session.cpp
blob80ce9fa5106d8be16fee0b5149b832f2ca80c993
1 #ifndef ACE_HTTP_SESSION_CPP
2 #define ACE_HTTP_SESSION_CPP
4 #include "ace/INet/HTTP_Session.h"
5 #include "ace/INet/INet_Log.h"
6 #include "ace/INet/IOS_util.h"
7 #include "ace/INet/HTTP_URL.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/Event_Handler.h"
10 #include "ace/Connector.h"
11 #include "ace/String_Base.h"
12 #include <istream>
13 #include <ostream>
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 namespace ACE
19 namespace HTTP
21 template <ACE_SYNCH_DECL>
22 Session_T<ACE_SYNCH_USE>::Session_T (bool keep_alive)
23 : SessionBase (URL::HTTP_PORT, keep_alive),
24 connection_ (0),
25 sock_stream_ (0)
27 INET_TRACE ("ACE_HTTP_Session - ctor");
30 template <ACE_SYNCH_DECL>
31 Session_T<ACE_SYNCH_USE>::Session_T (const ACE_Time_Value& timeout,
32 bool keep_alive,
33 const ACE_Time_Value* alive_timeout)
34 : SessionBase (URL::HTTP_PORT, timeout, keep_alive, alive_timeout),
35 connection_ (0),
36 sock_stream_ (0)
38 INET_TRACE ("ACE_HTTP_Session - ctor");
41 template <ACE_SYNCH_DECL>
42 Session_T<ACE_SYNCH_USE>::~Session_T ()
44 INET_TRACE ("ACE_HTTP_Session - dtor");
45 this->close_streams ();
46 this->close_connection ();
49 template <ACE_SYNCH_DECL>
50 bool Session_T<ACE_SYNCH_USE>::is_connected () const
52 return this->connection_ && this->connection_->is_connected ();
55 template <ACE_SYNCH_DECL>
56 bool Session_T<ACE_SYNCH_USE>::connect_i (const ACE_Synch_Options& sync_opt)
58 INET_TRACE ("ACE_HTTP_Session::connect_i");
60 typedef ACE_Connector<connection_type, ACE_SOCK_CONNECTOR> connector_type;
62 connector_type connector;
64 connection_type* new_connection = 0;
65 ACE_NEW_RETURN (new_connection,
66 connection_type(sync_opt),
67 false);
68 if (connector.connect (new_connection,
69 ACE_INET_Addr (this->port_,
70 this->host_.c_str ()),
71 ACE_Synch_Options (0,this->http_timeout_)) == -1)
73 INET_ERROR (1, (LM_ERROR, DLINFO
74 ACE_TEXT ("(%d) ACE_HTTP_Session::connect_i - ")
75 ACE_TEXT ("failed to connect; host=%C, port=%d\n"),
76 ACE_OS::last_error (), this->host_.c_str (), this->port_));
77 // as the connection was dynamically allocated
78 // the connector causes it to be destroyed after
79 // the connection failure
80 return false;
83 this->connection_ = new_connection;
84 this->connection_->reference_counting_policy ().value (
85 ACE_Event_Handler::Reference_Counting_Policy::ENABLED);
87 ACE_NEW_NORETURN (this->sock_stream_,
88 sock_stream_type (this->connection_));
89 if (this->sock_stream_)
91 this->cannot_reconnect_ = false;
92 this->reactive_ = sync_opt[ACE_Synch_Options::USE_REACTOR];
94 // reset reconnect timer
95 this->reconnect_timer_ = this->keep_alive_timeout_;
96 this->reconnect_countdown_.start ();
98 return true;
100 else
102 this->close ();
103 return false;
107 template <ACE_SYNCH_DECL>
108 bool Session_T<ACE_SYNCH_USE>::attach_connection (connection_type* connection)
110 INET_TRACE ("ACE_HTTP_Session::attach_connection");
112 if (!connection->is_connected ())
113 return false;
115 this->close ();
117 ACE_INET_Addr remote;
118 connection->peer ().get_remote_addr (remote);
119 this->host_ = remote.get_host_name ();
120 this->port_ = remote.get_port_number ();
122 this->connection_ = connection;
123 this->connection_->add_reference ();
125 ACE_NEW_NORETURN (this->sock_stream_,
126 sock_stream_type (this->connection_));
128 if (this->sock_stream_)
130 this->keep_alive_ = true;
131 this->keep_alive_timeout_ = ACE_Time_Value::zero;
132 this->cannot_reconnect_ = true;
133 return true;
135 else
137 this->close ();
138 return false;
142 template <ACE_SYNCH_DECL>
143 void Session_T<ACE_SYNCH_USE>::close_connection ()
145 if (this->sock_stream_)
147 delete this->sock_stream_;
148 this->sock_stream_ = 0;
151 if (this->connection_)
153 // this should be the last referece and removing it
154 // causes the connection to be destroyed
155 this->connection_->remove_reference ();
156 this->connection_ = 0;
160 template <ACE_SYNCH_DECL>
161 void Session_T<ACE_SYNCH_USE>::close_i ()
163 INET_TRACE ("ACE_HTTP_Session::close_i");
165 this->close_connection ();
168 template <ACE_SYNCH_DECL>
169 std::iostream& Session_T<ACE_SYNCH_USE>::sock_stream ()
171 return *this->sock_stream_;
177 ACE_END_VERSIONED_NAMESPACE_DECL
179 #endif /* ACE_HTTP_SESSION_CPP */