[OROCOSUSERS] FileDescriptorActivity

Dear all,

For a project where I use UDP for communication, I thought I start very
structured and put my UDP send, receive and other definitions in a
separate class. Hence I simply have an UDP object while send and receive
are member functions and I define the socket and bind to it in the
constructor.

Now I am trying to use this class in an orocos component and use
FileDescriptorActivity to watch the UDP socket. The problem was first to
figure out how prevent destructing the UDP object. So I reset the object
with

server_.reset(new UDPServer(comm_addr_, comm_port_));

socket which in constructor is made as:

sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);

is a public member of the class and is defined in the constructor of
UDPServer.

According to the documentation I define my activity in configureHook as:

fd_activity = new RTT::extras::FileDescriptorActivity();
this->setActivity(fd_activity);
fd_activity =
dynamic_cast<RTT::extras::FileDescriptorActivity*>(this->getActivity());

and I tell it to watch the socket:

fd_activity->watch(server->sockfd_);
fd_activity->setTimeout(1000);

I keep getting *error in select() errno 9* which apparently means the
file descriptor is not valid... Now my question is, what is best way to
watch the socket? Do I need to implement the whole connection and its
methods inside the component or is there a better way?

Thanks.

Regards
Keivan

[OROCOSUSERS] FileDescriptorActivity

Hi Keivan,

I created a simple test package roughly following your example:
https://github.com/meyerj/file_descriptor_activity_test

I could not reproduce the EBADF (errno 9) errors with this package, at
least using the most recent toolchain-2.9 version of RTT. Are you sure that
server->sockfd_ is really a valid file descriptor? It is not important how
you initialize and bind or connect the socket as long as it remains valid
while it is being watched by the FileDescriptorActivity.

Regards,
Johannes

On Fri, Jun 24, 2016 at 10:53 AM, Keivan Zavari <keivan [dot] zavari [..] ...>
wrote:

> Dear all,
>
>
> For a project where I use UDP for communication, I thought I start very
> structured and put my UDP send, receive and other definitions in a separate
> class. Hence I simply have an UDP object while send and receive are member
> functions and I define the socket and bind to it in the constructor.
>
> Now I am trying to use this class in an orocos component and use
> FileDescriptorActivity to watch the UDP socket. The problem was first to
> figure out how prevent destructing the UDP object. So I reset the object
> with
>
> server_.reset(new UDPServer(comm_addr_, comm_port_));
>
> socket which in constructor is made as:
>
> sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
>
> is a public member of the class and is defined in the constructor of
> UDPServer.
>
> According to the documentation I define my activity in configureHook as:
>
> fd_activity = new RTT::extras::FileDescriptorActivity();
> this->setActivity(fd_activity);
> fd_activity = dynamic_cast<RTT::extras::FileDescriptorActivity*>(this->
> getActivity());
>
> and I tell it to watch the socket:
>
> fd_activity->watch(server->sockfd_);
> fd_activity->setTimeout(1000);
>
> I keep getting *error in select() errno 9* which apparently means the
> file descriptor is not valid... Now my question is, what is best way to
> watch the socket? Do I need to implement the whole connection and its
> methods inside the component or is there a better way?
>
> Thanks.
>
> Regards
> Keivan
>

[OROCOSUSERS] FileDescriptorActivity

I think found it.

Since the UDP object got destructed at the end of every Hook, I was calling

fd_activity->watch(server_->socket_fd_) ;

once in startHook and once again in updateHook. If I do it only once in
updateHook, then I won't get the error anymore... I only had to call the
constructor more times and not the activity watch. Though I am not sure
if it's good practice to call the constructor so many times because I
move from one operation state to another.

Thanks again.

Regards
Keivan

