xref: /trunk/main/binaryurp/source/bridge.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
180     virtual rtl::OUString SAL_CALL getName();
181 
182     virtual rtl::OUString SAL_CALL getDescription();
183 
184     virtual void SAL_CALL dispose();
185 
186     virtual void SAL_CALL addEventListener(
187         com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
188             const & xListener);
189 
190     virtual void SAL_CALL removeEventListener(
191         com::sun::star::uno::Reference< com::sun::star::lang::XEventListener >
192             const & aListener);
193 
194     // Only called from reader_ thread:
195     void sendCommitChangeRequest();
196 
197     // Only called from reader_ thread:
198     void sendProtPropRequest(
199         OutgoingRequest::Kind kind,
200         std::vector< BinaryAny > const & inArguments);
201 
202     void makeReleaseCall(
203         rtl::OUString const & oid,
204         com::sun::star::uno::TypeDescription const & type);
205 
206     void sendRequest(
207         rtl::ByteSequence const & tid, rtl::OUString const & oid,
208         com::sun::star::uno::TypeDescription const & type,
209         com::sun::star::uno::TypeDescription const & member,
210         std::vector< BinaryAny > const & inArguments);
211 
212     void throwException(bool exception, BinaryAny const & value);
213 
214     com::sun::star::uno::Any mapBinaryToCppAny(BinaryAny const & binaryAny);
215 
216     bool becameUnused() const;
217 
218     void terminateWhenUnused(bool unused);
219 
220     typedef
221         std::list<
222             com::sun::star::uno::Reference<
223                 com::sun::star::lang::XEventListener > >
224         Listeners;
225 
226     struct SubStub {
227         com::sun::star::uno::UnoInterfaceReference object;
228         sal_uInt32 references;
229     };
230 
231     typedef std::map< com::sun::star::uno::TypeDescription, SubStub > Stub;
232 
233     typedef std::map< rtl::OUString, Stub > Stubs;
234 
235     enum Mode {
236         MODE_REQUESTED, MODE_REPLY_MINUS1, MODE_REPLY_0, MODE_REPLY_1,
237         MODE_WAIT, MODE_NORMAL, MODE_NORMAL_WAIT };
238 
239     rtl::Reference< BridgeFactory > factory_;
240     rtl::OUString name_;
241     com::sun::star::uno::Reference< com::sun::star::connection::XConnection >
242         connection_;
243     com::sun::star::uno::Reference< com::sun::star::bridge::XInstanceProvider >
244         provider_;
245     com::sun::star::uno::Environment binaryUno_;
246     com::sun::star::uno::Mapping cppToBinaryMapping_;
247     com::sun::star::uno::Mapping binaryToCppMapping_;
248     rtl::ByteSequence protPropTid_;
249     rtl::OUString protPropOid_;
250     com::sun::star::uno::TypeDescription protPropType_;
251     com::sun::star::uno::TypeDescription protPropRequest_;
252     com::sun::star::uno::TypeDescription protPropCommit_;
253     uno_ThreadPool threadPool_;
254     OutgoingRequests outgoingRequests_;
255 
256     osl::Mutex mutex_;
257     Listeners listeners_;
258     rtl::Reference< Writer > writer_;
259     rtl::Reference< Reader > reader_;
260     bool currentContextMode_;
261     Stubs stubs_;
262     std::size_t proxies_;
263     std::size_t calls_;
264     bool normalCall_;
265     std::size_t activeCalls_;
266     osl::Condition passive_;
267         // to guarantee that passive_ is eventually set (to avoid deadlock, see
268         // dispose), activeCalls_ only counts those calls for which it can be
269         // guaranteed that incrementActiveCalls is indeed followed by
270         // decrementActiveCalls, without an intervening exception
271     bool terminated_;
272 
273     // Only accessed from reader_ thread:
274     Mode mode_;
275     sal_Int32 random_;
276 };
277 
278 }
279 
280 #endif
281