1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: commtest.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <vcl/svapp.hxx>
34 #include <vcl/wrkwin.hxx>
35 #include <vcl/toolbox.hxx>
38 #include <tools/simplecm.hxx>
39 #include "communi.hxx"
40 #include "brooker.hxx"
41 //#include <tools/bcst.hxx>
43 #include "commtest.hrc"
47 #define TCP_PORT 17612
49 #define CUniString( constAsciiStr ) UniString( RTL_CONSTASCII_USTRINGPARAM ( constAsciiStr ) )
52 class PacketSender
: public Timer
55 CommunicationLinkRef mxCL
;
58 PacketSender( ULONG nDelay
, SvStream
* pData
, CommunicationLink
* pCL
);
59 virtual void Timeout();
62 PacketSender::PacketSender( ULONG nDelay
, SvStream
* pData
, CommunicationLink
* pCL
)
70 void PacketSender::Timeout()
72 mxCL
->TransferDataStream( mpData
);
79 class DelayedDeleter
: public Timer
81 CommunicationManager
*mpManager
;
84 DelayedDeleter( ULONG nDelay
, CommunicationManager
*pManager
);
85 virtual void Timeout();
88 DelayedDeleter::DelayedDeleter( ULONG nDelay
, CommunicationManager
*pManager
)
89 : mpManager( pManager
)
95 void DelayedDeleter::Timeout()
104 class CommunicationTester
: public Application
106 DECL_LINK( TBClick
, ToolBox
* );
107 DECL_LINK( DataReceived
, CommunicationLink
* );
108 DECL_LINK( ConnectionOpened
, CommunicationLink
* );
109 DECL_LINK( ConnectionClosed
, CommunicationLink
* );
112 CommunicationManager
*pClientTcp
, *pServerTcp
;
113 InformationBroadcaster
*pBCSTSend
;
114 InformationBroadcaster
*pBCSTListen
;
115 InformationBrooker
*pBCSTBrooker
;
117 CommunicationTester();
122 CommunicationTester IchSelber
;
124 CommunicationTester::CommunicationTester()
128 , pBCSTListen( NULL
)
129 , pBCSTBrooker( NULL
)
132 void CommunicationTester::Main()
134 ResMgr
*pRes
= ResMgr::CreateResMgr( "commtest" );
135 Resource::SetResManager( pRes
);
136 WorkWindow
aWW( NULL
, WB_APP
| WB_STDWORK
);
138 ToolBox
aTB( &aWW
, ResId( TBMenu
) );
141 aTB
.SetFloatingMode( TRUE
);
142 aTB
.SetFloatingMode( FALSE
);
143 aTB
.SetClickHdl( LINK( this, CommunicationTester
, TBClick
) );
148 #define SWITCH( pManager, ManagerClass ) \
152 pManager->StopCommunication(); \
153 new DelayedDeleter( 1000, pManager ); \
154 pTB->SetItemState( pTB->GetCurItemId(), STATE_NOCHECK ); \
159 pManager = new ManagerClass; \
160 pManager->SetConnectionOpenedHdl( LINK( this, CommunicationTester, ConnectionOpened ) );\
161 pManager->SetConnectionClosedHdl( LINK( this, CommunicationTester, ConnectionClosed ) );\
162 pManager->SetDataReceivedHdl( LINK( this, CommunicationTester, DataReceived ) );\
163 pTB->SetItemState( pTB->GetCurItemId(), STATE_CHECK ); \
168 IMPL_LINK( CommunicationTester
, TBClick
, ToolBox
*, pTB
)
170 switch ( pTB
->GetCurItemId() )
174 SWITCH( pServerTcp
, CommunicationManagerServerViaSocket( TCP_PORT
, (USHORT
) 32000 ) );
176 pServerTcp
->StartCommunication(); // Am Port horchen
181 SWITCH( pClientTcp
, CommunicationManagerClientViaSocket( "localhost", TCP_PORT
) );
183 pClientTcp
->StartCommunication(); // Eine Verbindung aufbauen
195 pBCSTBrooker
= new InformationBrooker();
208 pBCSTListen
= new InformationBroadcaster();
215 pBCSTSend
->Broadcast( BCST_CAT_PL2X
, "Message: BCST_CAT_PL2X" );
216 pBCSTSend
->Broadcast( BCST_CAT_MINORCOPY
, "Message: BCST_CAT_MINORCOPY" );
217 pBCSTSend
->Broadcast( BCST_CAT_DELIVER
, "Message: BCST_CAT_DELIVER" );
218 pBCSTSend
->Broadcast( BCST_CAT_ALL
, "Message: BCST_CAT_ALL" );
222 pBCSTSend
= new InformationBroadcaster();
230 IMPL_LINK( CommunicationTester
, ConnectionOpened
, CommunicationLink
*, pCL
)
232 SvStream
*pData
= pCL
->GetBestCommunicationStream();
233 while ( pData
->Tell() < 70 ) *pData
<< 123;
235 pCL
->TransferDataStream( pData
);
239 IMPL_LINK( CommunicationTester
, ConnectionClosed
, CommunicationLink
*, pCL
)
244 IMPL_LINK( CommunicationTester
, DataReceived
, CommunicationLink
*, pCL
)
247 CommunicationManager
* pManager
;
248 if ( pClientTcp
&& pClientTcp
->IsLinkValid( pCL
) )
250 pManager
= pClientTcp
;
252 if ( pServerTcp
&& pServerTcp
->IsLinkValid( pCL
) )
254 DBG_ASSERT( !pManager
, "CommunicationLink bei mehreren Managern eingetragen");
255 pManager
= pServerTcp
;
257 DBG_ASSERT( pCL
->GetCommunicationManager() == pManager
, "Manager des Link != Manager bei dem der Link Valid ist");
259 // Send Data Back (Echo)
260 new PacketSender( 1000, pCL
->GetServiceData(), pCL
);