merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / plugapp / commtest.cxx
blobce666ab031f7ca2ce2d7b87b1c90b9c0ade5cb3b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: commtest.cxx,v $
10 * $Revision: 1.6 $
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
54 SvStream* mpData;
55 CommunicationLinkRef mxCL;
57 public:
58 PacketSender( ULONG nDelay, SvStream* pData, CommunicationLink* pCL );
59 virtual void Timeout();
62 PacketSender::PacketSender( ULONG nDelay, SvStream* pData, CommunicationLink* pCL )
63 : mpData( pData )
64 , mxCL( pCL )
66 SetTimeout( nDelay );
67 Start();
70 void PacketSender::Timeout()
72 mxCL->TransferDataStream( mpData );
73 delete mpData;
74 delete this;
79 class DelayedDeleter : public Timer
81 CommunicationManager *mpManager;
83 public:
84 DelayedDeleter( ULONG nDelay, CommunicationManager *pManager );
85 virtual void Timeout();
88 DelayedDeleter::DelayedDeleter( ULONG nDelay, CommunicationManager *pManager )
89 : mpManager( pManager )
91 SetTimeout( nDelay );
92 Start();
95 void DelayedDeleter::Timeout()
97 delete mpManager;
98 delete this;
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;
116 public:
117 CommunicationTester();
119 virtual void Main();
122 CommunicationTester IchSelber;
124 CommunicationTester::CommunicationTester()
125 : pClientTcp( NULL )
126 , pServerTcp( NULL )
127 , pBCSTSend( NULL )
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 );
137 aWW.Show();
138 ToolBox aTB( &aWW, ResId( TBMenu ) );
139 aTB.Show();
140 aTB.RecalcItems();
141 aTB.SetFloatingMode( TRUE );
142 aTB.SetFloatingMode( FALSE );
143 aTB.SetClickHdl( LINK( this, CommunicationTester, TBClick ) );
145 Execute();
148 #define SWITCH( pManager, ManagerClass ) \
150 if ( pManager ) \
152 pManager->StopCommunication(); \
153 new DelayedDeleter( 1000, pManager ); \
154 pTB->SetItemState( pTB->GetCurItemId(), STATE_NOCHECK ); \
155 pManager = NULL; \
157 else \
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() )
172 case SERVER_TCP:
174 SWITCH( pServerTcp, CommunicationManagerServerViaSocket( TCP_PORT, (USHORT) 32000 ) );
175 if ( pServerTcp )
176 pServerTcp->StartCommunication(); // Am Port horchen
178 break;
179 case CLIENT_TCP:
181 SWITCH( pClientTcp, CommunicationManagerClientViaSocket( "localhost", TCP_PORT ) );
182 if ( pClientTcp )
183 pClientTcp->StartCommunication(); // Eine Verbindung aufbauen
185 break;
186 case BCST_BROOKER:
188 if ( pBCSTBrooker )
190 delete pBCSTBrooker;
191 pBCSTBrooker = NULL;
193 else
195 pBCSTBrooker = new InformationBrooker();
198 break;
199 case BCST_LISTEN:
201 if ( pBCSTListen )
203 delete pBCSTListen;
204 pBCSTListen = NULL;
206 else
208 pBCSTListen = new InformationBroadcaster();
211 case BCST_SEND:
213 if ( pBCSTSend )
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" );
220 else
222 pBCSTSend = new InformationBroadcaster();
225 break;
227 return 0;
230 IMPL_LINK( CommunicationTester, ConnectionOpened, CommunicationLink*, pCL )
232 SvStream *pData = pCL->GetBestCommunicationStream();
233 while ( pData->Tell() < 70 ) *pData << 123;
235 pCL->TransferDataStream( pData );
236 return 0;
239 IMPL_LINK( CommunicationTester, ConnectionClosed, CommunicationLink*, pCL )
241 return 0;
244 IMPL_LINK( CommunicationTester, DataReceived, CommunicationLink*, pCL )
246 // Find Manager
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 );
262 return 0;