[zeromq-dev] Non-Blocking REQ/REP Server
Lee Sylvester
lee.sylvester at gmail.com
Tue Feb 19 23:04:51 CET 2013
Okay, so this brings me kinda to where I was before:
int read_zmq_connections() {
zmq_pollitem_t items [] = {
{ zmq_responder, 0, ZMQ_POLLIN, 0 }
};
errno = 0;
int ret = 0;
while ( errno == 0 ) {
zmq_poll(items, 1, 0);
if (items [0].revents & ZMQ_POLLIN) {
char *str = s_recv(zmq_responder);
add_remote_account(str);
free(str);
++ret;
} else {
break;
}
}
return ret;
}
So, I poll the events and, if an error occurs or if there is no event, then I return the number of messages retrieved. Have I missed something?
Thanks,
Lee
On 19 Feb 2013, at 17:11, Charles Remes <lists at chuckremes.com> wrote:
> It looks better except for the use of "size" in the loop control. The return code from zmq_poll is the *number of sockets* that have pending events. The way you are using it appears as though you believe the return code indicates the *number of messages* which is incorrect. The way the loop is written now, it will loop once and exit at most.
>
>
> On Feb 19, 2013, at 10:29 AM, Lee Sylvester <lee.sylvester at gmail.com> wrote:
>
>> Okay, thank you. So I now have the following:
>>
>> int read_zmq_connections() {
>> zmq_pollitem_t items [] = {
>> { zmq_responder, 0, ZMQ_POLLIN, 0 }
>> };
>> errno = 0;
>> int ret = 0, size = zmq_poll(items, 1, 0);
>> zmq_msg_t message;
>> while ( size > 0 && errno == 0 ) {
>> char *str = s_recv(zmq_responder);
>> parse_new_data(str);
>> free(str);
>> ++ret;
>> --size;
>> }
>> return ret;
>> }
>>
>> I'm going to test it a little later once I've written my client code. :-)
>>
>> Thanks again.
>>
>> Cheers,
>> Lee
>>
>>
>>
>> On 19 Feb 2013, at 16:21, Charles Remes <lists at chuckremes.com> wrote:
>>
>>> Yes, you are missing out on being able to differentiate between reads & writes since you aren't checking the revents. However, in your case you only have a single socket and you only register for POLLIN, so you can just use the return code and skip the hard stuff. Any time it returns 1 then you know that your socket is readable.
>>>
>>>
>>> On Feb 19, 2013, at 10:18 AM, Lee Sylvester <lee.sylvester at gmail.com> wrote:
>>>
>>>> Okay, thanks. I'm looking at the reference and I can see that zmq_poll returns the number of items, but it feels like I'm missing something when I rely on that :-S
>>>>
>>>> Lee
>>>>
>>>>
>>>> On 19 Feb 2013, at 15:52, Charles Remes <lists at chuckremes.com> wrote:
>>>>
>>>>> Hmmm, I'm not sure that's exactly right.
>>>>>
>>>>> The basic idea is that you want to check the return code from zmq_poll. If it is greater than 0, then the socket can be read from. You should then read from the socket until no more messages are available. I don't know how it works with the #s_recv() function (presumably that is part of the czmq binding) but you want to read until the socket is empty or you get EAGAIN. Perhaps that function does that for you under the covers.
>>>>>
>>>>> So, the loop should be around reading from the socket and *not* around zmq_poll. Does that make sense?
>>>>>
>>>>>
>>>>>
>>>>> On Feb 19, 2013, at 9:16 AM, Lee Sylvester <lee.sylvester at gmail.com> wrote:
>>>>>
>>>>>> Thank you, that's great. So, based on what I've read, does this look correct for what I'm trying to accomplish?
>>>>>>
>>>>>> int read_zmq_connections() {
>>>>>> zmq_pollitem_t items [] = {
>>>>>> { zmq_responder, 0, ZMQ_POLLIN, 0 }
>>>>>> };
>>>>>> while (1) {
>>>>>> zmq_msg_t message;
>>>>>> zmq_poll(items, 1, 0);
>>>>>> if (items[0].revents & ZMQ_POLLIN) {
>>>>>> char *str = s_recv(zmq_responder);
>>>>>> parse_new_data(str);
>>>>>> free(str);
>>>>>> } else {
>>>>>> break;
>>>>>> }
>>>>>> }
>>>>>> return 0;
>>>>>> }
>>>>>>
>>>>>> Thanks,
>>>>>> Lee
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> On 19 Feb 2013, at 14:52, Charles Remes <lists at chuckremes.com> wrote:
>>>>>>
>>>>>>> Take a look at the man page for zmq_poll. You can do a non-blocking poll for incoming messages on your socket. If it returns immediately with 0, then no sockets in your pollset have pending messages to read.
>>>>>>>
>>>>>>> Be aware that when zmq_poll does indicate that you have messages, you must read *all* of them from the socket before zmq_poll will work again. I'm pretty sure the man page explains this.
>>>>>>>
>>>>>>> Good luck.
>>>>>>>
>>>>>>> On Feb 19, 2013, at 8:44 AM, Lee Sylvester <lee.sylvester at gmail.com> wrote:
>>>>>>>
>>>>>>>> Hey guys,
>>>>>>>>
>>>>>>>> So, I've integrated ØMQ into my server. Now, I want to use ØMQ as a means to supply information to a HTTP server from a separate management app. So, in theory, it will look something like this
>>>>>>>>
>>>>>>>> int read_zmq_connections() {
>>>>>>>> int ret = 0;
>>>>>>>> while (zmq_has_messages(zmq_responder)) {
>>>>>>>> char *str = s_recv(zmq_responder);
>>>>>>>> parse_new_data(str);
>>>>>>>> free(str);
>>>>>>>> ++ret;
>>>>>>>> }
>>>>>>>> return ret;
>>>>>>>> }
>>>>>>>>
>>>>>>>> This way, if there are no messages on zmq_responder, then the function will simply return. What I don't know how to do (and can't quite find) is how to check if messages exist on the connection. Can anyone please point me in the right direction?
>>>>>>>>
>>>>>>>> The reason why I need this non-blocking is that I will only be calling 'read_zmq_connections' approximately once every five minutes and I don't want my app to hang while waiting for messages.
>>>>>>>>
>>>>>>>> Thanks loads in advance,
>>>>>>>> Lee
>>>>>>>> _______________________________________________
>>>>>>>> zeromq-dev mailing list
>>>>>>>> zeromq-dev at lists.zeromq.org
>>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> zeromq-dev mailing list
>>>>>>> zeromq-dev at lists.zeromq.org
>>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>>
>>>>>> _______________________________________________
>>>>>> zeromq-dev mailing list
>>>>>> zeromq-dev at lists.zeromq.org
>>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>>
>>>>> _______________________________________________
>>>>> zeromq-dev mailing list
>>>>> zeromq-dev at lists.zeromq.org
>>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>>
>>>> _______________________________________________
>>>> zeromq-dev mailing list
>>>> zeromq-dev at lists.zeromq.org
>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>
>>> _______________________________________________
>>> zeromq-dev mailing list
>>> zeromq-dev at lists.zeromq.org
>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>
>> _______________________________________________
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
More information about the zeromq-dev
mailing list