Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : *
6 : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : *
8 : * OpenOffice.org - a multi-platform office productivity suite
9 : *
10 : * This file is part of OpenOffice.org.
11 : *
12 : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU Lesser General Public License version 3
14 : * only, as published by the Free Software Foundation.
15 : *
16 : * OpenOffice.org is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU Lesser General Public License version 3 for more details
20 : * (a copy is included in the LICENSE file that accompanied this code).
21 : *
22 : * You should have received a copy of the GNU Lesser General Public License
23 : * version 3 along with OpenOffice.org. If not, see
24 : * <http://www.openoffice.org/license.html>
25 : * for a copy of the LGPLv3 License.
26 : *
27 : ************************************************************************/
28 : #ifndef INCLUDED_EXTENSIONS_SOURCE_PLUGIN_INC_PLUGIN_UNX_MEDIATOR_HXX
29 : #define INCLUDED_EXTENSIONS_SOURCE_PLUGIN_INC_PLUGIN_UNX_MEDIATOR_HXX
30 :
31 : #include <string.h>
32 : #include <tools/link.hxx>
33 : #include <tools/solar.h>
34 : #include <osl/pipe.hxx>
35 : #include <osl/mutex.hxx>
36 : #include <osl/conditn.hxx>
37 : #include <osl/thread.hxx>
38 :
39 : #include <vector>
40 :
41 : struct MediatorMessage
42 : {
43 : sal_uLong m_nID;
44 : sal_uLong m_nBytes;
45 : char* m_pBytes;
46 : char* m_pRun;
47 :
48 : MediatorMessage() : m_nID( 0 ), m_nBytes( 0 ),
49 : m_pBytes( NULL ), m_pRun( NULL ) {}
50 0 : MediatorMessage( sal_uLong nID, sal_uLong nBytes, char* pBytes ) :
51 0 : m_nID( nID ),m_nBytes( nBytes ), m_pRun( NULL )
52 : {
53 0 : m_pBytes = new char[ m_nBytes ];
54 0 : memcpy( m_pBytes, pBytes, (size_t)m_nBytes );
55 0 : }
56 :
57 0 : ~MediatorMessage()
58 : {
59 0 : if( m_pBytes )
60 0 : delete [] m_pBytes;
61 0 : }
62 :
63 : void Set( sal_uLong nBytes, char* pBytes )
64 : {
65 : if( m_pBytes )
66 : delete [] m_pBytes;
67 : m_nBytes = nBytes;
68 : m_pBytes = new char[ m_nBytes ];
69 : memcpy( m_pBytes, pBytes, (size_t)m_nBytes );
70 : }
71 :
72 : sal_uLong ExtractULONG();
73 : char* GetString();
74 : sal_uInt32 GetUINT32();
75 : void* GetBytes( sal_uLong& );
76 0 : void* GetBytes() { sal_uLong nBytes; return GetBytes( nBytes ); }
77 :
78 : void Rewind() { m_pRun = NULL; }
79 : };
80 :
81 : class MediatorListener;
82 :
83 : class Mediator
84 : {
85 : friend class MediatorListener;
86 : protected:
87 : int m_nSocket;
88 :
89 : std::vector<MediatorMessage*> m_aMessageQueue;
90 : osl::Mutex m_aQueueMutex;
91 : osl::Mutex m_aSendMutex;
92 : // only one thread can send a message at any given time
93 : osl::Condition m_aNewMessageCdtn;
94 : MediatorListener* m_pListener;
95 : // thread to fill the queue
96 :
97 : sal_uLong m_nCurrentID;
98 : // will be constantly increased with each message sent
99 : bool m_bValid;
100 :
101 : Link<> m_aConnectionLostHdl;
102 : Link<> m_aNewMessageHdl;
103 : public:
104 : Mediator( int nSocket );
105 : virtual ~Mediator();
106 :
107 : // mark mediator as invalid. No more messages will be processed,
108 : // SendMessage, WaitForMessage, TransactMessage will return immediately
109 : // with error
110 0 : void invalidate() { m_bValid = false; }
111 :
112 : sal_uLong SendMessage( sal_uLong nBytes, const char* pBytes, sal_uLong nMessageID = 0 );
113 : sal_uLong SendMessage( const OString& rMessage, sal_uLong nMessageID = 0 )
114 : {
115 : return SendMessage( rMessage.getLength(), rMessage.getStr(), nMessageID );
116 : }
117 :
118 : bool WaitForMessage( sal_uLong nTimeOut = 5000 );
119 : // timeout in ms
120 : // TRUE: Message came in
121 : // FALSE: timed out
122 : // if timeout is set, WaitForMessage will wait even if there are messages
123 : // in the queue
124 :
125 : virtual MediatorMessage* WaitForAnswer( sal_uLong nMessageID );
126 : // wait for an answer message ( ID >= 1 << 24 )
127 : // the message will be removed from the queue and returned
128 :
129 : MediatorMessage* TransactMessage( sal_uLong nBytes, char* pBytes );
130 : // sends a message and waits for an answer
131 :
132 : MediatorMessage* GetNextMessage( bool bWait = false );
133 :
134 :
135 0 : Link<> SetConnectionLostHdl( const Link<>& rLink )
136 : {
137 0 : Link<> aRet = m_aConnectionLostHdl;
138 0 : m_aConnectionLostHdl = rLink;
139 0 : return aRet;
140 : }
141 :
142 0 : Link<> SetNewMessageHdl( const Link<>& rLink )
143 : {
144 0 : Link<> aRet = m_aNewMessageHdl;
145 0 : m_aNewMessageHdl = rLink;
146 0 : return aRet;
147 : }
148 : };
149 :
150 : class MediatorListener : public osl::Thread
151 : {
152 : friend class Mediator;
153 : private:
154 : Mediator* m_pMediator;
155 : ::osl::Mutex m_aMutex;
156 :
157 : MediatorListener( Mediator* );
158 : virtual ~MediatorListener();
159 :
160 : virtual void run() SAL_OVERRIDE;
161 : virtual void onTerminated() SAL_OVERRIDE;
162 : };
163 :
164 : #endif // INCLUDED_EXTENSIONS_SOURCE_PLUGIN_INC_PLUGIN_UNX_MEDIATOR_HXX
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|