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