[omniORB] Re: Getting client IP address in server + example interceptors

bjorn rohde jensen shamus@tdcadsl.dk
Thu, 14 Feb 2002 14:35:04 +0100


Hi guys,

 Well, i am glad, you chose to share this with the list. I am sure,
i will make good use of the example:)

yours sincerely,

bjorn

Brecht Vermeulen wrote:
> 
> Hi all,
> 
> sorry for replying to my own mail, but the problem is solved and I
> thought other persons would be interested in the solution (and maybe
> point out some odd things in my code :-) ). This is also an example of
> the omni Interceptors.
> 
> I used basically the eg2 (although an adapted version) example, and I
> will provide here the clues to the changes, if you would like full
> source code of my example, just contact me.
> 
> add:
> 
> // For interceptors
> #include <omniORB4/omniInterceptors.h>
> 
> // Thread specific data
> pthread_key_t key;
> 
> // Specifically for use of info in giop_s in
> serverReceiveRequest_T::info_T
> // to compile, add -I...omni/src/lib/omniORB/include/
> // This poses the question: wouldn't it be interesting to add these to
> omni/include/omniORB4, just as
> // omniInterceptors.h ??, now you need the source code to compile this
> (or you have to copy these header files)
> #include <giopStrand.h>
> #include <giopStream.h>
> #include <GIOP_S.h>
> 
> CORBA::Boolean
> testfunc(omni::omniInterceptors::serverReceiveRequest_T::info_T& info) {
>         char *clientaddress;
>         cout << "Thread ID : " << pthread_self() << endl;
>         cout << "Operation name : " << info.giop_s.operation_name() << endl;
>         cout << "Peer (client) address : " << ( (omni::giopStrand&)info.giop_s
> ).connection->peeraddress() << endl;
>         cout << "My (server) address : " << ( (omni::giopStrand&)info.giop_s
> ).connection->myaddress() << endl;
>         clientaddress = (char *)pthread_getspecific(key);
>         delete[] clientaddress;
>         clientaddress = new char [strlen( ( (omni::giopStrand&)info.giop_s
> ).connection->peeraddress() ) +1];
>         strcpy(clientaddress, ( (omni::giopStrand&)info.giop_s
> ).connection->peeraddress() );
>         pthread_setspecific(key, (void *)clientaddress);
>         return true;
> }
> 
> void delete_tsd(void *data) {
>         cout << "Deleting thread specific data: " << (char *)data << endl;
>         delete[] (char *)data;
> }
> 
> and in the main function, I added it before the poamanageer->activate
> and orb->run
> 
> if (pthread_key_create(&key, &delete_tsd) ) {
>         cerr << "cannot create key for thread specific data" << endl;
>         exit(1);
> }
> omniORB::getInterceptors()->serverReceiveRequest.add(&testfunc);
> 
> then, in the implementation of your IDL operations:
> 
>   cout << "echoString: client address : " << (char
> *)pthread_getspecific(key) << endl;
> 
> and that's it.
> 
> The output is:
> Thread ID : 5124
> Operation name : echoString
> Peer (client) address : giop:tcp:192.168.2.1:34125
> My (server) address : giop:tcp:192.168.2.1:34122
> echoString: client address : giop:tcp:192.168.2.1:34125
> 
> best regards,
> Brecht
>