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