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