1 #include "PeerProcess.h"
2 #include "PeerObject.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_string.h"
6 #include "Invocation.h"
7 #include "HostProcess.h"
11 Endpoint::Endpoint (void)
15 is_localhost_ (false),
20 Endpoint::Endpoint (const char *addr
, EndpointRole role
)
24 is_localhost_ (false),
27 this->assign (addr
, role
);
30 Endpoint::Endpoint (const Endpoint
&other
)
36 Endpoint::assign (const char *addr
, EndpointRole role
)
40 size_t p
= addr_
.rfind (':');
41 this->port_
= addr_
.substring(p
+1);
42 this->host_
= addr_
.substring(0,p
);
44 this->is_localhost_
= this->host_
== "localhost" ||
45 this->host_
== "127.0.0.1" || this->host_
== "[::1]";
49 Endpoint::operator = (const Endpoint
&other
)
51 this->addr_
= other
.addr_
;
52 this->role_
= other
.role_
;
53 this->host_
= other
.host_
;
54 this->port_
= other
.port_
;
55 this->is_localhost_
= other
.is_localhost_
;
60 Endpoint::operator == (const Endpoint
&other
) const
62 if (this->port_
!= other
.port_
)
64 if (this->is_localhost_
!= other
.is_localhost_
)
66 if (this->is_localhost_
)
68 if (this->host_
== other
.host_
)
70 return Session::is_equivalent (this->host_
, other
.host_
);
74 Endpoint::operator < (const Endpoint
&other
) const
76 if (this->port_
< other
.port_
)
78 if (this->host_
< other
.host_
)
84 Endpoint::is_client (void) const
86 return this->role_
!= ER_SERVER
;
90 Transport::Transport (const char *addr
, bool is_client
, size_t offset
, const ACE_CString
&time
)
92 client_endpoint_ (addr
, is_client
? ER_CLIENT
: ER_SERVER
),
93 open_offset_ (offset
),
101 Transport::close (size_t offset
, const ACE_CString
&time
)
103 this->close_offset_
= offset
;
104 this->close_time_
= time
;
108 PeerProcess::nextIdent(bool is_server
)
110 static int count
= 0;
111 char *ident
= new char[15];
112 ACE_OS::sprintf (ident
,"%s_%d", (is_server
? "server" : "client"), count
++);
116 PeerProcess::PeerProcess (size_t offset
, const ACE_CString
&time
, bool is_server
)
124 is_server_role_(is_server
),
126 origin_offset_ (offset
),
132 this->ident_
= PeerProcess::nextIdent(is_server
);
135 PeerProcess::~PeerProcess (void)
138 while (this->invocations_
.size())
140 Invocation
*head
= this->invocations_
.delete_head();
143 for (PeerObjectTable::ITERATOR i
= objects_
.begin(); i
!= objects_
.end(); i
++)
145 PeerObjectTable::ENTRY
*entry
;
146 if (i
.next(entry
) == 0)
148 delete entry
->item();
153 PeerProcess::set_server_addr (const ACE_CString
&addr
)
155 this->server_ep_
.assign (addr
.c_str(), this->is_server_role_
? ER_SERVER
: ER_CLIENT
);
159 PeerProcess::match_server_addr (const Endpoint
&addr
) const
161 return this->server_ep_
== addr
;
165 PeerProcess::server_addr (void) const
167 return this->server_ep_
;
171 PeerProcess::last_client_addr (void) const
173 return this->last_transport_
->client_endpoint_
;
177 PeerProcess::is_server (void) const
179 return this->is_server_role_
;
183 PeerProcess::offset (void) const
185 return this->origin_offset_
;
189 PeerProcess::ssl (bool is_ssl
)
195 PeerProcess::add_transport (Transport
*t
)
197 this->last_transport_
= t
;
198 this->transports_
.insert_tail (t
);
202 PeerProcess::last_transport (void)
204 return this->last_transport_
;
208 PeerProcess::find_transport (long handle
)
211 for (ACE_DLList_Reverse_Iterator
<Transport
> i(this->transports_
);
216 if (t
->handle_
== handle
)
225 PeerProcess::match_hosts (Session
*session
)
227 // This method wants to find the host process that listens
228 // on the server addr. But if the local side is the server
229 // then this wants to find the remote based on the Transport
231 if (this->is_server_role_
)
232 this->remote_
= session
->find_host(this->server_ep_
, true);
236 this->transports_
.get(t
,0);
238 this->remote_
= session
->find_host(t
->client_endpoint_
, false);
243 PeerProcess::id (void) const
245 if (this->remote_
!= 0)
247 const ACE_CString
&pname
= this->remote_
->proc_name();
248 if (pname
.length() > 0)
249 return pname
.c_str();
256 PeerProcess::split_filename (char *buffer
, size_t len
) const
258 ACE_OS::snprintf (buffer
, len
, "%s.txt", this->ident_
);
262 PeerProcess::object_for (const char *oid
, size_t len
)
265 u_long key
= ACE::hash_pjw (oid
,len
);
266 int result
= objects_
.find(key
, po
);
269 long index
= static_cast<long>(objects_
.current_size());
271 ACE_OS::sprintf (alias
, "obj_%ld", index
);
272 po
= new PeerObject(index
, alias
, this);
273 objects_
.bind(key
, po
);
274 object_by_index_
.bind (index
, po
);
280 PeerProcess::new_invocation (size_t req_id
, Thread
*thr
)
282 if (this->find_invocation (req_id
, thr
->active_handle()) != 0)
284 Invocation
*inv
= new Invocation (this, thr
, req_id
);
285 this->invocations_
.insert_tail(inv
);
286 thr
->add_invocation (inv
);
291 PeerProcess::find_invocation (size_t req_id
, long handle
)
294 for (ACE_DLList_Reverse_Iterator
<Invocation
> i(this->invocations_
);
299 if (inv
->request_id() == req_id
&&
300 inv
->handle() == handle
)
310 PeerProcess::find_invocation_size (size_t len
)
313 for (ACE_DLList_Reverse_Iterator
<Invocation
> i(this->invocations_
);
318 if (!inv
->message_complete() && inv
->expected_size() == len
)
327 PeerProcess::set_owner (HostProcess
*hp
)
333 PeerProcess::owner (void)
339 PeerProcess::dump_summary (ostream
&strm
)
341 size_t num_transports
= this->transports_
.size();
344 strm
<< " " << remote_
->proc_name();
346 strm
<< " peer process " << this->ident_
;
350 if (this->is_server_role_
)
351 strm
<< "server at ";
353 strm
<< "client to ";
354 strm
<< this->server_ep_
.host_
<< ":" << this->server_ep_
.port_
<< endl
;
356 if (this->first_time_
.length())
357 strm
<< " first seen at " << this->first_time_
;
359 strm
<< " with " << num_transports
<< " connections, ";
360 strm
<< " referenced " << this->objects_
.current_size()
361 << " objects in " << this->invocations_
.size() << " invocations";
363 for (ACE_DLList_Iterator
<Transport
> i(this->transports_
);
369 strm
<< " connection[" << tran
->handle_
<< "] ";
370 strm
<< (tran
->client_endpoint_
.is_client() ? "to " : "from ");
371 strm
<< tran
->client_endpoint_
.host_
<< ":" << tran
->client_endpoint_
.port_
;
372 strm
<< " created line " << tran
->open_offset_
;
373 if (tran
->close_offset_
)
374 strm
<< " closed line " << tran
->close_offset_
;
381 PeerProcess::dump_object_detail (ostream
&strm
)
383 strm
<< this->objects_
.current_size()
384 << " Objects referenced";
385 if (this->is_server_role_
)
390 strm
<< remote_
->proc_name();
392 strm
<< "peer process " << this->ident_
;
394 size_t count_inv
= 0;
395 for (ObjectByIndex::ITERATOR i
= this->object_by_index_
.begin();
399 ObjectByIndex::ENTRY
*entry
= 0;
401 PeerObject
*obj
= entry
->item();
402 obj
->dump_detail (strm
);
403 count_inv
+= obj
->num_invocations();
405 strm
<< "Total of " << count_inv
<< " invocations " << endl
;
409 PeerProcess::dump_invocation_detail (ostream
&strm
)
411 strm
<< "\n " << this->invocations_
.size() << " Invocations ";
412 strm
<< (this->is_server_role_
? "to " : "from ");
414 strm
<< remote_
->proc_name();
416 strm
<< "peer process " << this->ident_
;
419 bool show_handle
= this->transports_
.size() > 1;
420 for (ACE_DLList_Iterator
<Invocation
> i(this->invocations_
);
425 inv
->dump_detail(strm
, 0, Invocation::Dump_Thread
, show_handle
);