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