What happens to a ZeroMQ multipart message when send fails before the final part? - c

I'm intending to use multi-part messages in ZeroMQ, but I need to know what happens to the initially enqueued message parts when I get a send error before the last message part is sent.
For example, lets say I have a PUSH socket and I am sending a two part message because I am collecting the header and body from different sources. What happens if the header sends fine, but there is an error sending the body? Does the header remain in the socket's outbound queue until I attempt to send another message, or does the header get dropped due to the error in the body send?
Perhaps some code will make the question more precise:
int header[] = {1, 2, 3}; // <- Header From Source 1
int body[] = {4, 5, 6, 7, 8, 9}; // <- Body From Source 2
void *ctx = zmq_ctx_new();
void *push = zmq_socket(ctx, ZMQ_PUSH);
zmq_connect(push, "tcp://localhost:7890");
int headerSent = 0;
int bodySent = 0;
headerSent = zmq_send(push, &header[0], sizeof(header), ZMQ_SNDMORE);
if (-1 != headerSent)
{
bodySent = zmq_send(push, &body[0], sizeof(body), 0);
// What if ^-- this fails?
}
// Do some other processing to prepare a new header and body here...
headerSent = zmq_send(push, &header[0], sizeof(header), ZMQ_SNDMORE);
if (-1 != headerSent)
{
bodySent = zmq_send(push, &body[0], sizeof(body), 0);
}
Is it possible for me to have a message with two headers here, or does ZeroMQ discard the initial message parts that were submitted with the ZMQ_SNDMORE flag when a subsequent send fails due to (e.g.) interruption from a signal? I'm hoping that is the case because the documentation on zmq_send promises that all message parts will be delivered, or none of them will be delivered. However, I'm not sure how the library decides which message parts are from the same message. Does it consider two message parts to be from the same message if a send error occurs in between them?

ZeroMQ Guide:
When you send a multipart message, the first part (and all following parts) are only actually sent on the wire when you send
the final part.
You will receive all parts of a message, or none at all.
What you could always do is send single part messages and assemble them and manage failure on the receiving end
Recent update: Pieter Hintjens: The End of ZeroMQ Multipart

I don't know exactly what will happen, however logic dictates one of 3 outcomes:
The 2nd frame, which failed, will have successfully processed the 0 in the multipart parameter instead of ZMQ_SNDMORE. The first message will be dropped, and the 2nd message will start anew.
The 2nd frame, which failed, will not have successfully processed the 0 in the multipart parameter instead of ZMQ_SNDMORE. The first message will still be open, your 2nd "header" will be frame 2, your 2nd "body" will be frame 3, and the message will just send with 3 frames instead of 2, erroneously
ZMQ will have its pants around its ankles, and be in some error state which will respond in unexpected ways.
My expectation is that the 2nd possibility is what will happen, if the call fails that it should produce zero side effects so it will be as if it never happened. This is supported by the info on the man page talking about the several error types that you might encounter. In particular it looks like it should be a very rare/exceptional situation where the first part of a multipart message would be accepted by the 2nd would not.

Related

Can't send Raw Telegram Request through CAPL on CANoe

