|
1 /* |
|
2 * Copyright 2007 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 #include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #include <oasys/util/StringBuffer.h> |
|
22 |
|
23 #include "SessionTable.h" |
|
24 #include "Session.h" |
|
25 #include "naming/EndpointID.h" |
|
26 |
|
27 namespace dtn { |
|
28 |
|
29 //---------------------------------------------------------------------- |
|
30 SessionTable::SessionTable() |
|
31 { |
|
32 } |
|
33 |
|
34 //---------------------------------------------------------------------- |
|
35 Session* |
|
36 SessionTable::lookup_session(const EndpointID& eid) const |
|
37 { |
|
38 SessionMap::const_iterator iter = table_.find(eid); |
|
39 if (iter == table_.end()) |
|
40 return NULL; |
|
41 |
|
42 return iter->second; |
|
43 } |
|
44 |
|
45 //---------------------------------------------------------------------- |
|
46 void |
|
47 SessionTable::add_session(Session* s) |
|
48 { |
|
49 ASSERT(lookup_session(s->eid()) == NULL); |
|
50 |
|
51 table_[s->eid()] = s; |
|
52 } |
|
53 |
|
54 //---------------------------------------------------------------------- |
|
55 Session* |
|
56 SessionTable::get_session(const EndpointID& eid) |
|
57 { |
|
58 Session* session = lookup_session(eid); |
|
59 if (! session) { |
|
60 session = new Session(eid); |
|
61 add_session(session); |
|
62 } |
|
63 return session; |
|
64 } |
|
65 |
|
66 //---------------------------------------------------------------------- |
|
67 void |
|
68 SessionTable::dump(oasys::StringBuffer* buf) const |
|
69 { |
|
70 SessionMap::const_iterator i; |
|
71 SubscriberList::const_iterator j; |
|
72 for (i = table_.begin(); i != table_.end(); ++i) { |
|
73 |
|
74 Session* session = i->second; |
|
75 buf->appendf(" %s upstream=*%p subscribers=", |
|
76 session->eid().c_str(), &session->upstream()); |
|
77 |
|
78 for (j = session->subscribers().begin(); |
|
79 j != session->subscribers().end(); ++j) { |
|
80 |
|
81 const Subscriber& sub = *j; |
|
82 if (sub == session->upstream()) { |
|
83 continue; |
|
84 } |
|
85 buf->appendf("*%p ", &sub); |
|
86 } |
|
87 buf->appendf("\n"); |
|
88 } |
|
89 } |
|
90 |
|
91 } // namespace dtn |
|
92 |