Getting XConnectionBroadcaster interface in C++

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
mangesh
Posts: 26
Joined: Tue Aug 21, 2012 10:48 am

Getting XConnectionBroadcaster interface in C++

Post by mangesh »

Hello All,
I have recently started work on customizing OpenOffice for my application. For this I need to implement a Macro function which will talk to my external C++ application. When a call to the macro function is made, the function will connect on the port and send a query over the connection. For this I am trying to implement a stream listener (inherited from XStreamListener) in the external application. The idea is that the external application will get a call when the data is sent across the bridge. It will then accept the data and send the result back to OpenOffice.org. However I am unable get a reference to the XConnectionBroadcaster Interface after connecting to OpenOffice.org, so that I can add listeners to a connection. I have seen the example below on adding the XStreamlistener on the connection in the forum , but can’t find help on this in C++

http://www.oooforum.org/forum/viewtopic ... uest#26353

Any inputs in this regard will be appreciated.

Thanks
Mangesh Shukla
OpenOffice3.4.0 Win7 x64.
mangesh
Posts: 26
Joined: Tue Aug 21, 2012 10:48 am

Using XConnectionBroadcaster interface in C++

Post by mangesh »

I have resolved the issue with getting the XConnectionBroadcaster interface from the XConnection interface using the Xray tool.
Using this XConnectionBroadcaster, I am able to add my custom XStreamListener to the XConnectionBroadcaster. See code below

Code: Select all

    // Class to listen to the OpenOffice.org connection. It registers on the XConnection to recieve a callback 
    // when the user tries to write data to the socket from OpenOffice.org.
    class ConnectionMonitor : public ::cppu::WeakImplHelper1<XStreamListener>
    {

    public:
        // Constrution 
        ConnectionMonitor(Reference<XConnection> xConnection, bool bRegister = true) :
                          rConnection(xConnection), m_bRegistered(false) 		    
        {
            if (bRegister)
            {
                Register(true);
            }
        }

        //destructor
        ~ConnectionMonitor () 
        {
            Register(false);
        }


        bool Register(bool bRegister)
        {
            Reference <XConnectionBroadcaster > xBC (rConnection, UNO_QUERY);

            if ( !xBC.is() )
            {
                printf("XConnectionBroadcaster is not found");
                return false;
            }

            if (bRegister != m_bRegistered)
                try
                {			
                    if (bRegister)
                    {
                        xBC->addStreamListener(this);
                    }
                    else
                    {
                        xBC->removeStreamListener(this);
                    }
                
                    m_bRegistered = bRegister;
                }
                catch (Exception e)			    
                {	
                    printf("Error: Problem encountered while adding/removing StreamListener on Connection to OOo %s\n",
                            OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());                    
                    return false;  
                }

            return m_bRegistered;
        }
    protected:
        // Implementation data 
        bool m_bRegistered;
        Reference<XConnection> rConnection;

    public:
        // XEventListener
        virtual void SAL_CALL disposing(const EventObject& Source)
        {
            m_bRegistered = false;
        }

        // //XStreamListener
        virtual void SAL_CALL started() throw (RuntimeException)       
        {
            printf(" Streaming started \n");
        }
     
        // //XStreamListener
        virtual void SAL_CALL closed() throw (RuntimeException)
        {
            printf(" Streaming closed \n");
        }

       ////XStreamListener
        virtual void SAL_CALL terminated() throw (RuntimeException)
        {
            printf(" Streaming terminated \n");
        }

        ////XStreamListener
        virtual void SAL_CALL error( const ::Any& aException ) throw (::com::sun::star::uno::RuntimeException)
        {
        }
    };
The external application now waits on the OpenOffice.org application. I am using an OOBasic macro function, to get the XConnection. The macro writes some text to the socket, and closes the Connection as seen below

Code: Select all

Sub Test2   
   oConnector = createUnoService( "com.sun.star.connection.Connector" )
   oConnection = oConnector.connect("socket,host=localhost,port=2081")   
   cCR = Chr(13)
   cLF = Chr (10) 
   oConnection.write( StringToByteArray(  "Hello World" + cCR + cLF + cCR + cLF) )
   oConnection.flush()
   oConnection.close()   
End Sub
I expected a call to the ConnectionMonitor::started(), but it does not get called. I wish to read the data sent to the socket by OpenOffice macro, at the callbacks implemented by the ConnectionMonitor class.
Please let me know if I am missing something.

thanks
Mangesh
OpenOffice3.4.0 Win7 x64.
Post Reply