Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX
21 : #define INCLUDED_BINARYURP_SOURCE_BRIDGE_HXX
22 :
23 : #include "sal/config.h"
24 :
25 : #include <cstddef>
26 : #include <list>
27 : #include <map>
28 : #include <vector>
29 :
30 : #include "boost/noncopyable.hpp"
31 : #include "com/sun/star/bridge/XBridge.hpp"
32 : #include "com/sun/star/lang/XComponent.hpp"
33 : #include "com/sun/star/uno/Reference.hxx"
34 : #include "com/sun/star/uno/RuntimeException.hpp"
35 : #include "cppuhelper/implbase2.hxx"
36 : #include "osl/conditn.hxx"
37 : #include "osl/mutex.hxx"
38 : #include "rtl/ref.hxx"
39 : #include "rtl/ustring.hxx"
40 : #include "sal/types.h"
41 : #include "uno/environment.hxx"
42 : #include "uno/mapping.hxx"
43 : #include "uno/threadpool.h"
44 :
45 : #include "outgoingrequest.hxx"
46 : #include "outgoingrequests.hxx"
47 : #include "writer.hxx"
48 :
49 : namespace binaryurp {
50 : class BinaryAny;
51 : class BridgeFactory;
52 : class Proxy;
53 : class Reader;
54 : }
55 : namespace com { namespace sun { namespace star {
56 : namespace bridge { class XInstanceProvider; }
57 : namespace connection { class XConnection; }
58 : namespace lang { class XEventListener; }
59 : namespace uno {
60 : class Any;
61 : class TypeDescription;
62 : class UnoInterfaceReference;
63 : class XInterface;
64 : }
65 : } } }
66 : namespace rtl { class ByteSequence; }
67 :
68 : namespace binaryurp {
69 :
70 : class Bridge:
71 : public cppu::WeakImplHelper2<
72 : com::sun::star::bridge::XBridge, com::sun::star::lang::XComponent >,
73 : private boost::noncopyable
74 : {
75 : public:
76 : Bridge(
77 : rtl::Reference< BridgeFactory > const & factory,
78 : OUString const & name,
79 : com::sun::star::uno::Reference<
80 : com::sun::star::connection::XConnection > const & connection,
81 : com::sun::star::uno::Reference<
82 : com::sun::star::bridge::XInstanceProvider > const & provider);
83 :
84 : void start();
85 :
86 : // Internally waits for all incoming and outgoing remote calls to terminate,
87 : // so must not be called from within such a call; when final is true, also
88 : // joins all remaining threads (reader, writer, and worker threads from the
89 : // thread pool), so must not be called with final set to true from such a
90 : // thread:
91 : void terminate(bool final);
92 :
93 : com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
94 416091 : getConnection() const { return connection_;}
95 :
96 : com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
97 134 : getProvider() const { return provider_;}
98 :
99 138 : com::sun::star::uno::Mapping & getCppToBinaryMapping() { return cppToBinaryMapping_;}
100 :
101 : BinaryAny mapCppToBinaryAny(com::sun::star::uno::Any const & cppAny);
102 :
103 : uno_ThreadPool getThreadPool();
104 :
105 : rtl::Reference< Writer > getWriter();
106 :
107 : com::sun::star::uno::UnoInterfaceReference registerIncomingInterface(
108 : OUString const & oid,
109 : com::sun::star::uno::TypeDescription const & type);
110 :
111 : OUString registerOutgoingInterface(
112 : com::sun::star::uno::UnoInterfaceReference const & object,
113 : com::sun::star::uno::TypeDescription const & type);
114 :
115 : com::sun::star::uno::UnoInterfaceReference findStub(
116 : OUString const & oid,
117 : com::sun::star::uno::TypeDescription const & type);
118 :
119 : void releaseStub(
120 : OUString const & oid,
121 : com::sun::star::uno::TypeDescription const & type);
122 :
123 : void resurrectProxy(Proxy & proxy);
124 :
125 : void revokeProxy(Proxy & proxy);
126 :
127 : void freeProxy(Proxy & proxy);
128 :
129 : void incrementCalls(bool normalCall) throw ();
130 :
131 : void decrementCalls();
132 :
133 : void incrementActiveCalls() throw ();
134 :
135 : void decrementActiveCalls() throw ();
136 :
137 : bool makeCall(
138 : OUString const & oid,
139 : com::sun::star::uno::TypeDescription const & member, bool setter,
140 : std::vector< BinaryAny > const & inArguments, BinaryAny * returnValue,
141 : std::vector< BinaryAny > * outArguments);
142 :
143 : // Only called from reader_ thread:
144 : void sendRequestChangeRequest();
145 :
146 : // Only called from reader_ thread:
147 : void handleRequestChangeReply(
148 : bool exception, BinaryAny const & returnValue);
149 :
150 : // Only called from reader_ thread:
151 : void handleCommitChangeReply(bool exception, BinaryAny const & returnValue);
152 :
153 : // Only called from reader_ thread:
154 : void handleRequestChangeRequest(
155 : rtl::ByteSequence const & tid,
156 : std::vector< BinaryAny > const & inArguments);
157 :
158 : // Only called from reader_ thread:
159 : void handleCommitChangeRequest(
160 : rtl::ByteSequence const & tid,
161 : std::vector< BinaryAny > const & inArguments);
162 :
163 : OutgoingRequest lastOutgoingRequest(rtl::ByteSequence const & tid);
164 :
165 : bool isProtocolPropertiesRequest(
166 : OUString const & oid,
167 : com::sun::star::uno::TypeDescription const & type) const;
168 :
169 : void setCurrentContextMode();
170 :
171 : bool isCurrentContextMode();
172 :
173 : private:
174 : virtual ~Bridge();
175 :
176 : virtual com::sun::star::uno::Reference< com::sun::star::uno::XInterface >
177 : SAL_CALL getInstance(OUString const & sInstanceName)
178 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
179 :
180 : virtual OUString SAL_CALL getName()
181 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
182 :
183 : virtual OUString SAL_CALL getDescription()
184 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
185 :
186 : virtual void SAL_CALL dispose()
187 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
188 :
189 : virtual void SAL_CALL addEventListener(
190 : com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
191 : const & xListener)
192 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
193 :
194 : virtual void SAL_CALL removeEventListener(
195 : com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
196 : const & aListener)
197 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
198 :
199 : // Only called from reader_ thread:
200 : void sendCommitChangeRequest();
201 :
202 : // Only called from reader_ thread:
203 : void sendProtPropRequest(
204 : OutgoingRequest::Kind kind,
205 : std::vector< BinaryAny > const & inArguments);
206 :
207 : void makeReleaseCall(
208 : OUString const & oid,
209 : com::sun::star::uno::TypeDescription const & type);
210 :
211 : void sendRequest(
212 : rtl::ByteSequence const & tid, OUString const & oid,
213 : com::sun::star::uno::TypeDescription const & type,
214 : com::sun::star::uno::TypeDescription const & member,
215 : std::vector< BinaryAny > const & inArguments);
216 :
217 : void throwException(bool exception, BinaryAny const & value);
218 :
219 : com::sun::star::uno::Any mapBinaryToCppAny(BinaryAny const & binaryAny);
220 :
221 : bool becameUnused() const;
222 :
223 : void terminateWhenUnused(bool unused);
224 :
225 : // Must only be called with mutex_ locked:
226 : void checkDisposed();
227 :
228 : typedef
229 : std::list<
230 : com::sun::star::uno::Reference<
231 : com::sun::star::lang::XEventListener > >
232 : Listeners;
233 :
234 : struct SubStub;
235 :
236 : typedef std::map< com::sun::star::uno::TypeDescription, SubStub > Stub;
237 :
238 : typedef std::map< OUString, Stub > Stubs;
239 :
240 : enum State { STATE_INITIAL, STATE_STARTED, STATE_TERMINATED, STATE_FINAL };
241 :
242 : enum Mode {
243 : MODE_REQUESTED, MODE_REPLY_MINUS1, MODE_REPLY_0, MODE_REPLY_1,
244 : MODE_WAIT, MODE_NORMAL, MODE_NORMAL_WAIT };
245 :
246 : rtl::Reference< BridgeFactory > factory_;
247 : OUString name_;
248 : com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
249 : connection_;
250 : com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
251 : provider_;
252 : com::sun::star::uno::Environment binaryUno_;
253 : com::sun::star::uno::Mapping cppToBinaryMapping_;
254 : com::sun::star::uno::Mapping binaryToCppMapping_;
255 : rtl::ByteSequence protPropTid_;
256 : OUString protPropOid_;
257 : com::sun::star::uno::TypeDescription protPropType_;
258 : com::sun::star::uno::TypeDescription protPropRequest_;
259 : com::sun::star::uno::TypeDescription protPropCommit_;
260 : OutgoingRequests outgoingRequests_;
261 : osl::Condition passive_;
262 : // to guarantee that passive_ is eventually set (to avoid deadlock, see
263 : // dispose), activeCalls_ only counts those calls for which it can be
264 : // guaranteed that incrementActiveCalls is indeed followed by
265 : // decrementActiveCalls, without an intervening exception
266 : osl::Condition terminated_;
267 :
268 : osl::Mutex mutex_;
269 : State state_;
270 : Listeners listeners_;
271 : uno_ThreadPool threadPool_;
272 : rtl::Reference< Writer > writer_;
273 : rtl::Reference< Reader > reader_;
274 : bool currentContextMode_;
275 : Stubs stubs_;
276 : std::size_t proxies_;
277 : std::size_t calls_;
278 : bool normalCall_;
279 : std::size_t activeCalls_;
280 :
281 : // Only accessed from reader_ thread:
282 : Mode mode_;
283 : sal_Int32 random_;
284 : };
285 :
286 : }
287 :
288 : #endif
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|