1*9d1279ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9d1279ecSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9d1279ecSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9d1279ecSAndrew Rist * distributed with this work for additional information
6*9d1279ecSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9d1279ecSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9d1279ecSAndrew Rist * "License"); you may not use this file except in compliance
9*9d1279ecSAndrew Rist * with the License. You may obtain a copy of the License at
10*9d1279ecSAndrew Rist *
11*9d1279ecSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*9d1279ecSAndrew Rist *
13*9d1279ecSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9d1279ecSAndrew Rist * software distributed under the License is distributed on an
15*9d1279ecSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9d1279ecSAndrew Rist * KIND, either express or implied. See the License for the
17*9d1279ecSAndrew Rist * specific language governing permissions and limitations
18*9d1279ecSAndrew Rist * under the License.
19*9d1279ecSAndrew Rist *
20*9d1279ecSAndrew Rist *************************************************************/
21*9d1279ecSAndrew Rist
22*9d1279ecSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_automation.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir #define ENABLE_BYTESTRING_STREAM_OPERATORS
29cdf0e10cSrcweir #include <tools/solar.h>
30cdf0e10cSrcweir #include <automation/simplecm.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <automation/commdefines.hxx>
33cdf0e10cSrcweir #include "packethandler.hxx"
34cdf0e10cSrcweir #include "tcpio.hxx"
35cdf0e10cSrcweir
36cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
37cdf0e10cSrcweir #include <stdio.h>
debug_printf(const char * chars)38cdf0e10cSrcweir void debug_printf( const char *chars )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir static sal_Bool bPrint = (getenv("DEBUG") != NULL);
41cdf0e10cSrcweir if ( bPrint )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir printf( chars );
44cdf0e10cSrcweir fflush( stdout );
45cdf0e10cSrcweir }
46cdf0e10cSrcweir }
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir
CommunicationLink(CommunicationManager * pMan)49cdf0e10cSrcweir CommunicationLink::CommunicationLink( CommunicationManager *pMan )
50cdf0e10cSrcweir : pMyManager(pMan)
51cdf0e10cSrcweir , pServiceData(NULL)
52cdf0e10cSrcweir , nServiceProtocol( 0 )
53cdf0e10cSrcweir , bIsInsideCallback( sal_False )
54cdf0e10cSrcweir , nTotalBytes( 0 )
55cdf0e10cSrcweir , maApplication("Undefined")
56cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
57cdf0e10cSrcweir , bFlag( sal_False )
58cdf0e10cSrcweir , nSomething( 0 )
59cdf0e10cSrcweir #endif
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir
~CommunicationLink()63cdf0e10cSrcweir CommunicationLink::~CommunicationLink()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
66cdf0e10cSrcweir if ( !bFlag ) // bFlag will be set if deletion is expected else we can set a breakpoint
67cdf0e10cSrcweir bFlag = sal_False;
68cdf0e10cSrcweir #endif
69cdf0e10cSrcweir if ( pMyManager )
70cdf0e10cSrcweir pMyManager->DestroyingLink( this );
71cdf0e10cSrcweir }
72cdf0e10cSrcweir
CallInfoMsg(InfoString aMsg)73cdf0e10cSrcweir void CommunicationLink::CallInfoMsg( InfoString aMsg )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir if ( pMyManager )
76cdf0e10cSrcweir pMyManager->InfoMsg( aMsg );
77cdf0e10cSrcweir };
78cdf0e10cSrcweir
GetInfoType()79cdf0e10cSrcweir CM_InfoType CommunicationLink::GetInfoType()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir if ( pMyManager )
82cdf0e10cSrcweir return pMyManager->GetInfoType();
83cdf0e10cSrcweir else
84cdf0e10cSrcweir return CM_NO_TEXT;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
IMPL_LINK(CommunicationLink,ConnectionClosed,void *,EMPTYARG)87cdf0e10cSrcweir IMPL_LINK( CommunicationLink, ConnectionClosed, void*, EMPTYARG )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir if ( pMyManager )
90cdf0e10cSrcweir pMyManager->CallConnectionClosed( this );
91cdf0e10cSrcweir return 1;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir
IMPL_LINK(CommunicationLink,DataReceived,void *,EMPTYARG)94cdf0e10cSrcweir IMPL_LINK( CommunicationLink, DataReceived, void*, EMPTYARG )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir if ( pMyManager )
97cdf0e10cSrcweir pMyManager->CallDataReceived( this );
98cdf0e10cSrcweir return 1;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
DoTransferDataStream(SvStream * pDataStream,CMProtocol nProtocol)101cdf0e10cSrcweir sal_Bool CommunicationLink::DoTransferDataStream( SvStream *pDataStream, CMProtocol nProtocol )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir INFO_MSG( CByteString("S :").Append( GetCommunicationPartner( CM_FQDN ) ),
104cdf0e10cSrcweir CByteString("Daten Senden:").Append( GetCommunicationPartner( CM_FQDN ) ),
105cdf0e10cSrcweir CM_SEND, this );
106cdf0e10cSrcweir sal_Bool bWasError = sal_False;
107cdf0e10cSrcweir
108cdf0e10cSrcweir sal_uInt32 nBuffer;
109cdf0e10cSrcweir nBuffer = pDataStream->SeekRel(0) +1;
110cdf0e10cSrcweir bWasError = pPacketHandler->TransferData( ((SvMemoryStream*)pDataStream)->GetData(), nBuffer, nProtocol ) != C_ERROR_NONE;
111cdf0e10cSrcweir
112cdf0e10cSrcweir if ( bWasError )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir INFO_MSG( CByteString("Send Failed:").Append( GetCommunicationPartner( CM_FQDN ) ),
115cdf0e10cSrcweir CByteString( "Socket wird wegen Fehlers beim Senden geschlossen: ").Append( GetCommunicationPartner( CM_FQDN ) ),
116cdf0e10cSrcweir CM_ERROR, this );
117cdf0e10cSrcweir ShutdownCommunication();
118cdf0e10cSrcweir }
119cdf0e10cSrcweir return !bWasError;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir
TransferDataStream(SvStream * pDataStream,CMProtocol nProtocol)122cdf0e10cSrcweir sal_Bool CommunicationLink::TransferDataStream( SvStream *pDataStream, CMProtocol nProtocol )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir aLastAccess = DateTime();
125cdf0e10cSrcweir nTotalBytes += pDataStream->Seek( STREAM_SEEK_TO_END );
126cdf0e10cSrcweir return DoTransferDataStream( pDataStream, nProtocol );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
SetApplication(const ByteString & aApp)129cdf0e10cSrcweir void CommunicationLink::SetApplication( const ByteString& aApp )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir maApplication = aApp;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
134cdf0e10cSrcweir
SimpleCommunicationLinkViaSocket(CommunicationManager * pMan,vos::OStreamSocket * pSocket)135cdf0e10cSrcweir SimpleCommunicationLinkViaSocket::SimpleCommunicationLinkViaSocket( CommunicationManager *pMan, vos::OStreamSocket *pSocket )
136cdf0e10cSrcweir : CommunicationLink( pMan )
137cdf0e10cSrcweir , aCommunicationPartner()
138cdf0e10cSrcweir , aMyName()
139cdf0e10cSrcweir , pStreamSocket( pSocket )
140cdf0e10cSrcweir , pReceiveStream( NULL )
141cdf0e10cSrcweir , bIsRequestShutdownPending( sal_False )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir pTCPIO = new TCPIO( pStreamSocket );
144cdf0e10cSrcweir pPacketHandler = new PacketHandler( (ITransmiter*) pTCPIO, pTCPIO, pMyManager->IsMultiChannel() );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir
~SimpleCommunicationLinkViaSocket()147cdf0e10cSrcweir SimpleCommunicationLinkViaSocket::~SimpleCommunicationLinkViaSocket()
148cdf0e10cSrcweir {
149cdf0e10cSrcweir delete pPacketHandler;
150cdf0e10cSrcweir pPacketHandler = NULL;
151cdf0e10cSrcweir delete pTCPIO;
152cdf0e10cSrcweir pTCPIO = NULL;
153cdf0e10cSrcweir delete pStreamSocket;
154cdf0e10cSrcweir pStreamSocket = NULL;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
SetStreamSocket(vos::OStreamSocket * pSocket)157cdf0e10cSrcweir void SimpleCommunicationLinkViaSocket::SetStreamSocket( vos::OStreamSocket* pSocket )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir if ( pTCPIO )
160cdf0e10cSrcweir pTCPIO->SetStreamSocket( pSocket );
161cdf0e10cSrcweir pStreamSocket = pSocket;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir
StopCommunication()164cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocket::StopCommunication()
165cdf0e10cSrcweir {
166cdf0e10cSrcweir CommunicationLinkRef rHold(this); // avoid deleting this link before the end of the method
167cdf0e10cSrcweir if ( !IsCommunicationError() ) // Meaning that the Communication is still runnung
168cdf0e10cSrcweir {
169cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
170cdf0e10cSrcweir debug_printf("Sending REQUEST_ShutdownLink\n");
171cdf0e10cSrcweir #endif
172cdf0e10cSrcweir SendHandshake( CH_REQUEST_ShutdownLink );
173cdf0e10cSrcweir }
174cdf0e10cSrcweir WaitForShutdown();
175cdf0e10cSrcweir return sal_True;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
SetFinalRecieveTimeout()178cdf0e10cSrcweir void SimpleCommunicationLinkViaSocket::SetFinalRecieveTimeout()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir if ( !IsCommunicationError() )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir TimeValue aTime = {30, 0}; // 30 seconds
183cdf0e10cSrcweir pStreamSocket->setRecvTimeout( &aTime );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir }
186cdf0e10cSrcweir
IsCommunicationError()187cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocket::IsCommunicationError()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir return !pStreamSocket;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
GetCommunicationPartner(CM_NameType eType)192cdf0e10cSrcweir ByteString SimpleCommunicationLinkViaSocket::GetCommunicationPartner( CM_NameType eType )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir if ( pStreamSocket )
195cdf0e10cSrcweir {
196cdf0e10cSrcweir switch ( eType )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir case CM_DOTTED:
199cdf0e10cSrcweir {
200cdf0e10cSrcweir rtl::OUString aDotted;
201cdf0e10cSrcweir vos::OSocketAddr *pPeerAdr = new vos::OSocketAddr;
202cdf0e10cSrcweir pStreamSocket->getPeerAddr( *pPeerAdr );
203cdf0e10cSrcweir ((vos::OInetSocketAddr*)pPeerAdr)->getDottedAddr( aDotted );
204cdf0e10cSrcweir delete pPeerAdr;
205cdf0e10cSrcweir return ByteString( UniString(aDotted), RTL_TEXTENCODING_UTF8 );
206cdf0e10cSrcweir }
207cdf0e10cSrcweir //break;
208cdf0e10cSrcweir case CM_FQDN:
209cdf0e10cSrcweir {
210cdf0e10cSrcweir if ( !aCommunicationPartner.Len() )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir rtl::OUString aFQDN;
213cdf0e10cSrcweir pStreamSocket->getPeerHost( aFQDN );
214cdf0e10cSrcweir aCommunicationPartner = ByteString( UniString(aFQDN), RTL_TEXTENCODING_UTF8 );
215cdf0e10cSrcweir }
216cdf0e10cSrcweir return aCommunicationPartner;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir //break;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir }
221cdf0e10cSrcweir return CByteString( "Unknown" );
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
GetMyName(CM_NameType eType)224cdf0e10cSrcweir ByteString SimpleCommunicationLinkViaSocket::GetMyName( CM_NameType eType )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir if ( pStreamSocket )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir switch ( eType )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir case CM_DOTTED:
231cdf0e10cSrcweir {
232cdf0e10cSrcweir rtl::OUString aDotted;
233cdf0e10cSrcweir vos::OSocketAddr *pPeerAdr = new vos::OSocketAddr;
234cdf0e10cSrcweir pStreamSocket->getLocalAddr( *pPeerAdr );
235cdf0e10cSrcweir ((vos::OInetSocketAddr*)pPeerAdr)->getDottedAddr( aDotted );
236cdf0e10cSrcweir delete pPeerAdr;
237cdf0e10cSrcweir return ByteString( UniString(aDotted), RTL_TEXTENCODING_UTF8 );
238cdf0e10cSrcweir }
239cdf0e10cSrcweir //break;
240cdf0e10cSrcweir case CM_FQDN:
241cdf0e10cSrcweir {
242cdf0e10cSrcweir if ( !aMyName.Len() )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir rtl::OUString aFQDN;
245cdf0e10cSrcweir pStreamSocket->getLocalHost( aFQDN );
246cdf0e10cSrcweir aMyName = ByteString( UniString(aFQDN), RTL_TEXTENCODING_UTF8 );
247cdf0e10cSrcweir }
248cdf0e10cSrcweir return aMyName;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir //break;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir }
253cdf0e10cSrcweir return CByteString( "Error" );
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
GetBestCommunicationStream()256cdf0e10cSrcweir SvStream* SimpleCommunicationLinkViaSocket::GetBestCommunicationStream()
257cdf0e10cSrcweir {
258cdf0e10cSrcweir SvStream* pStream = new SvMemoryStream;
259cdf0e10cSrcweir // pStream->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
260cdf0e10cSrcweir return pStream;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir
263cdf0e10cSrcweir #define READ_SOCKET( pBuffer, nLength )\
264cdf0e10cSrcweir if ( !bWasError )\
265cdf0e10cSrcweir {bWasError |= pTCPIO->ReceiveBytes( pBuffer, nLength ) != C_ERROR_NONE;}
266cdf0e10cSrcweir
267cdf0e10cSrcweir #define READ_SOCKET_LEN( pBuffer, nLength, nTotal )\
268cdf0e10cSrcweir READ_SOCKET( pBuffer, nLength );\
269cdf0e10cSrcweir if ( !bWasError )\
270cdf0e10cSrcweir {nTotal += nLength;}
271cdf0e10cSrcweir
DoReceiveDataStream()272cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocket::DoReceiveDataStream()
273cdf0e10cSrcweir {
274cdf0e10cSrcweir sal_Bool bWasError = sal_False;
275cdf0e10cSrcweir void* pBuffer = NULL;
276cdf0e10cSrcweir comm_UINT32 nLen;
277cdf0e10cSrcweir bWasError = pPacketHandler->ReceiveData( pBuffer, nLen ) != C_ERROR_NONE;
278cdf0e10cSrcweir if ( !bWasError )
279cdf0e10cSrcweir {
280cdf0e10cSrcweir pReceiveStream = GetBestCommunicationStream();
281cdf0e10cSrcweir DBG_ASSERT( pReceiveStream->IsA() == ID_MEMORYSTREAM, "CommunicationStream is not an SvMemoryStream. Communication has to be reimplemented here!");
282cdf0e10cSrcweir if ( pReceiveStream->IsA() == ID_MEMORYSTREAM )
283cdf0e10cSrcweir ((SvMemoryStream*)pReceiveStream)->SetBuffer( pBuffer, nLen, sal_True, nLen );
284cdf0e10cSrcweir DBG_ASSERT( pReceiveStream, "Datastream is NULL");
285cdf0e10cSrcweir }
286cdf0e10cSrcweir
287cdf0e10cSrcweir return !bWasError;
288cdf0e10cSrcweir }
289cdf0e10cSrcweir
SetApplication(const ByteString & aApp)290cdf0e10cSrcweir void SimpleCommunicationLinkViaSocket::SetApplication( const ByteString& aApp )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir CommunicationLink::SetApplication( aApp );
293cdf0e10cSrcweir SvStream* pData = GetBestCommunicationStream();
294cdf0e10cSrcweir *pData << aApp;
295cdf0e10cSrcweir SendHandshake( CH_SetApplication, pData );
296cdf0e10cSrcweir delete pData;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir
SetNewPacketAsCurrent()299cdf0e10cSrcweir void SimpleCommunicationLinkViaSocket::SetNewPacketAsCurrent()
300cdf0e10cSrcweir {
301cdf0e10cSrcweir pServiceData = pReceiveStream;
302cdf0e10cSrcweir nServiceProtocol = pPacketHandler->GetReceiveProtocol();
303cdf0e10cSrcweir nServiceHeaderType = pPacketHandler->GetReceiveHeaderType();
304cdf0e10cSrcweir }
305cdf0e10cSrcweir
SendHandshake(HandshakeType aHandshakeType,SvStream * pData)306cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocket::SendHandshake( HandshakeType aHandshakeType, SvStream* pData )
307cdf0e10cSrcweir {
308cdf0e10cSrcweir sal_Bool bWasError;
309cdf0e10cSrcweir
310cdf0e10cSrcweir if ( pData )
311cdf0e10cSrcweir {
312cdf0e10cSrcweir sal_uInt32 nBuffer;
313cdf0e10cSrcweir nBuffer = pData->Seek( STREAM_SEEK_TO_END );
314cdf0e10cSrcweir bWasError = !pPacketHandler->SendHandshake( aHandshakeType, ((SvMemoryStream*)pData)->GetData(), nBuffer );
315cdf0e10cSrcweir }
316cdf0e10cSrcweir else
317cdf0e10cSrcweir bWasError = !pPacketHandler->SendHandshake( aHandshakeType );
318cdf0e10cSrcweir
319cdf0e10cSrcweir
320cdf0e10cSrcweir if ( bWasError )
321cdf0e10cSrcweir {
322cdf0e10cSrcweir INFO_MSG( CByteString("Send Failed:").Append( GetCommunicationPartner( CM_FQDN ) ),
323cdf0e10cSrcweir CByteString( "Socket wird wegen Fehlers beim Senden geschlossen: ").Append( GetCommunicationPartner( CM_FQDN ) ),
324cdf0e10cSrcweir CM_ERROR, this );
325cdf0e10cSrcweir ShutdownCommunication();
326cdf0e10cSrcweir }
327cdf0e10cSrcweir else
328cdf0e10cSrcweir { // set new status
329cdf0e10cSrcweir switch ( aHandshakeType )
330cdf0e10cSrcweir {
331cdf0e10cSrcweir case CH_REQUEST_HandshakeAlive:
332cdf0e10cSrcweir break;
333cdf0e10cSrcweir case CH_RESPONSE_HandshakeAlive:
334cdf0e10cSrcweir break;
335cdf0e10cSrcweir case CH_REQUEST_ShutdownLink:
336cdf0e10cSrcweir bIsRequestShutdownPending = sal_True;
337cdf0e10cSrcweir break;
338cdf0e10cSrcweir case CH_ShutdownLink:
339cdf0e10cSrcweir break;
340cdf0e10cSrcweir case CH_SUPPORT_OPTIONS:
341cdf0e10cSrcweir break;
342cdf0e10cSrcweir case CH_SetApplication:
343cdf0e10cSrcweir break;
344cdf0e10cSrcweir default:
345cdf0e10cSrcweir DBG_ERROR("Unknown HandshakeType");
346cdf0e10cSrcweir }
347cdf0e10cSrcweir }
348cdf0e10cSrcweir return !bWasError;
349cdf0e10cSrcweir }
350cdf0e10cSrcweir
SimpleCommunicationLinkViaSocketWithReceiveCallbacks(CommunicationManager * pMan,vos::OStreamSocket * pSocket)351cdf0e10cSrcweir SimpleCommunicationLinkViaSocketWithReceiveCallbacks::SimpleCommunicationLinkViaSocketWithReceiveCallbacks( CommunicationManager *pMan, vos::OStreamSocket *pSocket )
352cdf0e10cSrcweir : SimpleCommunicationLinkViaSocket( pMan, pSocket )
353cdf0e10cSrcweir {
354cdf0e10cSrcweir }
355cdf0e10cSrcweir
~SimpleCommunicationLinkViaSocketWithReceiveCallbacks()356cdf0e10cSrcweir SimpleCommunicationLinkViaSocketWithReceiveCallbacks::~SimpleCommunicationLinkViaSocketWithReceiveCallbacks()
357cdf0e10cSrcweir {
358cdf0e10cSrcweir if ( pMyManager && pMyManager->IsLinkValid( this ) && !bIsRequestShutdownPending )
359cdf0e10cSrcweir StopCommunication();
360cdf0e10cSrcweir }
361cdf0e10cSrcweir
WaitForShutdown()362cdf0e10cSrcweir void SimpleCommunicationLinkViaSocketWithReceiveCallbacks::WaitForShutdown()
363cdf0e10cSrcweir {
364cdf0e10cSrcweir CommunicationLinkRef rHold(this); // avoid deleting this link before the end of the method
365cdf0e10cSrcweir SetFinalRecieveTimeout();
366cdf0e10cSrcweir while ( pMyManager && !IsCommunicationError() )
367cdf0e10cSrcweir ReceiveDataStream();
368cdf0e10cSrcweir }
369cdf0e10cSrcweir
ReceiveDataStream()370cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocketWithReceiveCallbacks::ReceiveDataStream()
371cdf0e10cSrcweir {
372cdf0e10cSrcweir if ( DoReceiveDataStream() )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir SetNewPacketAsCurrent();
375cdf0e10cSrcweir StartCallback();
376cdf0e10cSrcweir DataReceived();
377cdf0e10cSrcweir return sal_True;
378cdf0e10cSrcweir }
379cdf0e10cSrcweir else
380cdf0e10cSrcweir {
381cdf0e10cSrcweir StartCallback();
382cdf0e10cSrcweir ShutdownCommunication();
383cdf0e10cSrcweir return sal_False;
384cdf0e10cSrcweir }
385cdf0e10cSrcweir }
386cdf0e10cSrcweir
ShutdownCommunication()387cdf0e10cSrcweir sal_Bool SimpleCommunicationLinkViaSocketWithReceiveCallbacks::ShutdownCommunication()
388cdf0e10cSrcweir {
389cdf0e10cSrcweir if ( GetStreamSocket() )
390cdf0e10cSrcweir GetStreamSocket()->shutdown();
391cdf0e10cSrcweir
392cdf0e10cSrcweir if ( GetStreamSocket() )
393cdf0e10cSrcweir GetStreamSocket()->close();
394cdf0e10cSrcweir
395cdf0e10cSrcweir vos::OStreamSocket *pTempSocket = GetStreamSocket();
396cdf0e10cSrcweir SetStreamSocket( NULL );
397cdf0e10cSrcweir delete pTempSocket;
398cdf0e10cSrcweir
399cdf0e10cSrcweir ConnectionClosed();
400cdf0e10cSrcweir
401cdf0e10cSrcweir return sal_True;
402cdf0e10cSrcweir }
403cdf0e10cSrcweir
404cdf0e10cSrcweir
405cdf0e10cSrcweir
CommunicationManager(sal_Bool bUseMultiChannel)406cdf0e10cSrcweir CommunicationManager::CommunicationManager( sal_Bool bUseMultiChannel )
407cdf0e10cSrcweir : nInfoType( CM_NONE )
408cdf0e10cSrcweir , bIsCommunicationRunning( sal_False )
409cdf0e10cSrcweir , maApplication("Unknown")
410cdf0e10cSrcweir , bIsMultiChannel( bUseMultiChannel )
411cdf0e10cSrcweir {
412cdf0e10cSrcweir }
413cdf0e10cSrcweir
~CommunicationManager()414cdf0e10cSrcweir CommunicationManager::~CommunicationManager()
415cdf0e10cSrcweir {
416cdf0e10cSrcweir xLastNewLink.Clear();
417cdf0e10cSrcweir }
418cdf0e10cSrcweir
StartCommunication(String aApp,String aParams)419cdf0e10cSrcweir sal_Bool CommunicationManager::StartCommunication( String aApp, String aParams )
420cdf0e10cSrcweir {
421cdf0e10cSrcweir (void) aApp; /* avoid warning about unused parameter */
422cdf0e10cSrcweir (void) aParams; /* avoid warning about unused parameter */
423cdf0e10cSrcweir return sal_False;
424cdf0e10cSrcweir }
425cdf0e10cSrcweir
StartCommunication(ByteString aHost,sal_uLong nPort)426cdf0e10cSrcweir sal_Bool CommunicationManager::StartCommunication( ByteString aHost, sal_uLong nPort )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir (void) aHost; /* avoid warning about unused parameter */
429cdf0e10cSrcweir (void) nPort; /* avoid warning about unused parameter */
430cdf0e10cSrcweir return sal_False;
431cdf0e10cSrcweir }
432cdf0e10cSrcweir
GetMyName(CM_NameType)433cdf0e10cSrcweir ByteString CommunicationManager::GetMyName( CM_NameType )
434cdf0e10cSrcweir {
435cdf0e10cSrcweir rtl::OUString aHostname;
436cdf0e10cSrcweir vos::OSocketAddr::getLocalHostname( aHostname );
437cdf0e10cSrcweir return ByteString( UniString(aHostname), RTL_TEXTENCODING_UTF8 );
438cdf0e10cSrcweir }
439cdf0e10cSrcweir
CallConnectionOpened(CommunicationLink * pCL)440cdf0e10cSrcweir void CommunicationManager::CallConnectionOpened( CommunicationLink* pCL )
441cdf0e10cSrcweir {
442cdf0e10cSrcweir pCL->StartCallback(); // Sollte bereits vor dem Aufruf gerufen werden
443cdf0e10cSrcweir pCL->aStart = DateTime();
444cdf0e10cSrcweir pCL->aLastAccess = pCL->aStart;
445cdf0e10cSrcweir bIsCommunicationRunning = sal_True;
446cdf0e10cSrcweir pCL->SetApplication( GetApplication() );
447cdf0e10cSrcweir
448cdf0e10cSrcweir xLastNewLink = pCL;
449cdf0e10cSrcweir
450cdf0e10cSrcweir INFO_MSG( CByteString("C+:").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
451cdf0e10cSrcweir CByteString("Verbindung aufgebaut: ").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
452cdf0e10cSrcweir CM_OPEN, pCL );
453cdf0e10cSrcweir ConnectionOpened( pCL );
454cdf0e10cSrcweir pCL->FinishCallback();
455cdf0e10cSrcweir }
456cdf0e10cSrcweir
CallConnectionClosed(CommunicationLink * pCL)457cdf0e10cSrcweir void CommunicationManager::CallConnectionClosed( CommunicationLink* pCL )
458cdf0e10cSrcweir {
459cdf0e10cSrcweir pCL->StartCallback(); // Sollte bereits vor dem Aufruf gerufen werden
460cdf0e10cSrcweir pCL->aLastAccess = DateTime();
461cdf0e10cSrcweir
462cdf0e10cSrcweir INFO_MSG( CByteString("C-:").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
463cdf0e10cSrcweir CByteString("Verbindung abgebrochen: ").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
464cdf0e10cSrcweir CM_CLOSE, pCL );
465cdf0e10cSrcweir ConnectionClosed( pCL );
466cdf0e10cSrcweir
467cdf0e10cSrcweir if ( xLastNewLink == pCL )
468cdf0e10cSrcweir xLastNewLink.Clear();
469cdf0e10cSrcweir
470cdf0e10cSrcweir pCL->FinishCallback();
471cdf0e10cSrcweir // delete pCL;
472cdf0e10cSrcweir }
473cdf0e10cSrcweir
CallDataReceived(CommunicationLink * pCL)474cdf0e10cSrcweir void CommunicationManager::CallDataReceived( CommunicationLink* pCL )
475cdf0e10cSrcweir {
476cdf0e10cSrcweir pCL->StartCallback(); // Sollte bereits vor dem Aufruf gerufen werden
477cdf0e10cSrcweir pCL->aLastAccess = DateTime();
478cdf0e10cSrcweir CommunicationLinkRef rHold(pCL); // H�lt den Zeiger bis zum Ende des calls
479cdf0e10cSrcweir
480cdf0e10cSrcweir // should be impossible but happens for mysterious reasons
481cdf0e10cSrcweir if ( !pCL->pServiceData )
482cdf0e10cSrcweir {
483cdf0e10cSrcweir DBG_ERROR( "Datastream is NULL" );
484cdf0e10cSrcweir pCL->FinishCallback();
485cdf0e10cSrcweir return;
486cdf0e10cSrcweir }
487cdf0e10cSrcweir
488cdf0e10cSrcweir
489cdf0e10cSrcweir if ( CH_Handshake == pCL->nServiceHeaderType )
490cdf0e10cSrcweir {
491cdf0e10cSrcweir SvStream *pData = pCL->GetServiceData();
492cdf0e10cSrcweir sal_uInt16 nType;
493cdf0e10cSrcweir pData->SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN ); // Unfortulately it is written this way :((
494cdf0e10cSrcweir *pData >> nType;
495cdf0e10cSrcweir pData->SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
496cdf0e10cSrcweir switch ( nType )
497cdf0e10cSrcweir {
498cdf0e10cSrcweir case CH_REQUEST_HandshakeAlive:
499cdf0e10cSrcweir {
500cdf0e10cSrcweir pCL->SendHandshake( CH_RESPONSE_HandshakeAlive );
501cdf0e10cSrcweir }
502cdf0e10cSrcweir break;
503cdf0e10cSrcweir case CH_REQUEST_ShutdownLink:
504cdf0e10cSrcweir {
505cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
506cdf0e10cSrcweir debug_printf("Sending ShutdownLink\n");
507cdf0e10cSrcweir #endif
508cdf0e10cSrcweir pCL->SendHandshake( CH_ShutdownLink );
509cdf0e10cSrcweir }
510cdf0e10cSrcweir break;
511cdf0e10cSrcweir case CH_ShutdownLink:
512cdf0e10cSrcweir {
513cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
514cdf0e10cSrcweir debug_printf("Executing ShutdownLink\n");
515cdf0e10cSrcweir #endif
516cdf0e10cSrcweir pCL->ShutdownCommunication();
517cdf0e10cSrcweir }
518cdf0e10cSrcweir break;
519cdf0e10cSrcweir case CH_SetApplication:
520cdf0e10cSrcweir {
521cdf0e10cSrcweir ByteString aApplication;
522cdf0e10cSrcweir *pData >> aApplication;
523cdf0e10cSrcweir pCL->CommunicationLink::SetApplication( aApplication );
524cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
525cdf0e10cSrcweir debug_printf( "Setting Application to " );
526cdf0e10cSrcweir debug_printf( aApplication.GetBuffer() );
527cdf0e10cSrcweir debug_printf( "\n" );
528cdf0e10cSrcweir #endif
529cdf0e10cSrcweir }
530cdf0e10cSrcweir break;
531cdf0e10cSrcweir
532cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
533cdf0e10cSrcweir default:
534cdf0e10cSrcweir {
535cdf0e10cSrcweir debug_printf("Unknown Handshake received\n");
536cdf0e10cSrcweir }
537cdf0e10cSrcweir #endif
538cdf0e10cSrcweir }
539cdf0e10cSrcweir delete pData;
540cdf0e10cSrcweir }
541cdf0e10cSrcweir else
542cdf0e10cSrcweir {
543cdf0e10cSrcweir if ( pCL->pServiceData )
544cdf0e10cSrcweir {
545cdf0e10cSrcweir pCL->nTotalBytes += pCL->pServiceData->Seek( STREAM_SEEK_TO_END );
546cdf0e10cSrcweir pCL->pServiceData->Seek( STREAM_SEEK_TO_BEGIN );
547cdf0e10cSrcweir }
548cdf0e10cSrcweir
549cdf0e10cSrcweir INFO_MSG( CByteString("D :").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
550cdf0e10cSrcweir CByteString("Daten Empfangen:").Append( pCL->GetCommunicationPartner( CM_FQDN ) ),
551cdf0e10cSrcweir CM_RECEIVE, pCL );
552cdf0e10cSrcweir DataReceived( pCL );
553cdf0e10cSrcweir }
554cdf0e10cSrcweir delete pCL->GetServiceData();
555cdf0e10cSrcweir pCL->FinishCallback();
556cdf0e10cSrcweir }
557cdf0e10cSrcweir
CallInfoMsg(InfoString aMsg)558cdf0e10cSrcweir void CommunicationManager::CallInfoMsg( InfoString aMsg )
559cdf0e10cSrcweir {
560cdf0e10cSrcweir // Hier wird es wohl kein Housekeeping geben
561cdf0e10cSrcweir InfoMsg( aMsg );
562cdf0e10cSrcweir }
563cdf0e10cSrcweir
SetApplication(const ByteString & aApp,sal_Bool bRunningLinks)564cdf0e10cSrcweir void CommunicationManager::SetApplication( const ByteString& aApp, sal_Bool bRunningLinks )
565cdf0e10cSrcweir {
566cdf0e10cSrcweir maApplication = aApp;
567cdf0e10cSrcweir if ( bRunningLinks )
568cdf0e10cSrcweir {
569cdf0e10cSrcweir sal_uInt16 i;
570cdf0e10cSrcweir for ( i = 0 ; i < GetCommunicationLinkCount() ; i++ )
571cdf0e10cSrcweir GetCommunicationLink( i )->SetApplication( aApp );
572cdf0e10cSrcweir }
573cdf0e10cSrcweir }
574cdf0e10cSrcweir
575cdf0e10cSrcweir
576cdf0e10cSrcweir
SingleCommunicationManager(sal_Bool bUseMultiChannel)577cdf0e10cSrcweir SingleCommunicationManager::SingleCommunicationManager( sal_Bool bUseMultiChannel )
578cdf0e10cSrcweir : CommunicationManager( bUseMultiChannel )
579cdf0e10cSrcweir {
580cdf0e10cSrcweir xActiveLink = NULL;
581cdf0e10cSrcweir pInactiveLink = NULL;
582cdf0e10cSrcweir }
583cdf0e10cSrcweir
~SingleCommunicationManager()584cdf0e10cSrcweir SingleCommunicationManager::~SingleCommunicationManager()
585cdf0e10cSrcweir {
586cdf0e10cSrcweir StopCommunication();
587cdf0e10cSrcweir if ( pInactiveLink )
588cdf0e10cSrcweir pInactiveLink->InvalidateManager();
589cdf0e10cSrcweir }
590cdf0e10cSrcweir
StopCommunication()591cdf0e10cSrcweir sal_Bool SingleCommunicationManager::StopCommunication()
592cdf0e10cSrcweir {
593cdf0e10cSrcweir if ( xActiveLink.Is() )
594cdf0e10cSrcweir {
595cdf0e10cSrcweir sal_Bool bSuccess = xActiveLink->StopCommunication();
596cdf0e10cSrcweir if ( pInactiveLink )
597cdf0e10cSrcweir pInactiveLink->InvalidateManager();
598cdf0e10cSrcweir pInactiveLink = xActiveLink;
599cdf0e10cSrcweir xActiveLink.Clear();
600cdf0e10cSrcweir return bSuccess;
601cdf0e10cSrcweir }
602cdf0e10cSrcweir return sal_True;
603cdf0e10cSrcweir }
604cdf0e10cSrcweir
IsLinkValid(CommunicationLink * pCL)605cdf0e10cSrcweir sal_Bool SingleCommunicationManager::IsLinkValid( CommunicationLink* pCL )
606cdf0e10cSrcweir {
607cdf0e10cSrcweir return &xActiveLink == pCL;
608cdf0e10cSrcweir }
609cdf0e10cSrcweir
GetCommunicationLinkCount()610cdf0e10cSrcweir sal_uInt16 SingleCommunicationManager::GetCommunicationLinkCount()
611cdf0e10cSrcweir {
612cdf0e10cSrcweir return IsCommunicationRunning()?1:0;
613cdf0e10cSrcweir }
614cdf0e10cSrcweir
GetCommunicationLink(sal_uInt16)615cdf0e10cSrcweir CommunicationLinkRef SingleCommunicationManager::GetCommunicationLink( sal_uInt16 )
616cdf0e10cSrcweir {
617cdf0e10cSrcweir return xActiveLink;
618cdf0e10cSrcweir }
619cdf0e10cSrcweir
CallConnectionOpened(CommunicationLink * pCL)620cdf0e10cSrcweir void SingleCommunicationManager::CallConnectionOpened( CommunicationLink* pCL )
621cdf0e10cSrcweir {
622cdf0e10cSrcweir DBG_ASSERT( !xActiveLink.Is(), "Es ist bereits ein CommunicationLink aktiv");
623cdf0e10cSrcweir if ( xActiveLink.Is() )
624cdf0e10cSrcweir {
625cdf0e10cSrcweir if ( pInactiveLink )
626cdf0e10cSrcweir pInactiveLink->InvalidateManager();
627cdf0e10cSrcweir pInactiveLink = xActiveLink;
628cdf0e10cSrcweir xActiveLink->StopCommunication(); // Den alten Link brutal abw�rgen
629cdf0e10cSrcweir }
630cdf0e10cSrcweir xActiveLink = pCL;
631cdf0e10cSrcweir CommunicationManager::CallConnectionOpened( pCL );
632cdf0e10cSrcweir }
633cdf0e10cSrcweir
CallConnectionClosed(CommunicationLink * pCL)634cdf0e10cSrcweir void SingleCommunicationManager::CallConnectionClosed( CommunicationLink* pCL )
635cdf0e10cSrcweir {
636cdf0e10cSrcweir CommunicationManager::CallConnectionClosed( pCL );
637cdf0e10cSrcweir
638cdf0e10cSrcweir DBG_ASSERT( pCL == xActiveLink, "SingleCommunicationManager::CallConnectionClosed mit fremdem Link");
639cdf0e10cSrcweir if ( pInactiveLink )
640cdf0e10cSrcweir pInactiveLink->InvalidateManager();
641cdf0e10cSrcweir pInactiveLink = xActiveLink;
642cdf0e10cSrcweir xActiveLink.Clear();
643cdf0e10cSrcweir bIsCommunicationRunning = sal_False;
644cdf0e10cSrcweir }
645cdf0e10cSrcweir
DestroyingLink(CommunicationLink * pCL)646cdf0e10cSrcweir void SingleCommunicationManager::DestroyingLink( CommunicationLink *pCL )
647cdf0e10cSrcweir {
648cdf0e10cSrcweir pInactiveLink = NULL;
649cdf0e10cSrcweir pCL->InvalidateManager();
650cdf0e10cSrcweir }
651cdf0e10cSrcweir
652cdf0e10cSrcweir
SingleCommunicationManagerClientViaSocket(ByteString aHost,sal_uLong nPort,sal_Bool bUseMultiChannel)653cdf0e10cSrcweir SingleCommunicationManagerClientViaSocket::SingleCommunicationManagerClientViaSocket( ByteString aHost, sal_uLong nPort, sal_Bool bUseMultiChannel )
654cdf0e10cSrcweir : SingleCommunicationManager( bUseMultiChannel )
655cdf0e10cSrcweir , aHostToTalk( aHost )
656cdf0e10cSrcweir , nPortToTalk( nPort )
657cdf0e10cSrcweir {
658cdf0e10cSrcweir }
659cdf0e10cSrcweir
660cdf0e10cSrcweir
SingleCommunicationManagerClientViaSocket(sal_Bool bUseMultiChannel)661cdf0e10cSrcweir SingleCommunicationManagerClientViaSocket::SingleCommunicationManagerClientViaSocket( sal_Bool bUseMultiChannel )
662cdf0e10cSrcweir : SingleCommunicationManager( bUseMultiChannel )
663cdf0e10cSrcweir , aHostToTalk()
664cdf0e10cSrcweir , nPortToTalk( 0 )
665cdf0e10cSrcweir {
666cdf0e10cSrcweir }
667cdf0e10cSrcweir
668cdf0e10cSrcweir
DoStartCommunication(CommunicationManager * pCM,ICommunicationManagerClient * pCMC,ByteString aHost,sal_uLong nPort)669cdf0e10cSrcweir sal_Bool CommonSocketFunctions::DoStartCommunication( CommunicationManager *pCM, ICommunicationManagerClient *pCMC, ByteString aHost, sal_uLong nPort )
670cdf0e10cSrcweir {
671cdf0e10cSrcweir vos::OInetSocketAddr Addr;
672cdf0e10cSrcweir vos::OConnectorSocket *pConnSocket;
673cdf0e10cSrcweir
674cdf0e10cSrcweir Addr.setAddr( rtl::OUString( UniString( aHost, RTL_TEXTENCODING_UTF8 ) ) );
675cdf0e10cSrcweir Addr.setPort( nPort );
676cdf0e10cSrcweir
677cdf0e10cSrcweir TimeValue aTV;
678cdf0e10cSrcweir aTV.Seconds = 10; // Warte 10 Sekunden
679cdf0e10cSrcweir aTV.Nanosec = 0;
680cdf0e10cSrcweir do
681cdf0e10cSrcweir {
682cdf0e10cSrcweir pConnSocket = new vos::OConnectorSocket();
683cdf0e10cSrcweir pConnSocket->setTcpNoDelay( 1 );
684cdf0e10cSrcweir if ( pConnSocket->connect( Addr, &aTV ) == vos::ISocketTypes::TResult_Ok )
685cdf0e10cSrcweir {
686cdf0e10cSrcweir pConnSocket->setTcpNoDelay( 1 );
687cdf0e10cSrcweir
688cdf0e10cSrcweir pCM->CallConnectionOpened( CreateCommunicationLink( pCM, pConnSocket ) );
689cdf0e10cSrcweir return sal_True;
690cdf0e10cSrcweir }
691cdf0e10cSrcweir else
692cdf0e10cSrcweir delete pConnSocket;
693cdf0e10cSrcweir
694cdf0e10cSrcweir } while ( pCMC->RetryConnect() );
695cdf0e10cSrcweir
696cdf0e10cSrcweir return sal_False;
697cdf0e10cSrcweir }
698cdf0e10cSrcweir
699