|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Container for sharable endpoints. Sharable endpoints can be |
|
15 found using name (wstring) |
|
16 * |
|
17 */ |
|
18 |
|
19 #include <sstream> |
|
20 #include <errno.h> |
|
21 |
|
22 #include "logger.h" |
|
23 #include "javacommonutils.h" |
|
24 |
|
25 #include "commscontext.h" |
|
26 #include "commsclientendpoint.h" |
|
27 |
|
28 using namespace java::comms; |
|
29 using java::util::ScopedLock; |
|
30 |
|
31 using namespace java::util; |
|
32 |
|
33 CommsContext::CommsContext() |
|
34 { |
|
35 } |
|
36 |
|
37 CommsContext::~CommsContext() |
|
38 { |
|
39 for (endpoints_t::iterator it = mEndpoints.begin(); it != mEndpoints.end(); it++) |
|
40 { |
|
41 delete it->second; |
|
42 } |
|
43 mEndpoints.clear(); |
|
44 |
|
45 for (serverEndpoints_t::iterator it = mServerEndpoints.begin(); it != mServerEndpoints.end(); it++) |
|
46 { |
|
47 delete it->second; |
|
48 } |
|
49 mServerEndpoints.clear(); |
|
50 } |
|
51 |
|
52 #if defined(__SYMBIAN32__) && defined(__WINSCW__) |
|
53 |
|
54 #include <pls.h> |
|
55 CommsContext& CommsContext::getContext() |
|
56 { |
|
57 // Access the PLS of this process |
|
58 CommsContext* context = Pls<CommsContext>(TUid::Uid(0x200211DF)); |
|
59 return *context; |
|
60 } |
|
61 |
|
62 #else |
|
63 |
|
64 static CommsContext* sContext = 0; |
|
65 CommsContext& CommsContext::getContext() |
|
66 { |
|
67 |
|
68 if (sContext == 0) |
|
69 { |
|
70 sContext = new CommsContext(); |
|
71 } |
|
72 return *sContext; |
|
73 } |
|
74 #endif |
|
75 |
|
76 void CommsContext::add(CommsClientEndpoint* aEndpoint, const wstring& aName) |
|
77 { |
|
78 // JELOG2(EJavaComms); |
|
79 ScopedLock lock(mEndpointsMutex); |
|
80 |
|
81 endpoints_t::iterator it = mEndpoints.find(aEndpoint); |
|
82 if (it == mEndpoints.end() && find(aName) == 0) |
|
83 { |
|
84 mEndpoints.insert(std::make_pair(aEndpoint, new wstring(aName))); |
|
85 LOG1WSTR(EJavaComms, EInfo, "Added client endpoint=%s", aName); |
|
86 } |
|
87 else |
|
88 { |
|
89 ELOG1WSTR(EJavaComms, "Add client endpoint failed, endpoint=%s", aName); |
|
90 } |
|
91 } |
|
92 |
|
93 void CommsContext::remove(CommsClientEndpoint* aEndpoint) |
|
94 { |
|
95 // JELOG2(EJavaComms); |
|
96 ScopedLock lock(mEndpointsMutex); |
|
97 |
|
98 endpoints_t::iterator it = mEndpoints.find(aEndpoint); |
|
99 |
|
100 if (it != mEndpoints.end()) |
|
101 { |
|
102 LOG1WSTR(EJavaComms, EInfo, "Removed client endpoint=%s", *(it->second)); |
|
103 delete it->second; |
|
104 mEndpoints.erase(it); |
|
105 } |
|
106 } |
|
107 |
|
108 CommsClientEndpoint* CommsContext::find(const wstring& aName) |
|
109 { |
|
110 // JELOG2(EJavaComms); |
|
111 ScopedLock lock(mEndpointsMutex); |
|
112 |
|
113 CommsClientEndpoint* client = 0; |
|
114 for (endpoints_t::iterator it = mEndpoints.begin(); it != mEndpoints.end(); it++) |
|
115 { |
|
116 if (aName.compare(*(it->second)) == 0) |
|
117 { |
|
118 client = it->first; |
|
119 LOG1WSTR(EJavaComms, EInfo, "Found client endpoint=%s", aName); |
|
120 break; |
|
121 } |
|
122 } |
|
123 return client; |
|
124 } |
|
125 |
|
126 void CommsContext::add(CommsServerEndpoint* aEndpoint, const wstring& aName) |
|
127 { |
|
128 // JELOG2(EJavaComms); |
|
129 ScopedLock lock(mEndpointsMutex); |
|
130 |
|
131 serverEndpoints_t::iterator it = mServerEndpoints.find(aEndpoint); |
|
132 if (it == mServerEndpoints.end() && findServer(aName) == 0) |
|
133 { |
|
134 mServerEndpoints.insert(std::make_pair(aEndpoint, new wstring(aName))); |
|
135 LOG1WSTR(EJavaComms, EInfo, "Added server endpoint=%s", aName); |
|
136 } |
|
137 else |
|
138 { |
|
139 ELOG1WSTR(EJavaComms, "Add server endpoint failed, endpoint=%s", aName); |
|
140 } |
|
141 } |
|
142 |
|
143 void CommsContext::remove(CommsServerEndpoint* aEndpoint) |
|
144 { |
|
145 // JELOG2(EJavaComms); |
|
146 ScopedLock lock(mEndpointsMutex); |
|
147 |
|
148 serverEndpoints_t::iterator it = mServerEndpoints.find(aEndpoint); |
|
149 |
|
150 if (it != mServerEndpoints.end()) |
|
151 { |
|
152 LOG1WSTR(EJavaComms, EInfo, "Removed server endpoint=%s", *(it->second)); |
|
153 delete it->second; |
|
154 mServerEndpoints.erase(it); |
|
155 } |
|
156 } |
|
157 |
|
158 CommsServerEndpoint* CommsContext::findServer(const wstring& aName) |
|
159 { |
|
160 // JELOG2(EJavaComms); |
|
161 ScopedLock lock(mEndpointsMutex); |
|
162 |
|
163 CommsServerEndpoint* server = 0; |
|
164 for (serverEndpoints_t::iterator it = mServerEndpoints.begin(); it != mServerEndpoints.end(); it++) |
|
165 { |
|
166 if (aName.compare(*(it->second)) == 0) |
|
167 { |
|
168 server = it->first; |
|
169 LOG1WSTR(EJavaComms, EInfo, "Found server endpoint=%s", aName); |
|
170 break; |
|
171 } |
|
172 } |
|
173 return server; |
|
174 } |
|
175 |