EDIT: The main problem has been solved, but I stilla have a question, check the third attempt to see it.
I'm trying to send a Diagnostic Request that is not defined on my Diagnostic Description.
I have the following on my script:
variables
{
//Diagnostic Request that doesn't exist on the .cdd
diagRequest ReadParameter Parameter_Req;
}
on preStart
{
//Sets Diganostic Target just as it was configured
diagSetTarget("DUT");
}
on key 's'
{
//Setting request size to 3 bytes
//I asigned the size to a variable to be able to read which value it had after resizing if but
//everytime I got 0xFF9E or something like that the case is it seems the diagResize is not working
diagResize(Parameter_Req,0x3);
//Setting bytes on the request to creat 22 05 70 (read by identifier)
Parameter_Req.SetPrimitiveByte(0,0x22);
Parameter_Req.SetPrimitiveByte(1,0x05);
Parameter_Req.SetPrimitiveByte(2,0x70);
//Send Request
diagSendRequest(Parameter_Req);
}
But the request is never sent, nothing new is seen on the Trace window. Does anybody know what I am doing wrong? I tried this with a Diagnostic Request that is declared on the Diagnostic Description and it works the request is sent, so I know my diagnostic configuration is OK. Also, no error is reported by CANoe
Thanks for your help
Edit: I also tried this other way
variables
{
byte ReadDID0570[3];
}
on preStart
{
//Sets Diganostic Target just as it was configured
diagSetTarget("DUT");
}
on key 's'
{
//Set bytes and Send Read Request
ReadDID0570[0] = 0x22;
ReadDID0570[1] = 0x05;
ReadDID0570[2] = 0x70;
//Send request
DiagSendRequestPDU(ReadDID0570, elCount(ReadDID0570));
}
But the result the same absolutely nothing happens.
Edit After the suggestion of M. Spiller
variables
{
diagRequest * Parameter_Req;
}
on preStart
{
//Sets Diganostic Target just as it was configured
diagSetTarget("DUT");
}
on key 's'
{
//Resize the request to three bytes
diagResize(Parameter_Req,0x3);
//Set bytes
Parameter_Req.SetPrimitiveByte(0,0x22);
Parameter_Req.SetPrimitiveByte(1,0x05);
Parameter_Req.SetPrimitiveByte(2,0x70);
//Send Request
diagSendRequest(Parameter_Req);
}
This worked! The request is sent, although is not showed in the Trace window, I know it was sent because the response could be seen on Trace. Now my only question is how can I use diagGetLastResponse(Parameter_res); and on diagResponse Parameter_res using this same method to declare the response?
diagResponse * Parameter_Res;
Because those functions receive the name of the request/response declared on the Diagnostic Description, but using this method the type of request is * so how do I use it?
You have used diagGetLastResponse(Parameter_res) to save the response to the Parameter_res variable. Since this is a variable declared with *, you won't have access to the parameters as specified in your Diagnostic Description.
You can make use of the function diagInterpretRespAs to convert this response variable to a suitable class according to your description file. After this, you can use diagGetParameter to get the parameter with the resolution and offset considered.
Otherwise, you can simply use the raw response variable and use diagGetPrimitiveByte to access the bytes in the response.

RxFrameNtf, TxFrameNtf and Ntf.data in unetpy

I am using Unetstack software along with Unetpy. I wish to retrieve transmit and recieve notifications when I run .py file which imports Unetpy python library. I followed this tutorial
I am successfully able to connect to the localhost and print values like phy.MTU and so on. When I transmit a packet I also receive a reply saying AGREE on the command prompt.output_of_my_script
my_script
Can you please help me in receiving Txframentf and rxframentf along with data payload.
I have made changes posted in bug reports suggested in this linkeven.
Please guide me on how to print notifications for rxframe and txframe.
Thank you``
Your script is fine until the last line:
print(phy << org_arl_unet_phy.TxFrameNtf())
Here you are trying to send a TxFrameNtf to the physical agent. This does not make sense, as it is the physical agent who sends you such a notification when a transmission is completed.
By the time you reach this line, you should have already received the notification as txntf as long as the transmission was completed within 5 seconds (timeout=5000). To print out the notification, all you need to do is:
print(txntf)
I just tested this against the 3-node-network.groovy sample. I am using unetpy-1.3b5 and fjagepy-1.4.2b3. Here's the modified code:
from unetpy import *
modem = UnetGateway('localhost', 1102)
phy = modem.agentForService(Services.PHYSICAL)
print(phy.MTU)
print(phy.basebandRate)
print(phy << org_arl_unet_phy.TxFrameReq(to=3, data=[1,2,3,4]))
txntf = modem.receive(timeout=5000)
print(txntf)
and the output:
16
4096
AGREE
TxFrameNtf:INFORM[type:1]
You can see that the TxFrameNtf is correctly received.
For reception, you need to subscribe to the agent's notifications and then receive a frame:
modem.subscribe(phy)
rxntf = modem.receive(org_arl_unet_phy.RxFrameNtf, timeout=5000)
print(rxntf)
Assuming you receive a frame within the 5 second timeout specified (in this example, on node 3), this should print out something like:
RxFrameNtf:INFORM[type:CONTROL from:1 to:3 protocol:0 rxTime:34587658 (4 bytes)]
You sent a datagram through some agent that supports the DATAGRAM service. There may be many agents that support this service (not just the physical layer). In any case, that datagram would be received on a different node, and so you wouldn't expect to receive DatagramNtf on the transmitting node.
The RangeReq should yield a RangeNtf if successful, but that might take more than the default receive timeout of 1 second, depending on how far node 2 is. So you might want to try a longer receive timeout to see if you get your notification.
To access the data from payload from the rxntf, you can try print(rxntf.data).

