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 _COMMUNI_HXX
25 #define _COMMUNI_HXX
26 
27 #include <svl/svarray.hxx>
28 #include <vos/thread.hxx>
29 #include <vos/mutex.hxx>
30 #include <vcl/timer.hxx>
31 #include <automation/simplecm.hxx>
32 
33 class SvStream;
34 class SvMemoryStream;
35 //class Application;
36 
37 class CommunicationManagerServerAcceptThread;
38 SV_DECL_PTRARR_SORT( CommunicationLinkList, CommunicationLink*, 1, 10 )
39 
40 class MultiCommunicationManager : public CommunicationManager
41 {
42 public:
43 	MultiCommunicationManager( sal_Bool bUseMultiChannel = sal_False );
44 	virtual ~MultiCommunicationManager();
45 	virtual sal_Bool StopCommunication();		// H�lt alle CommunicationLinks an
46 	virtual sal_Bool IsLinkValid( CommunicationLink* pCL );
47 	virtual sal_uInt16 GetCommunicationLinkCount();
48 	virtual CommunicationLinkRef GetCommunicationLink( sal_uInt16 nNr );
49 
DoQuickShutdown(sal_Bool bQuickShutdown=sal_True)50     void DoQuickShutdown( sal_Bool bQuickShutdown = sal_True) { bGracefullShutdown = !bQuickShutdown; }
51 
52 protected:
53 	virtual void CallConnectionOpened( CommunicationLink* pCL );
54 	virtual void CallConnectionClosed( CommunicationLink* pCL );
55 	CommunicationLinkList *ActiveLinks;
56 	CommunicationLinkList *InactiveLinks;		/// Hier sind die CommunicationLinks drin, die sich noch nicht selbst abgemeldet haben.
57 												/// allerdings schon ein StopCommunication gekriegt haben, bzw ein ConnectionTerminated
58 	virtual void DestroyingLink( CommunicationLink *pCL );	// Link tr�gt sich im Destruktor aus
59 
60     sal_Bool bGracefullShutdown;
61 };
62 
63 class CommunicationManagerServer : public MultiCommunicationManager
64 {
65 public:
CommunicationManagerServer(sal_Bool bUseMultiChannel=sal_False)66 	CommunicationManagerServer( sal_Bool bUseMultiChannel = sal_False ):MultiCommunicationManager( bUseMultiChannel ){;}
67 };
68 
69 class CommunicationManagerClient : public MultiCommunicationManager, public ICommunicationManagerClient
70 {
71 public:
72 	CommunicationManagerClient( sal_Bool bUseMultiChannel = sal_False );
73 };
74 
75 class CommunicationLinkViaSocket : public SimpleCommunicationLinkViaSocket, public vos::OThread
76 {
77 public:
78 	CommunicationLinkViaSocket( CommunicationManager *pMan, vos::OStreamSocket *pSocket );
79 	virtual ~CommunicationLinkViaSocket();
80 
81 	virtual sal_Bool IsCommunicationError();
82 	virtual sal_Bool DoTransferDataStream( SvStream *pDataStream, CMProtocol nProtocol = CM_PROTOCOL_OLDSTYLE );
83 
84 	// Diese sind Virtuelle Links!!!!
85 	virtual long ConnectionClosed( void* = NULL );
86 	virtual long DataReceived( void* = NULL );
87 
88 	virtual sal_Bool StopCommunication();
89 
SetPutDataReceivedHdl(Link lPutDataReceived)90     void SetPutDataReceivedHdl( Link lPutDataReceived ){ mlPutDataReceived = lPutDataReceived; }
GetDataReceivedLink()91     Link GetDataReceivedLink () {Link aLink = LINK( this, CommunicationLinkViaSocket, DataReceived ); return aLink;}
92     DECL_LINK( PutDataReceivedHdl, CommunicationLinkViaSocket* );
93 
94 protected:
95 	virtual void SAL_CALL run();
96 
97 	virtual sal_Bool ShutdownCommunication();
98 	sal_uLong nConnectionClosedEventId;
99 	sal_uLong nDataReceivedEventId;
100 	vos::OMutex aMConnectionClosed;	// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
101 	vos::OMutex aMDataReceived;		// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
102 	virtual void WaitForShutdown();
103 
104     DECL_LINK( ShutdownLink, void* );
105    	Timer aShutdownTimer;
106     sal_Bool bShutdownStarted;
107     sal_Bool bDestroying;
108     Link mlPutDataReceived;
109 };
110 
111 class CommunicationManagerServerViaSocket : public CommunicationManagerServer
112 {
113 	friend class CommunicationManagerServerAcceptThread;
114 public:
115     using CommunicationManager::StartCommunication;
116 
117 	CommunicationManagerServerViaSocket( sal_uLong nPort, sal_uInt16 nMaxCon, sal_Bool bUseMultiChannel = sal_False );
118 	virtual ~CommunicationManagerServerViaSocket();
119 
120 	virtual sal_Bool StartCommunication();
121 	virtual sal_Bool StopCommunication();
122 
123 protected:
124 	sal_uLong nPortToListen;
125 	sal_uInt16 nMaxConnections;
126 
127 private:
128 	CommunicationManagerServerAcceptThread *pAcceptThread;
129 	void AddConnection( CommunicationLink *pNewConnection );
130 };
131 
132 class CommunicationManagerServerAcceptThread: public vos::OThread
133 {
134 public:
135 	CommunicationManagerServerAcceptThread( CommunicationManagerServerViaSocket* pServer, sal_uLong nPort, sal_uInt16 nMaxCon = CM_UNLIMITED_CONNECTIONS );
136 	virtual ~CommunicationManagerServerAcceptThread();
GetNewConnection()137 	CommunicationLinkRef GetNewConnection(){ CommunicationLinkRef xTemp = xmNewConnection; xmNewConnection.Clear(); return xTemp; }
138 
139 protected:
140 	virtual void SAL_CALL run();
141 
142 private:
143 	CommunicationManagerServerViaSocket* pMyServer;
144 	vos::OAcceptorSocket *pAcceptorSocket;
145 	sal_uLong nPortToListen;
146 	sal_uInt16 nMaxConnections;
147 	sal_uLong nAddConnectionEventId;
148 	vos::OMutex aMAddConnection;	// Notwendig, da Event verarbeitet werden kann bevor Variable gesetzt ist
CallInfoMsg(InfoString aMsg)149 	void CallInfoMsg( InfoString aMsg ){ pMyServer->CallInfoMsg( aMsg ); }
GetInfoType()150 	CM_InfoType GetInfoType(){ return pMyServer->GetInfoType(); }
151 
152 	// Diese beiden werden zum Transport der Connection vom Thread zum Mainthread verwendet.
153 	CommunicationLinkRef xmNewConnection;
154 	DECL_LINK( AddConnection, void* );
155 };
156 
157 class CommunicationManagerClientViaSocket : public CommunicationManagerClient, CommonSocketFunctions
158 {
159 public:
160     using CommunicationManager::StartCommunication;
161 
162 	CommunicationManagerClientViaSocket( ByteString aHost, sal_uLong nPort, sal_Bool bUseMultiChannel = sal_False );
163 	CommunicationManagerClientViaSocket( sal_Bool bUseMultiChannel = sal_False );
164 	virtual ~CommunicationManagerClientViaSocket();
165 
StartCommunication()166 	virtual sal_Bool StartCommunication(){ return StartCommunication( aHostToTalk, nPortToTalk );}
StartCommunication(ByteString aHost,sal_uLong nPort)167 	virtual sal_Bool StartCommunication( ByteString aHost, sal_uLong nPort ){ return DoStartCommunication( this, (ICommunicationManagerClient*) this, aHost, nPort  );}
168 
169 private:
170 	ByteString aHostToTalk;
171 	sal_uLong nPortToTalk;
172 protected:
CreateCommunicationLink(CommunicationManager * pCM,vos::OConnectorSocket * pCS)173 	virtual CommunicationLink *CreateCommunicationLink( CommunicationManager *pCM, vos::OConnectorSocket *pCS ){ return new CommunicationLinkViaSocket( pCM, pCS ); }
174 };
175 
176 #endif
177