1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _COMMUNI_HXX
29 #define _COMMUNI_HXX
30 
31 #include <svl/svarray.hxx>
32 #include <vos/thread.hxx>
33 #include <vos/mutex.hxx>
34 #include <vcl/timer.hxx>
35 #include <automation/simplecm.hxx>
36 
37 class SvStream;
38 class SvMemoryStream;
39 //class Application;
40 
41 class CommunicationManagerServerAcceptThread;
42 SV_DECL_PTRARR_SORT( CommunicationLinkList, CommunicationLink*, 1, 10 )
43 
44 class MultiCommunicationManager : public CommunicationManager
45 {
46 public:
47 	MultiCommunicationManager( sal_Bool bUseMultiChannel = sal_False );
48 	virtual ~MultiCommunicationManager();
49 	virtual sal_Bool StopCommunication();		// H�lt alle CommunicationLinks an
50 	virtual sal_Bool IsLinkValid( CommunicationLink* pCL );
51 	virtual sal_uInt16 GetCommunicationLinkCount();
52 	virtual CommunicationLinkRef GetCommunicationLink( sal_uInt16 nNr );
53 
54     void DoQuickShutdown( sal_Bool bQuickShutdown = sal_True) { bGracefullShutdown = !bQuickShutdown; }
55 
56 protected:
57 	virtual void CallConnectionOpened( CommunicationLink* pCL );
58 	virtual void CallConnectionClosed( CommunicationLink* pCL );
59 	CommunicationLinkList *ActiveLinks;
60 	CommunicationLinkList *InactiveLinks;		/// Hier sind die CommunicationLinks drin, die sich noch nicht selbst abgemeldet haben.
61 												/// allerdings schon ein StopCommunication gekriegt haben, bzw ein ConnectionTerminated
62 	virtual void DestroyingLink( CommunicationLink *pCL );	// Link tr�gt sich im Destruktor aus
63 
64     sal_Bool bGracefullShutdown;
65 };
66 
67 class CommunicationManagerServer : public MultiCommunicationManager
68 {
69 public:
70 	CommunicationManagerServer( sal_Bool bUseMultiChannel = sal_False ):MultiCommunicationManager( bUseMultiChannel ){;}
71 };
72 
73 class CommunicationManagerClient : public MultiCommunicationManager, public ICommunicationManagerClient
74 {
75 public:
76 	CommunicationManagerClient( sal_Bool bUseMultiChannel = sal_False );
77 };
78 
79 class CommunicationLinkViaSocket : public SimpleCommunicationLinkViaSocket, public vos::OThread
80 {
81 public:
82 	CommunicationLinkViaSocket( CommunicationManager *pMan, vos::OStreamSocket *pSocket );
83 	virtual ~CommunicationLinkViaSocket();
84 
85 	virtual sal_Bool IsCommunicationError();
86 	virtual sal_Bool DoTransferDataStream( SvStream *pDataStream, CMProtocol nProtocol = CM_PROTOCOL_OLDSTYLE );
87 
88 	// Diese sind Virtuelle Links!!!!
89 	virtual long ConnectionClosed( void* = NULL );
90 	virtual long DataReceived( void* = NULL );
91 
92 	virtual sal_Bool StopCommunication();
93 
94     void SetPutDataReceivedHdl( Link lPutDataReceived ){ mlPutDataReceived = lPutDataReceived; }
95     Link GetDataReceivedLink () {Link aLink = LINK( this, CommunicationLinkViaSocket, DataReceived ); return aLink;}
96     DECL_LINK( PutDataReceivedHdl, CommunicationLinkViaSocket* );
97 
98 protected:
99 	virtual void SAL_CALL run();
100 
101 	virtual sal_Bool ShutdownCommunication();
102 	sal_uLong nConnectionClosedEventId;
103 	sal_uLong nDataReceivedEventId;
104 	vos::OMutex aMConnectionClosed;	// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
105 	vos::OMutex aMDataReceived;		// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
106 	virtual void WaitForShutdown();
107 
108     DECL_LINK( ShutdownLink, void* );
109    	Timer aShutdownTimer;
110     sal_Bool bShutdownStarted;
111     sal_Bool bDestroying;
112     Link mlPutDataReceived;
113 };
114 
115 class CommunicationManagerServerViaSocket : public CommunicationManagerServer
116 {
117 	friend class CommunicationManagerServerAcceptThread;
118 public:
119     using CommunicationManager::StartCommunication;
120 
121 	CommunicationManagerServerViaSocket( sal_uLong nPort, sal_uInt16 nMaxCon, sal_Bool bUseMultiChannel = sal_False );
122 	virtual ~CommunicationManagerServerViaSocket();
123 
124 	virtual sal_Bool StartCommunication();
125 	virtual sal_Bool StopCommunication();
126 
127 protected:
128 	sal_uLong nPortToListen;
129 	sal_uInt16 nMaxConnections;
130 
131 private:
132 	CommunicationManagerServerAcceptThread *pAcceptThread;
133 	void AddConnection( CommunicationLink *pNewConnection );
134 };
135 
136 class CommunicationManagerServerAcceptThread: public vos::OThread
137 {
138 public:
139 	CommunicationManagerServerAcceptThread( CommunicationManagerServerViaSocket* pServer, sal_uLong nPort, sal_uInt16 nMaxCon = CM_UNLIMITED_CONNECTIONS );
140 	virtual ~CommunicationManagerServerAcceptThread();
141 	CommunicationLinkRef GetNewConnection(){ CommunicationLinkRef xTemp = xmNewConnection; xmNewConnection.Clear(); return xTemp; }
142 
143 protected:
144 	virtual void SAL_CALL run();
145 
146 private:
147 	CommunicationManagerServerViaSocket* pMyServer;
148 	vos::OAcceptorSocket *pAcceptorSocket;
149 	sal_uLong nPortToListen;
150 	sal_uInt16 nMaxConnections;
151 	sal_uLong nAddConnectionEventId;
152 	vos::OMutex aMAddConnection;	// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
153 	void CallInfoMsg( InfoString aMsg ){ pMyServer->CallInfoMsg( aMsg ); }
154 	CM_InfoType GetInfoType(){ return pMyServer->GetInfoType(); }
155 
156 	// Diese beiden werden zum Transport der Connection vom Thread zum Mainthread verwendet.
157 	CommunicationLinkRef xmNewConnection;
158 	DECL_LINK( AddConnection, void* );
159 };
160 
161 class CommunicationManagerClientViaSocket : public CommunicationManagerClient, CommonSocketFunctions
162 {
163 public:
164     using CommunicationManager::StartCommunication;
165 
166 	CommunicationManagerClientViaSocket( ByteString aHost, sal_uLong nPort, sal_Bool bUseMultiChannel = sal_False );
167 	CommunicationManagerClientViaSocket( sal_Bool bUseMultiChannel = sal_False );
168 	virtual ~CommunicationManagerClientViaSocket();
169 
170 	virtual sal_Bool StartCommunication(){ return StartCommunication( aHostToTalk, nPortToTalk );}
171 	virtual sal_Bool StartCommunication( ByteString aHost, sal_uLong nPort ){ return DoStartCommunication( this, (ICommunicationManagerClient*) this, aHost, nPort  );}
172 
173 private:
174 	ByteString aHostToTalk;
175 	sal_uLong nPortToTalk;
176 protected:
177 	virtual CommunicationLink *CreateCommunicationLink( CommunicationManager *pCM, vos::OConnectorSocket *pCS ){ return new CommunicationLinkViaSocket( pCM, pCS ); }
178 };
179 
180 #endif
181