What exactly does multicast() do?

What's the exact difference between
from("stream:in")
.to("stream:out", "stream:err");
and
from("stream:in")
.multicast()
.to("stream:out", "stream:err");
?
In this case - no real difference, since the incoming message body for the stream camel component seems to always be sent onwards as the outgoing message body ;)
Imagine however a more substantial case, for example:
from("stream:in")
.to("direct:one", "direct:two");
In this case, whatever is received on the stream is first sent to route direct:one. Now, if that route modifies the message in some way (e.g. setBody(constant("modified")), then route direct:two will received the modified outgoing message from the route direct:one.
Think of it like this: stream:in -> direct:one -> direct:two.
Multicast
from("stream:in")
.multicast()
.to("direct:one", "direct:two");
In contrast, with multicast, whatever is received on the stream is firstly sent to direct:one, and that same message body from the stream (as a copy) is sent to direct:two - regardless of what direct:one sets as it's outgoing message body.
We can think of the multicast like this:
stream:in -----> direct:one
\----> direct:two

How to use the binary push notifications format by C?

I use the The Push Notification Binary Interface cmd=2
This is format :
Q1: Can I send some device_id in one frame? For example:
item id = 1 , device_tocken #1
item id = 1 , device_tocken #2
item id = 1 , device_tocken #3
item id = 2 , message
item id = 3 ...
and etc
Q2: How I can receive the response error ?
The documentation said: If you send a notification that is accepted by APNs, nothing is returned.
If I make SSL_read after SSL_write and package was accepted by APNs, the program is waiting in SSL_read command.
r = SSL_write(ssl, out_buffer, size);
int len = SSL_read(ssl, in_buff, 6);
If I read from ssl channel into single thread - I have segmentation fault.
Q3: Do You know the link to example of use this protocol?
It's not clear from the documentation, but I don't think you can send multiple device tokens in the same frame, simply because if you get an error response of an invalid device token, you won't be able to know which device token it refers to. If, on the other hand, your frame contains a single device token and a single message identifier, then an error response containing that message identifier will tell you exactly which message caused the error.
You should use a non-blocking read for attempting to read the error response. I don't know how you write that in C, but there must be a way to specify some timeout or to call a read method that specifies a timeout. If there is nothing to read, the method will return after the timeout is elapsed.
The APNS docs contain samples for sending notifications in the older formats (0 and 1). I suggest you use format 1 (which supports error responses), since I don't see any advantage of using the newer format 2.

Writing a GSM modem driver?

I've been working on an application which uses a GSM modem for one of two things; check its status using the built in HTTP stack by sending a GET request to the server, or sending data to the server (using UDP). I have tried several different methods to keep this as reliable as possible, and I'm finally ready to ask for help.
My application is written for the SIMCOM908 module and the PIC18 platform (I'm using a PIC18 Explorer for development).
So the problem is sometimes the modem is busy doing something, and misses a command. As a human, I would see that and just resend the command. Adding a facility for my MCU to timeout and resend isn't an issue.
What is an issue is that the modem sends unsolicited responses after different events. When the modem changes registration status (with the cell tower) it would respond with +CGREG: 1, ... or when the GPS is ready GPS Ready. These responses can happen at any time, including in the middle of a command (like creating an IP connection).
This is a problem, because I haven't thought of a way to deal with this. My application needs to send a command (to connect to the server for example, AT+CIPSTART="UDP","example.com",5000) This command will response with 'OK', and then when the command has finished 'CONNECT OK'. However, I need to be able to react to the many other possible responses, and I haven't figured out a way of doing this. What do I need to do with my code to; wait for a response from the modem, check the response, perform an action based on that response?
I am code limited (being an 8-bit microcontroller!) and would like the keep repetition to a minimum. How can I write a response function that will take a response from the GSM module (solicited or now) and then let the rest of my program know what is happening?
Ideally, I'd like to do something with those responses. Like keep an internal state (when I hear GPS Ready, I know I can power the GPS etc.
Maybe there are some things I should think about, or maybe there's an open source project that already solves this problem?
Here's what I have so far:
/* Command responses */
enum {
// Common
OK = 0,
ERROR,
TIMEOUT,
OTHER,
// CGREG
NOT_REGISTERED,
// CGATT
NOT_ATTACHED,
// Network Status
NO_NETWORK,
// GPRS status
NO_ADDRESS,
// HTTP ACTION
NETWORK_ERROR,
// IP Stack State
IP_INITIAL,
IP_STATUS,
IP_CONFIG,
UDP_CLOSING,
UDP_CLOSED,
UDP_CONNECTING
} gsmResponse;
int gsm_sendCommand(const char * cmd) {
unsigned long timeout = timer_getCurrentTime() + 5000;
uart_clearb(GSM_UART); // Clear the input buffer
uart_puts(GSM_UART, cmd); // Send the command to the module
while (strstr(bf2, "\r") == NULL) { // Keep waiting for a response from the module
if (timeout < timer_getCurrentTime()) { // Check we haven't timed out yet
printf("Command timed out: %s\r\n", cmd);
return TIMEOUT;
}
}
timer_delay(100); // Let the rest of the response be received.
return OK;
}
int gsm_simpleCommand(const char * cmd) {
if (gsm_sendCommand(cmd) == TIMEOUT)
return TIMEOUT;
// Getting an ERROR response is quick, so if there is a response, this will be there
if (strstr(bf2, "ERROR") != NULL)
return ERROR;
// Sometimes the OK (meaning the command ran) can take a while
// As long as there wasn't an error, we can wait for the OK
while (strstr(bf2, "OK") == NULL);
return OK;
}
A simple command is any AT command that is specifically looking for OK or ERROR in response. Something like AT. However, I also use it for more advanced commands like AT+CPIN? because it means I will have captured the whole response, and can further search for the +CPIN: READY. However, none of this actually response to the unsolicited responses. In fact, the gsm_sendCommand() function will return early when the unsolicited response is received.
What a good way to manage complex, occasionally unsolicited, status messages like this? Please take note that this application is written in C, and runs on an 8bit microcontroller!
Having to handle both unsolicited messages as well as responses to requests in the same data stream is difficult since you will need to demultiplex the incoming stream and dispatch the results to the appropriate handler. It's a bit like an interrupt handler in that you have to drop what you were doing and handle this other bit of information which you were not necessarily expecting.
Some modules have a secondary serial port which can also be used for messages. If this is possible you could have unsolicited messages only appear on a single serial port while the main port is for your AT commands. This may not be possible, and some GSM modules will not support the complete command set on a secondary port.
Perhaps a better approach is to just disable unsolicited messages. Most commands all the state to be requested. eg While waiting for registration, instead of waiting for an unsolicited registration message to appear, simply poll the module for the current registration state. This allows you to always be in control, and you only have to handle the responses for the command just sent. If you're waiting for multiple events you can poll in a loop for each item in turn. This will generally make the code simpler as you only have to handle a single response at a time. The downside is that your response times are limited by your polling rate.
If you're set on continuing with the unsolicited message approach, I'd suggest implementing a small queue for unsolicited messages. While waiting for responses to a command, if the response does not match the command, just push the response on a queue. Then, when you've either received a response to your AT command or timed out you can process the unsolicited message queue afterwards.

Resources