update dev300-m58
[ooovba.git] / configmgr / workben / local_io / cfglocal.cxx
blobc5de94c03ff6b71342227bcaf18e04beee31191a
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: cfglocal.cxx,v $
10 * $Revision: 1.5 $
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_configmgr.hxx"
33 #define _PRIVATE_TEST_
35 #include <memory.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <vos/socket.hxx>
39 #include "attributes.hxx"
41 #ifndef _CONFIGMGR_SESSION_REMOTESESSION_HXX_
42 #include "remotesession.hxx"
43 #endif
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #include <cppuhelper/servicefactory.hxx>
46 #include <cppuhelper/implbase1.hxx>
47 #include <vos/conditn.hxx>
49 #include <osl/time.h>
51 #include "localsession.hxx"
52 #include "confname.hxx"
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::xml;
57 using namespace ::vos;
58 using namespace ::cppu;
59 using namespace ::configmgr;
60 using namespace rtl;
62 // #define USE_LAYOUT_NODE
64 //=============================================================================
65 //= a dirty littly class for printing ascii characters
66 //=============================================================================
67 class OAsciiOutput
69 protected:
70 sal_Char* m_pCharacters;
72 public:
73 OAsciiOutput(const ::rtl::OUString& _rUnicodeChars);
74 ~OAsciiOutput() { delete m_pCharacters; }
76 const sal_Char* getCharacters() const { return m_pCharacters; }
79 //-----------------------------------------------------------------------------
80 OAsciiOutput::OAsciiOutput(const ::rtl::OUString& _rUnicodeChars)
82 sal_Int32 nLen = _rUnicodeChars.getLength();
83 m_pCharacters = new sal_Char[nLen + 1];
84 sal_Char* pFillPtr = m_pCharacters;
85 const sal_Unicode* pSourcePtr = _rUnicodeChars.getStr();
86 #ifdef DBG_UTIL
87 sal_Bool bAsserted = sal_False;
88 #endif
89 for (sal_Int32 i=0; i<nLen; ++i, ++pFillPtr, ++pSourcePtr)
91 OSL_ENSURE(bAsserted || !(bAsserted = (*pSourcePtr >= 0x80)),
92 "OAsciiOutput::OAsciiOutput : non-ascii character found !");
93 *pFillPtr = *reinterpret_cast<const sal_Char*>(pSourcePtr);
95 *pFillPtr = 0;
98 #define ASCII_STRING(rtlOUString) OAsciiOutput(rtlOUString).getCharacters()
99 #define UNI_STRING(salCharPtr) ::rtl::OUString::createFromAscii(salCharPtr)
101 //=============================================================================
102 //= OOpenNodeCallback
103 //=============================================================================
104 typedef ::cppu::WeakImplHelper1< sax::XDocumentHandler > OOpenNodeCallback_Base;
105 class OOpenNodeCallback : public IOpenObjectCallback, public OOpenNodeCallback_Base
107 protected:
108 OUString m_sNodeId;
109 ::vos::OCondition& m_rFinishCondition;
111 enum ACTION { STARTELEMENT, CHARACTERS, ENDELEMENT };
112 ACTION m_eLastAction;
113 sal_Int32 m_nLevel;
114 sal_Bool m_bCloseStartTag;
116 protected:
117 ~OOpenNodeCallback()
121 public:
122 OOpenNodeCallback(::vos::OCondition& _rFinishCond) : m_rFinishCondition(_rFinishCond), m_nLevel(0), m_eLastAction(ENDELEMENT), m_bCloseStartTag(sal_False) { }
124 OUString getNodeId() const { return m_sNodeId; }
126 // IOpenObjectCallback
127 virtual void gotObjectId(const OUString &aName);
129 // IDataRequestCallback
130 virtual Reference< sax::XDocumentHandler > getDataReader() { return static_cast< sax::XDocumentHandler* >(this); }
132 // IRequestCallback
133 virtual void acknowledged(sal_Int32 _nTransId);
134 virtual void failed(sal_Int32 _nErrorCode);
135 virtual void done(const StatusInfo& _rStatus);
137 // IInterface
138 virtual void SAL_CALL acquire( ) throw (::com::sun::star::uno::RuntimeException) { OOpenNodeCallback_Base::acquire(); }
139 virtual void SAL_CALL release( ) throw (::com::sun::star::uno::RuntimeException) { OOpenNodeCallback_Base::release(); }
141 // XDocumentHandler
142 virtual void SAL_CALL startDocument( ) throw(sax::SAXException, RuntimeException) { }
143 virtual void SAL_CALL endDocument( ) throw(sax::SAXException, RuntimeException) { }
144 virtual void SAL_CALL startElement( const ::rtl::OUString& aName, const Reference< sax::XAttributeList >& xAttribs ) throw(sax::SAXException, RuntimeException);
145 virtual void SAL_CALL endElement( const ::rtl::OUString& aName ) throw(sax::SAXException, RuntimeException);
146 virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw(sax::SAXException, RuntimeException);
147 virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw(sax::SAXException, RuntimeException) { }
148 virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData ) throw(sax::SAXException, RuntimeException) { }
149 virtual void SAL_CALL setDocumentLocator( const Reference< sax::XLocator >& xLocator ) throw(sax::SAXException, RuntimeException) { }
152 //.............................................................................
153 inline void linefeed()
155 // printf("\n");
158 //.............................................................................
159 void printTabs(sal_Int32 _nCount)
161 sal_Char* pBuffer = new sal_Char[2 * _nCount + 1];
162 memset(pBuffer, ' ', 2 * _nCount);
163 pBuffer[2 * _nCount] = 0;
164 // printf(pBuffer);
167 //-----------------------------------------------------------------------------
168 void SAL_CALL OOpenNodeCallback::startElement( const ::rtl::OUString& _rName, const Reference< sax::XAttributeList >& xAttribs ) throw(sax::SAXException, RuntimeException)
170 switch (m_eLastAction)
172 case STARTELEMENT:
173 if (m_bCloseStartTag)
175 // printf(">");
178 m_bCloseStartTag = sal_False;
179 // no break
180 case CHARACTERS:
181 case ENDELEMENT:
182 linefeed();
183 printTabs(m_nLevel);
184 break;
186 m_eLastAction = STARTELEMENT;
187 ++m_nLevel;
188 // printf("<%s", ASCII_STRING(_rName));
189 m_bCloseStartTag = sal_True;
192 //-----------------------------------------------------------------------------
193 void SAL_CALL OOpenNodeCallback::endElement( const ::rtl::OUString& _rName ) throw(sax::SAXException, RuntimeException)
195 --m_nLevel;
196 switch (m_eLastAction)
198 case STARTELEMENT:
199 if (m_bCloseStartTag)
201 // printf("/>");
204 m_bCloseStartTag = sal_False;
205 break;
206 case ENDELEMENT:
207 linefeed();
208 printTabs(m_nLevel);
209 // dont break
210 case CHARACTERS:
211 // printf("</%s>", ASCII_STRING(_rName));
212 break;
214 m_eLastAction = ENDELEMENT;
217 //-----------------------------------------------------------------------------
218 void SAL_CALL OOpenNodeCallback::characters( const ::rtl::OUString& _rChars ) throw(sax::SAXException, RuntimeException)
220 if (STARTELEMENT == m_eLastAction)
222 if (m_bCloseStartTag && _rChars.trim().getLength())
224 // printf(">");
225 m_bCloseStartTag = sal_False;
228 if (_rChars.trim().getLength() != 0)
230 // printf("%s", ASCII_STRING(_rChars));
231 m_eLastAction = CHARACTERS;
235 //-----------------------------------------------------------------------------
236 void OOpenNodeCallback::gotObjectId(const OUString &_nId)
238 m_sNodeId = _nId;
239 // printf("object id %i\n", m_nNodeId);
242 //-----------------------------------------------------------------------------
243 void OOpenNodeCallback::acknowledged(sal_Int32 _nTransId)
245 // printf("acknowledged, transaction id : %i\n", _nTransId);
248 //-----------------------------------------------------------------------------
249 void OOpenNodeCallback::failed(sal_Int32 _nErrorCode)
251 // printf("failed because of a connection error (%i)\n", _nErrorCode);
252 m_rFinishCondition.set();
255 //-----------------------------------------------------------------------------
256 void OOpenNodeCallback::done(const StatusInfo& _rStatus)
258 if (_rStatus.nCode)
260 printf("\n\ndone, but had an error : %s\n", ASCII_STRING(_rStatus.sMessage));
262 else
264 // printf("\n\nsuccessfully done\n", ASCII_STRING(_rStatus.sMessage));
267 m_rFinishCondition.set();
270 //=============================================================================
271 //= ONodeUpdater
272 //=============================================================================
273 class ONodeUpdater : public IDOMNodeDataProvider
275 sal_Bool m_bWriterLevel;
276 sal_Bool m_bAdd;
277 public:
278 ONodeUpdater(sal_Bool _bStartAtWriterLevel, sal_Bool bAdd) : m_bWriterLevel(_bStartAtWriterLevel), m_bAdd(bAdd) { }
280 virtual void writeNodeData(const Reference< sax::XDocumentHandler >& _rHandler);
283 //-----------------------------------------------------------------------------
284 void ONodeUpdater::writeNodeData(const Reference< sax::XDocumentHandler >& _rHandler)
286 AttributeListImpl *pAttr = new AttributeListImpl;
287 Reference< sax::XAttributeList > xEmptyAttrList = pAttr;
289 pAttr = new AttributeListImpl;
290 pAttr->addAttribute(UNI_STRING("type"),UNI_STRING("CDATA"),UNI_STRING("string"));
291 Reference< sax::XAttributeList > xStringAttrList = pAttr;
293 if (m_bWriterLevel)
294 _rHandler->startElement(UNI_STRING("Writer"), xEmptyAttrList);
295 #ifdef USE_LAYOUT_NODE
296 _rHandler->startElement(UNI_STRING("Layout"), xEmptyAttrList);
297 _rHandler->startElement(UNI_STRING("TabStops"), xEmptyAttrList);
298 if (m_bAdd)
299 _rHandler->characters(UNI_STRING("0.90"));
300 else
301 _rHandler->characters(UNI_STRING("99.99"));
303 _rHandler->endElement(UNI_STRING("TabStops"));
304 _rHandler->endElement(UNI_STRING("Layout"));
305 #else
306 // _rHandler->startElement(UNI_STRING("com.sun.star.office.Setup"), xEmptyAttrList);
307 _rHandler->startElement(UNI_STRING("Modules"), xEmptyAttrList);
308 _rHandler->startElement(UNI_STRING("StandFonts"), xEmptyAttrList);
309 _rHandler->startElement(UNI_STRING("Standard"), xStringAttrList);
310 _rHandler->startElement(UNI_STRING("value"), xEmptyAttrList);
311 if (m_bAdd)
312 _rHandler->characters(UNI_STRING("Arial"));
313 else
314 _rHandler->characters(UNI_STRING("Courier"));
316 _rHandler->endElement(UNI_STRING("value"));
317 _rHandler->endElement(UNI_STRING("Standard"));
318 _rHandler->endElement(UNI_STRING("StandFonts"));
319 _rHandler->endElement(UNI_STRING("Modules"));
320 // _rHandler->endElement(UNI_STRING("com.sun.star.office.Setup"));
322 _rHandler->ignorableWhitespace(OUString());
323 #endif
324 if (m_bWriterLevel)
325 _rHandler->endElement(UNI_STRING("Writer"));
328 //=============================================================================
329 //= OSessionListener
330 //=============================================================================
331 class OSessionListener : public ISessionListener
333 protected:
334 oslInterlockedCount m_refCount;
336 public:
337 // ISessionListener
338 virtual void nodeUpdated(const ::rtl::OUString& _rNodePath);
339 virtual void nodeDeleted(const ::rtl::OUString& _rNodePath);
340 virtual void nodeAdded(const ::rtl::OUString& _rNodePath);
342 // IInterface
343 virtual void SAL_CALL acquire( ) throw (::com::sun::star::uno::RuntimeException);
344 virtual void SAL_CALL release( ) throw (::com::sun::star::uno::RuntimeException);
347 //-----------------------------------------------------------------------------
348 void OSessionListener::nodeUpdated(const ::rtl::OUString& _rNodePath)
350 // printf("[listener] : a node was updated, node path : %s\n", ASCII_STRING(_rNodePath));
353 //-----------------------------------------------------------------------------
354 void OSessionListener::nodeDeleted(const ::rtl::OUString& _rNodePath)
356 // printf("[listener] : a node was deleted, node path : %s\n", ASCII_STRING(_rNodePath));
359 //-----------------------------------------------------------------------------
360 void OSessionListener::nodeAdded(const ::rtl::OUString& _rNodePath)
362 // printf("\n[listener] : a node was added, node path : %s", ASCII_STRING(_rNodePath));
365 //-----------------------------------------------------------------------------
366 void SAL_CALL OSessionListener::acquire( ) throw (::com::sun::star::uno::RuntimeException)
368 osl_incrementInterlockedCount(&m_refCount);
371 //-----------------------------------------------------------------------------
372 void SAL_CALL OSessionListener::release( ) throw (::com::sun::star::uno::RuntimeException)
374 if (!osl_decrementInterlockedCount(&m_refCount))
375 delete this;
378 //=============================================================================
379 //=============================================================================
381 //=============================================================================
382 static ::rtl::OUString sHost;
383 static sal_Int32 nPort;
384 static ::rtl::OUString sRegistry = ::rtl::OUString::createFromAscii("applicat.rdb");
386 //=============================================================================
387 sal_Bool collectArgs(int argc, char * argv[])
389 if (argc > 1)
391 sal_Char* pConnectTo = argv[1];
392 sal_Char* pSeparator = strchr(pConnectTo, ':');
393 if (pSeparator && (0 != *(pSeparator + 1)))
395 sHost = ::rtl::OUString(pConnectTo, pSeparator - pConnectTo, RTL_TEXTENCODING_ASCII_US);
396 nPort = ::rtl::OUString::createFromAscii(pSeparator + 1).toInt32();
398 if (argc > 2)
399 sRegistry = ::rtl::OUString::createFromAscii(argv[2]);
400 return sal_True;
404 printf("cfgclient - registry server client test ...\n\r\n\r");
405 printf("usage :\n\r");
406 printf(" cfgclient <server>:<port> [<registry file>]\n\r\n\r");
407 printf(" <server> : machine to connect to\n\r");
408 printf(" <port> : port to connect to\n\r");
409 printf(" <registry file> : (optional) registry to bootstrap from. defaulted to \"applicat.rdb\"\n\r\n\r");
410 return sal_False;
413 //=============================================================================
415 #if (defined UNX) || (defined OS2)
416 void main( int argc, char * argv[] )
417 #else
418 void _cdecl main( int argc, char * argv[] )
419 #endif
421 Reference< XMultiServiceFactory > xORB;
424 xORB = createRegistryServiceFactory(sRegistry, ::rtl::OUString());
426 catch (Exception& e)
428 printf("could not bootstrap the services from %s\n\r", ASCII_STRING(sRegistry));
429 printf(" (error message : %s)", ASCII_STRING(e.Message));
432 if (!xORB.is())
434 fprintf(stdout, "could not create the service factory !");
435 return;
438 ORef< OSessionListener > xListener = new OSessionListener;
441 ORemoteSession aSession(xORB);
442 // --------- connect ----------
443 sal_Bool bSuccess = aSession.connect(sHost, nPort, &aTimeout);
444 if (!bSuccess)
446 printf("could not connect to the server (error : %i) ...", aSession.getConnectionError());
447 return;
451 void testSession(const Reference< XMultiServiceFactory > &xORB, bool bPrint);
453 for (int i=0;i<100;i++)
455 TimeValue aStartTime, aEndTime;
456 osl_getSystemTime(&aStartTime);
457 testSession(xORB, false);
458 osl_getSystemTime(&aEndTime);
460 sal_Int32 nSeconds = aEndTime.Seconds - aStartTime.Seconds;
461 sal_Int32 nNanoSec = aEndTime.Nanosec - aStartTime.Nanosec;
462 if (nNanoSec < 0)
464 nNanoSec = 1000000000 - nNanoSec;
465 nSeconds++;
468 cout << "Time: " << nSeconds << ". " << nNanoSec << endl;
473 void testSession(const Reference< XMultiServiceFactory > &xORB, bool bPrint)
475 TimeValue aTimeout;
476 aTimeout.Seconds = 5;
477 aTimeout.Nanosec = 0;
480 // create it .. and connect
481 LocalSession aSession(xORB);
483 // --------- openSession ----------
485 OUString aRootPath = OUString::createFromAscii("f:/local/SRC598/configmgr/workben/local_io");
486 // f:/local/SRC595/configmgr/workben/local_io);
487 aSession.open(aRootPath);
489 if (aSession.isOpen())
491 if (bPrint) printf("\nopened the session ...");
493 else
495 printf("\ncould not open the session ... exiting\n\n");
496 return;
499 // aSession.setListener(xListener.getBodyPtr());
501 OCondition aWaitForSomething;
502 aWaitForSomething.reset();
504 // --------- openNode ----------
505 char* pWriterNode = "com.sun.star.Setup/Modules";
506 if (bPrint) printf("\nsending an openNode request for %s ...\n", pWriterNode);
507 ORef< OOpenNodeCallback > xOpenCallback = new OOpenNodeCallback(aWaitForSomething);
508 aSession.openNode(UNI_STRING(pWriterNode), 2, xOpenCallback.getBodyPtr());
510 aTimeout.Seconds = 30;
511 switch (aWaitForSomething.wait(&aTimeout))
513 case ICondition::result_error:
514 printf("error while waiting for the callback ... exiting\n\n");
515 return;
516 case ICondition::result_timeout:
517 if (bPrint) printf("timed out ... exiting\n\n");
518 return;
521 OUString sOpenedNode = xOpenCallback->getNodeId();
523 // aSession.getNode(UNI_STRING("com.sun.star.Spreadsheet"), NULL);
525 // // --------- getNode (1) ----------
526 //#ifdef USE_LAYOUT_NODE
527 // char* pLayoutNode = "com.sun.star.office.Setup/Modules";
528 //#else
529 char* pLayoutNode = "com.sun.star.Setup/Modules";
530 //#endif
531 // printf("\nsending an getNode request for %s ...\n", pLayoutNode);
532 // aWaitForSomething.reset();
533 // aSession.getNode(UNI_STRING(pLayoutNode), new OOpenNodeCallback(aWaitForSomething));
535 // switch (aWaitForSomething.wait(&aTimeout))
536 // {
537 // case ICondition::result_error:
538 // printf("error while waiting for the callback ... exiting\n\n");
539 // return;
540 // case ICondition::result_timeout:
541 // printf("timed out ... exiting\n\n");
542 // return;
543 // }
545 // --------- addNode ----------
546 if (bPrint) printf("\n\naddNode ....");
547 ONodeUpdater aAddTabstops(sal_False, true); // true = WRITER LEVEL
548 aWaitForSomething.reset();
549 aSession.addNode(sOpenedNode, UNI_STRING(pLayoutNode), &aAddTabstops, new OOpenNodeCallback(aWaitForSomething));
550 switch (aWaitForSomething.wait(&aTimeout))
552 case ICondition::result_error:
553 printf("error while waiting for the callback ... exiting\n\n");
554 return;
555 case ICondition::result_timeout:
556 if (bPrint) printf("timed out ... exiting\n\n");
557 return;
560 // --------- updateNode ----------
561 if (bPrint) printf("\n\nokay, let's try an update ....\n");
563 ONodeUpdater aUpdateWriter(sal_False, false);
564 aWaitForSomething.reset();
565 aSession.updateNode(sOpenedNode, UNI_STRING(pLayoutNode), &aUpdateWriter, new OOpenNodeCallback(aWaitForSomething));
567 switch (aWaitForSomething.wait(&aTimeout))
569 case ICondition::result_error:
570 printf("error while waiting for the callback ... exiting\n\n");
571 return;
572 case ICondition::result_timeout:
573 if (bPrint) printf("timed out ... exiting\n\n");
574 return;
577 // --------- deleteNode ----------
578 char* pLayoutNode2 = "com.sun.star.Setup/Modules/StandFonts";
580 if (bPrint) printf("\n\ndeleteNode ....");
581 aWaitForSomething.reset();
582 aSession.deleteNode(sOpenedNode, UNI_STRING(pLayoutNode2), new OOpenNodeCallback(aWaitForSomething));
583 switch (aWaitForSomething.wait(&aTimeout))
585 case ICondition::result_error:
586 printf("error while waiting for the callback ... exiting\n\n");
587 return;
588 case ICondition::result_timeout:
589 if (bPrint) printf("timed out ... exiting\n\n");
590 return;
594 // --------- startListening ----------
595 printf("\n\nadding a listener for the Layout node\n");
596 Sequence< ::rtl::OUString > aNodesToListen(1);
597 aNodesToListen[0] = UNI_STRING(pLayoutNode);
598 aSession.startListening(aNodesToListen, NULL);
600 printf("waiting 10 seconds ....\n\n");
601 aWaitForSomething.reset();
602 aTimeout.Seconds = 10;
603 aWaitForSomething.wait(&aTimeout);
605 // --------- getNode (2) ----------
606 // printf("\ndoing a new getNode for the Layout node ...\n");
607 // aWaitForSomething.reset();
608 // aSession.getNode(UNI_STRING(pLayoutNode), new OOpenNodeCallback(aWaitForSomething));
609 // switch (aWaitForSomething.wait(&aTimeout))
610 // {
611 // case ICondition::result_error:
612 // printf("error while waiting for the callback ... exiting\n\n");
613 // return;
614 // case ICondition::result_timeout:
615 // printf("timed out ... exiting\n\n");
616 // return;
617 // }
619 aSession.closeNode(sOpenedNode, NULL);
620 aSession.close(NULL);