On 24/06/16 12:35, Johannes Meyer wrote:
> Hi Keivan,
>
> I created a simple test package roughly following your example:
> https://github.com/meyerj/file_descriptor_activity_test
>
> I could not reproduce the EBADF (errno 9) errors with this package, at
> least using the most recent toolchain-2.9 version of RTT. Are you sure
> that server->sockfd_ is really a valid file descriptor? It is not
> important how you initialize and bind or connect the socket as long as
> it remains valid while it is being watched by the FileDescriptorActivity.
>
> Regards,
> Johannes
>
>
>
> On Fri, Jun 24, 2016 at 10:53 AM, Keivan Zavari
> <keivan [dot] zavari [..] ... keivan [dot] zavari [..] ...>> wrote:
>
> Dear all,
>
>
> For a project where I use UDP for communication, I thought I start
> very structured and put my UDP send, receive and other definitions
> in a separate class. Hence I simply have an UDP object while send
> and receive are member functions and I define the socket and bind
> to it in the constructor.
>
> Now I am trying to use this class in an orocos component and use
> FileDescriptorActivity to watch the UDP socket. The problem was
> first to figure out how prevent destructing the UDP object. So I
> reset the object with
>
> server_.reset(new UDPServer(comm_addr_, comm_port_));
>
> socket which in constructor is made as:
>
> sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
>
> is a public member of the class and is defined in the constructor
> of UDPServer.
>
> According to the documentation I define my activity in
> configureHook as:
>
> fd_activity = new RTT::extras::FileDescriptorActivity();
> this->setActivity(fd_activity);
> fd_activity =
> dynamic_cast<RTT::extras::FileDescriptorActivity*>(this->getActivity());
>
>
> and I tell it to watch the socket:
>
> fd_activity->watch(server->sockfd_);
> fd_activity->setTimeout(1000);
>
> I keep getting *error in select() errno 9* which apparently means
> the file descriptor is not valid... Now my question is, what is
> best way to watch the socket? Do I need to implement the whole
> connection and its methods inside the component or is there a
> better way?
>
> Thanks.
>
> Regards
> Keivan
>
>
>
>

[OROCOSUSERS] FileDescriptorActivity

Correct, I just came to the same conclusion. It is no problem to destroy a
UDPServer instance (implicitly by managing it in a shared pointer) and
create a new one for whatever reason, but in this case you have to make
sure that you also unwatch the previous file descriptor in the
FileDescriptorActivity and watch the new one instead. There is no mechanism
in the FileDescriptorActivity that automatically removes bad file
descriptors from the watch list.

Cheers,
Johannes

On Fri, Jun 24, 2016 at 5:00 PM, Keivan Zavari <keivan [dot] zavari [..] ...>
wrote:

> I think found it.
>
> Since the UDP object got destructed at the end of every Hook, I was
> calling
>
> fd_activity->watch(server_->socket_fd_) ;
>
> once in startHook and once again in updateHook. If I do it only once in
> updateHook, then I won't get the error anymore... I only had to call the
> constructor more times and not the activity watch. Though I am not sure if
> it's good practice to call the constructor so many times because I move
> from one operation state to another.
>
> Thanks again.
>
> Regards
> Keivan
>
>
> On 24/06/16 12:35, Johannes Meyer wrote:
>
> Hi Keivan,
>
> I created a simple test package roughly following your example:
> https://github.com/meyerj/file_descriptor_activity_test
>
> I could not reproduce the EBADF (errno 9) errors with this package, at
> least using the most recent toolchain-2.9 version of RTT. Are you sure that
> server->sockfd_ is really a valid file descriptor? It is not important how
> you initialize and bind or connect the socket as long as it remains valid
> while it is being watched by the FileDescriptorActivity.
>
> Regards,
> Johannes
>
>
>
> On Fri, Jun 24, 2016 at 10:53 AM, Keivan Zavari <keivan [dot] zavari [..] ...>
> wrote:
>
>> Dear all,
>>
>>
>> For a project where I use UDP for communication, I thought I start very
>> structured and put my UDP send, receive and other definitions in a separate
>> class. Hence I simply have an UDP object while send and receive are member
>> functions and I define the socket and bind to it in the constructor.
>>
>> Now I am trying to use this class in an orocos component and use
>> FileDescriptorActivity to watch the UDP socket. The problem was first to
>> figure out how prevent destructing the UDP object. So I reset the object
>> with
>>
>> server_.reset(new UDPServer(comm_addr_, comm_port_));
>>
>> socket which in constructor is made as:
>>
>> sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
>>
>> is a public member of the class and is defined in the constructor of
>> UDPServer.
>>
>> According to the documentation I define my activity in configureHook as:
>>
>> fd_activity = new RTT::extras::FileDescriptorActivity();
>> this->setActivity(fd_activity);
>> fd_activity = dynamic_cast<RTT::extras::FileDescriptorActivity*>(this->
>> getActivity());
>>
>> and I tell it to watch the socket:
>>
>> fd_activity->watch(server->sockfd_);
>> fd_activity->setTimeout(1000);
>>
>> I keep getting *error in select() errno 9* which apparently means the
>> file descriptor is not valid... Now my question is, what is best way to
>> watch the socket? Do I need to implement the whole connection and its
>> methods inside the component or is there a better way?
>>
>> Thanks.
>>
>> Regards
>> Keivan
>>
>
>
>
> --
> Johannes Meyer, Roboticist
> +32 468 139828
> Intermodalics - Gaston Geenslaan 9, 3001 Heverlee - BELGIUM
> www.intermodalics.eu
>
>
>

