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: cfglocal.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_configmgr.hxx"
33 #define _PRIVATE_TEST_
38 #include <vos/socket.hxx>
39 #include "attributes.hxx"
41 #ifndef _CONFIGMGR_SESSION_REMOTESESSION_HXX_
42 #include "remotesession.hxx"
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #include <cppuhelper/servicefactory.hxx>
46 #include <cppuhelper/implbase1.hxx>
47 #include <vos/conditn.hxx>
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
;
62 // #define USE_LAYOUT_NODE
64 //=============================================================================
65 //= a dirty littly class for printing ascii characters
66 //=============================================================================
70 sal_Char
* m_pCharacters
;
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();
87 sal_Bool bAsserted
= sal_False
;
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
);
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
109 ::vos::OCondition
& m_rFinishCondition
;
111 enum ACTION
{ STARTELEMENT
, CHARACTERS
, ENDELEMENT
};
112 ACTION m_eLastAction
;
114 sal_Bool m_bCloseStartTag
;
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); }
133 virtual void acknowledged(sal_Int32 _nTransId
);
134 virtual void failed(sal_Int32 _nErrorCode
);
135 virtual void done(const StatusInfo
& _rStatus
);
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(); }
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()
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;
167 //-----------------------------------------------------------------------------
168 void SAL_CALL
OOpenNodeCallback::startElement( const ::rtl::OUString
& _rName
, const Reference
< sax::XAttributeList
>& xAttribs
) throw(sax::SAXException
, RuntimeException
)
170 switch (m_eLastAction
)
173 if (m_bCloseStartTag
)
178 m_bCloseStartTag
= sal_False
;
186 m_eLastAction
= STARTELEMENT
;
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
)
196 switch (m_eLastAction
)
199 if (m_bCloseStartTag
)
204 m_bCloseStartTag
= sal_False
;
211 // printf("</%s>", ASCII_STRING(_rName));
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())
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
)
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
)
260 printf("\n\ndone, but had an error : %s\n", ASCII_STRING(_rStatus
.sMessage
));
264 // printf("\n\nsuccessfully done\n", ASCII_STRING(_rStatus.sMessage));
267 m_rFinishCondition
.set();
270 //=============================================================================
272 //=============================================================================
273 class ONodeUpdater
: public IDOMNodeDataProvider
275 sal_Bool m_bWriterLevel
;
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
;
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
);
299 _rHandler
->characters(UNI_STRING("0.90"));
301 _rHandler
->characters(UNI_STRING("99.99"));
303 _rHandler
->endElement(UNI_STRING("TabStops"));
304 _rHandler
->endElement(UNI_STRING("Layout"));
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
);
312 _rHandler
->characters(UNI_STRING("Arial"));
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());
325 _rHandler
->endElement(UNI_STRING("Writer"));
328 //=============================================================================
330 //=============================================================================
331 class OSessionListener
: public ISessionListener
334 oslInterlockedCount m_refCount
;
338 virtual void nodeUpdated(const ::rtl::OUString
& _rNodePath
);
339 virtual void nodeDeleted(const ::rtl::OUString
& _rNodePath
);
340 virtual void nodeAdded(const ::rtl::OUString
& _rNodePath
);
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
))
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
[])
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();
399 sRegistry
= ::rtl::OUString::createFromAscii(argv
[2]);
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");
413 //=============================================================================
415 #if (defined UNX) || (defined OS2)
416 void main( int argc
, char * argv
[] )
418 void _cdecl
main( int argc
, char * argv
[] )
421 Reference
< XMultiServiceFactory
> xORB
;
424 xORB
= createRegistryServiceFactory(sRegistry
, ::rtl::OUString());
428 printf("could not bootstrap the services from %s\n\r", ASCII_STRING(sRegistry
));
429 printf(" (error message : %s)", ASCII_STRING(e
.Message
));
434 fprintf(stdout
, "could not create the service factory !");
438 ORef
< OSessionListener
> xListener
= new OSessionListener
;
441 ORemoteSession aSession(xORB);
442 // --------- connect ----------
443 sal_Bool bSuccess = aSession.connect(sHost, nPort, &aTimeout);
446 printf("could not connect to the server (error : %i) ...", aSession.getConnectionError());
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
;
464 nNanoSec
= 1000000000 - nNanoSec
;
468 cout
<< "Time: " << nSeconds
<< ". " << nNanoSec
<< endl
;
473 void testSession(const Reference
< XMultiServiceFactory
> &xORB
, bool bPrint
)
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 ...");
495 printf("\ncould not open the session ... exiting\n\n");
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");
516 case ICondition::result_timeout
:
517 if (bPrint
) printf("timed out ... exiting\n\n");
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";
529 char* pLayoutNode
= "com.sun.star.Setup/Modules";
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))
537 // case ICondition::result_error:
538 // printf("error while waiting for the callback ... exiting\n\n");
540 // case ICondition::result_timeout:
541 // printf("timed out ... exiting\n\n");
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");
555 case ICondition::result_timeout
:
556 if (bPrint
) printf("timed out ... exiting\n\n");
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");
572 case ICondition::result_timeout
:
573 if (bPrint
) printf("timed out ... exiting\n\n");
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");
588 case ICondition::result_timeout
:
589 if (bPrint
) printf("timed out ... exiting\n\n");
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))
611 // case ICondition::result_error:
612 // printf("error while waiting for the callback ... exiting\n\n");
614 // case ICondition::result_timeout:
615 // printf("timed out ... exiting\n\n");
619 aSession
.closeNode(sOpenedNode
, NULL
);
620 aSession
.close(NULL
);