Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / utils / logWalker / PeerProcess.cpp
blobd920ef7ad249d9be369c7735d766fa1df1c1d496
1 #include "PeerProcess.h"
2 #include "PeerObject.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/ACE.h"
6 #include "Invocation.h"
7 #include "HostProcess.h"
8 #include "Thread.h"
9 #include "Session.h"
11 Endpoint::Endpoint (void)
12 : addr_ (),
13 host_ (),
14 port_ (),
15 is_localhost_ (false),
16 role_ (ER_UNKNOWN)
20 Endpoint::Endpoint (const char *addr, EndpointRole role)
21 : addr_ (),
22 host_ (),
23 port_ (),
24 is_localhost_ (false),
25 role_ (ER_UNKNOWN)
27 this->assign (addr, role);
30 Endpoint::Endpoint (const Endpoint &other)
32 operator = (other);
35 void
36 Endpoint::assign (const char *addr, EndpointRole role)
38 this->addr_ = addr;
39 this->role_ = 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]";
48 Endpoint&
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_;
56 return *this;
59 bool
60 Endpoint::operator == (const Endpoint &other) const
62 if (this->port_ != other.port_)
63 return false;
64 if (this->is_localhost_ != other.is_localhost_)
65 return false;
66 if (this->is_localhost_)
67 return true;
68 if (this->host_ == other.host_)
69 return true;
70 return Session::is_equivalent (this->host_, other.host_);
73 bool
74 Endpoint::operator < (const Endpoint &other) const
76 if (this->port_ < other.port_)
77 return true;
78 if (this->host_ < other.host_)
79 return true;
80 return false;
83 bool
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)
91 : handle_ (0),
92 client_endpoint_ (addr, is_client ? ER_CLIENT : ER_SERVER),
93 open_offset_ (offset),
94 open_time_ (time),
95 close_offset_ (0),
96 close_time_ ()
100 void
101 Transport::close (size_t offset, const ACE_CString &time)
103 this->close_offset_ = offset;
104 this->close_time_ = time;
107 char *
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++);
113 return ident;
116 PeerProcess::PeerProcess (size_t offset, const ACE_CString &time, bool is_server)
117 : ident_ (0),
118 origin_ (0),
119 owner_ (0),
120 remote_ (0),
121 server_ep_ (),
122 transports_ (),
123 last_transport_ (0),
124 is_server_role_(is_server),
125 ssl_(false),
126 origin_offset_ (offset),
127 objects_ (),
128 invocations_ (),
129 object_by_index_ (),
130 first_time_ (time)
132 this->ident_ = PeerProcess::nextIdent(is_server);
135 PeerProcess::~PeerProcess (void)
137 delete [] ident_;
138 while (this->invocations_.size())
140 Invocation *head = this->invocations_.delete_head();
141 delete head;
143 for (PeerObjectTable::ITERATOR i = objects_.begin(); i != objects_.end(); i++)
145 PeerObjectTable::ENTRY *entry;
146 if (i.next(entry) == 0)
147 break;
148 delete entry->item();
152 void
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);
158 bool
159 PeerProcess::match_server_addr (const Endpoint &addr) const
161 return this->server_ep_ == addr;
164 const Endpoint &
165 PeerProcess::server_addr (void) const
167 return this->server_ep_;
170 const Endpoint &
171 PeerProcess::last_client_addr (void) const
173 return this->last_transport_->client_endpoint_;
176 bool
177 PeerProcess::is_server (void) const
179 return this->is_server_role_;
182 size_t
183 PeerProcess::offset (void) const
185 return this->origin_offset_;
188 void
189 PeerProcess::ssl (bool is_ssl)
191 this->ssl_ = is_ssl;
194 void
195 PeerProcess::add_transport (Transport *t)
197 this->last_transport_ = t;
198 this->transports_.insert_tail (t);
201 Transport *
202 PeerProcess::last_transport (void)
204 return this->last_transport_;
207 Transport *
208 PeerProcess::find_transport (long handle)
210 Transport *t = 0;
211 for (ACE_DLList_Reverse_Iterator<Transport> i(this->transports_);
212 !i.done();
213 i.advance())
215 i.next(t);
216 if (t->handle_ == handle)
218 return t;
221 return 0;
224 void
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
230 // instance
231 if (this->is_server_role_)
232 this->remote_ = session->find_host(this->server_ep_, true);
233 else
235 Transport *t = 0;
236 this->transports_.get(t,0);
237 if (t != 0)
238 this->remote_ = session->find_host(t->client_endpoint_, false);
242 const char *
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();
252 return this->ident_;
255 void
256 PeerProcess::split_filename (char *buffer, size_t len) const
258 ACE_OS::snprintf (buffer, len, "%s.txt", this->ident_);
261 PeerObject *
262 PeerProcess::object_for (const char *oid, size_t len)
264 PeerObject *po = 0;
265 u_long key = ACE::hash_pjw (oid,len);
266 int result = objects_.find(key, po);
267 if (result == -1)
269 long index = static_cast<long>(objects_.current_size());
270 char alias[20];
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);
276 return po;
279 Invocation *
280 PeerProcess::new_invocation (size_t req_id, Thread *thr)
282 if (this->find_invocation (req_id, thr->active_handle()) != 0)
283 return 0;
284 Invocation *inv = new Invocation (this, thr, req_id);
285 this->invocations_.insert_tail(inv);
286 thr->add_invocation (inv);
287 return inv;
290 Invocation *
291 PeerProcess::find_invocation (size_t req_id, long handle)
293 Invocation *inv = 0;
294 for (ACE_DLList_Reverse_Iterator<Invocation> i(this->invocations_);
295 !i.done();
296 i.advance())
298 i.next(inv);
299 if (inv->request_id() == req_id &&
300 inv->handle() == handle)
302 return inv;
306 return 0;
309 Invocation *
310 PeerProcess::find_invocation_size (size_t len)
312 Invocation *inv = 0;
313 for (ACE_DLList_Reverse_Iterator<Invocation> i(this->invocations_);
314 !i.done();
315 i.advance())
317 i.next(inv);
318 if (!inv->message_complete() && inv->expected_size() == len)
320 return inv;
323 return 0;
326 void
327 PeerProcess::set_owner (HostProcess *hp)
329 this->owner_ = hp;
332 HostProcess *
333 PeerProcess::owner (void)
335 return this->owner_;
338 void
339 PeerProcess::dump_summary (ostream &strm)
341 size_t num_transports = this->transports_.size();
343 if (this->remote_)
344 strm << " " << remote_->proc_name();
345 else
346 strm << " peer process " << this->ident_;
347 strm << " is a ";
348 if (this->ssl_)
349 strm << "secure ";
350 if (this->is_server_role_)
351 strm << "server at ";
352 else
353 strm << "client to ";
354 strm << this->server_ep_.host_ << ":" << this->server_ep_.port_ << endl;
355 strm << " ";
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";
362 strm << endl;
363 for (ACE_DLList_Iterator<Transport> i(this->transports_);
364 !i.done();
365 i.advance())
367 Transport *tran = 0;
368 i.next(tran);
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_;
375 strm << endl;
377 strm << endl;
380 void
381 PeerProcess::dump_object_detail (ostream &strm)
383 strm << this->objects_.current_size()
384 << " Objects referenced";
385 if (this->is_server_role_)
386 strm << " in ";
387 else
388 strm << " by ";
389 if (this->remote_)
390 strm << remote_->proc_name();
391 else
392 strm << "peer process " << this->ident_;
393 strm << ":" << endl;
394 size_t count_inv = 0;
395 for (ObjectByIndex::ITERATOR i = this->object_by_index_.begin();
396 !i.done();
397 i.advance())
399 ObjectByIndex::ENTRY *entry = 0;
400 i.next (entry);
401 PeerObject *obj = entry->item();
402 obj->dump_detail (strm);
403 count_inv += obj->num_invocations();
405 strm << "Total of " << count_inv << " invocations " << endl;
408 void
409 PeerProcess::dump_invocation_detail (ostream &strm)
411 strm << "\n " << this->invocations_.size() << " Invocations ";
412 strm << (this->is_server_role_ ? "to " : "from ");
413 if (this->remote_)
414 strm << remote_->proc_name();
415 else
416 strm << "peer process " << this->ident_;
417 strm << ":" << endl;
418 Invocation *inv = 0;
419 bool show_handle = this->transports_.size() > 1;
420 for (ACE_DLList_Iterator<Invocation> i(this->invocations_);
421 !i.done();
422 i.advance())
424 i.next(inv);
425 inv->dump_detail(strm, 0, Invocation::Dump_Thread, show_handle);