[OROCOSUSERS] FileDescriptorActivity

Hi Johannes

Thank you for your prompt answer.
I had a look at the example. I made a fork of your example here:

https://github.com/keivanzavari/file_descriptor_activity_test

I configure the component and start the deployer. Then I start the
component.

Then I get errno 9. The problem is I think because the socket is not a
global variable for the component but a property of an object of another
class. Is this correct?
The orocos component works like a charm when I just implement the methods &
properties as functions and variables of it. :)

Regards
Keivan

On 24 June 2016 at 12:35, Johannes Meyer <johannes [..] ...> wrote:

> Hi Keivan,
>
> I created a simple test package roughly following your example:
> https://github.com/meyerj/file_descriptor_activity_test
>
> I could not reproduce the EBADF (errno 9) errors with this package, at
> least using the most recent toolchain-2.9 version of RTT. Are you sure that
> server->sockfd_ is really a valid file descriptor? It is not important how
> you initialize and bind or connect the socket as long as it remains valid
> while it is being watched by the FileDescriptorActivity.
>
> Regards,
> Johannes
>
>
>
> On Fri, Jun 24, 2016 at 10:53 AM, Keivan Zavari <keivan [dot] zavari [..] ...>
> wrote:
>
>> Dear all,
>>
>>
>> For a project where I use UDP for communication, I thought I start very
>> structured and put my UDP send, receive and other definitions in a separate
>> class. Hence I simply have an UDP object while send and receive are member
>> functions and I define the socket and bind to it in the constructor.
>>
>> Now I am trying to use this class in an orocos component and use
>> FileDescriptorActivity to watch the UDP socket. The problem was first to
>> figure out how prevent destructing the UDP object. So I reset the object
>> with
>>
>> server_.reset(new UDPServer(comm_addr_, comm_port_));
>>
>> socket which in constructor is made as:
>>
>> sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
>>
>> is a public member of the class and is defined in the constructor of
>> UDPServer.
>>
>> According to the documentation I define my activity in configureHook as:
>>
>> fd_activity = new RTT::extras::FileDescriptorActivity();
>> this->setActivity(fd_activity);
>> fd_activity = dynamic_cast<RTT::extras::FileDescriptorActivity*>(this->
>> getActivity());
>>
>> and I tell it to watch the socket:
>>
>> fd_activity->watch(server->sockfd_);
>> fd_activity->setTimeout(1000);
>>
>> I keep getting *error in select() errno 9* which apparently means the
>> file descriptor is not valid... Now my question is, what is best way to
>> watch the socket? Do I need to implement the whole connection and its
>> methods inside the component or is there a better way?
>>
>> Thanks.
>>
>> Regards
>> Keivan
>>
>
>
>
> --
> Johannes Meyer, Roboticist
> +32 468 139828
> Intermodalics - Gaston Geenslaan 9, 3001 Heverlee - BELGIUM
> www.intermodalics.eu
>