PtAppAddInput() and Arguments

Hello,

I am passing a non-photon message to a GUI, which utilizes the
PtAppAddInput() function to register an input handler. However,
though this might be fundamentals of C programming, I cannot figure
out how to access all the members of the message I am passing.

The message sent is a structure of the form

struct _my_data_t
{
char *str;
int data;
};

and I often need to pass a string of characters (such as an error
message) to the GUI. However, in the input handler, I have the
following code,

int NonPtMsgHandler( void *data, int rcvid, void *msg, size_t msglen)
{
struct _my_data_t *rcvd_msg=(struct _my_data_t *)msg;
int number=rcvd_msg->data;
char *text = ???

return 1;
}

I am not sure how to access the pointer of the text sent to the GUI.
Can anyone help? Thank you.

Matthew.

m2asseli wrote:

I am passing a non-photon message to a GUI, which utilizes the
PtAppAddInput() function to register an input handler. However,
though this might be fundamentals of C programming, I cannot figure
out how to access all the members of the message I am passing.

Yes, it is pretty fundamental… The situation is very similar to
saving the data to a file and reading them back later on, perhaps in a
different process.

The message sent is a structure of the form

struct _my_data_t
{
char *str;
int data;
};

I am not sure how to access the pointer of the text sent to the GUI.
Can anyone help? Thank you.

Sending a pointer is no good – the pointer won’t point to the same
string in the other process. You need to send the characters; the
easiest way is by copying them into an array first:

struct _my_data_t
{
char str[ SOME_MAXIMUM_LENGTH ];
int data;
};

So there is no way to send a pointer to the GUI input handler that
will point to the text in the other process?

m2asseli wrote:

So there is no way to send a pointer to the GUI input handler that
will point to the text in the other process?

If the both the GUI and non-GUI threads are in the same process, you

should be able to send a pointer to a text string if text string is
global to both the gui and non-gui threads. If the threads are in
different processes, then the text string would have to be in shared memory.
(I know, I know; using global variables! bad programmer, bad, bad!)

m2asseli wrote:

So there is no way to send a pointer to the GUI input handler that
will point to the text in the other process?

There are ways, but there is no general and simple way. If you’re
sending from another thread in the same proces, it’ll just work,
provided that you make sure that the sending thread doesn’t have a
chance to clobber the string before the receiving thread is done looking
at it. If you have two processes but one is a forked child of the
other, pointers to static strings will work, too. But if the two
processes are running two different programs, you’d have to put your
strings in a shared memory object and make sure that both processes mmap
it in at the same address – which is neither easy nor recommended.

Thanks for the information. I gather I am having the problem because
I have two separate processes, and the pointer is not to a shared
memory location.

With regards to this shared memory, which you do not recommend…I
have used some shared memory variables between two processes, because
I assumed it would be quicker to shared the information in that manner
than to send a message with the data. I haven’t encountered any
problems to date, but you still recommend not using shared memory? I
am sharing approximately a dozen or so decimal values between the two
processes, would it be safer and just as quick to send a message with
the data?

Thanks again. Matthew.

“m2asseli” <m2asselin@yahoo-dot-com.no-spam.invalid> wrote in message
news:d0aerl$5ec$1@inn.qnx.com

Thanks for the information. I gather I am having the problem because
I have two separate processes, and the pointer is not to a shared
memory location.

With regards to this shared memory, which you do not recommend…I
have used some shared memory variables between two processes, because
I assumed it would be quicker to shared the information in that manner
than to send a message with the data. I haven’t encountered any
problems to date, but you still recommend not using shared memory? I
am sharing approximately a dozen or so decimal values between the two
processes, would it be safer and just as quick to send a message with
the data?

I would go with the messaging. And why not send the value in float or
integer instead of string. Let the GUI decide how it’s formated.

Using messaging gives you the added flexibility of being able to have the
two processes on different machines.

Thanks again. Matthew.

m2asseli wrote:

Thanks for the information. I gather I am having the problem because
I have two separate processes, and the pointer is not to a shared
memory location.

With regards to this shared memory, which you do not recommend…I

No, shared memory is fine (provided you know you’ll never need to run
the two processes on two different machines in the network). What I
meant is not recommended is sending pointers to shared memory in
messages between processes.

If you have a small amount of data and you need to send a message
anyway, just send the data in the message. If you have a large amount
of data and you think that it’ll be faster to send a small message to
tell the other process to look at the data in shared memory, don’t send
a pointer – send an offset into the shared memory block instead. This
way, you don’t have to go through hoops to make sure that both processes
see the shared memory at the same address. Mapping a shared object is
relatively safe and easy; mapping it at the same address in two
different processes is much trickier.

have used some shared memory variables between two processes, because
I assumed it would be quicker to shared the information in that manner
than to send a message with the data. I haven’t encountered any
problems to date, but you still recommend not using shared memory? I
am sharing approximately a dozen or so decimal values between the two
processes, would it be safer and just as quick to send a message with
the data?

It depends on what exactly you need to be faster. Accessing a value
that sits in memory is a lot faster than sending it in a message. But
when the value is in shared memory, it has no way to tell process A that
process B has just changed it. It also has no way to tell process A
that process B is half way through changing it, and that process A
shouldn’t look at it until process B is done changing it. If you want
to use shared memory instead of messages, there’s a good chance that
you’ll need to put a mutex and perhaps a condition variable in the
shared memory to ensure proper synchronization and notification;
otherwise, you’ll end up with a program that works 99.9% of the time and
does something horribly bad once every two weeks – but only on the
customer’s machine, and never when you’re looking at it in the debugger.

In short, shared memory can be faster than messages, but using it safely
is unlikely to be simpler than sending messages. Whether you need the
complexity depends on how much speed you really need.

(BTW If you care about speed, why are you using strings? Accessing a
binary value is much faster than parsing a string of decimal digits.)

m2asseli wrote:

Thanks for the information. I gather I am having the problem because
I have two separate processes, and the pointer is not to a shared
memory location.

With regards to this shared memory, which you do not recommend…I
have used some shared memory variables between two processes, because
I assumed it would be quicker to shared the information in that manner
than to send a message with the data. I haven’t encountered any
problems to date, but you still recommend not using shared memory? I
am sharing approximately a dozen or so decimal values between the two
processes, would it be safer and just as quick to send a message with
the data?

Thanks again. Matthew.

Hello Matthew,

You can to use something like this:
Messge structure:
#define MY_MSG_TYPE ??? // Now I don’t know, where is correct space
for user message type, for experiment we can use value 0x200. Don’t use
this values: 0x000-0x0FF - SYSMGR, PROCMGR, MEMMGR, PATHMGR, CPUMGR,
RSRCDBMGR; 0x100-0x1FF - IOMSG; 0x700-??? - PHOTON; 0xA00-0xA80 - PCCARD;

struct _my_data_t
{
msg_t type;
size_t TextLen;
int data;
/* char Text[TextLen]; this line is only for information about
message format */
};

Similar construction you can to find in iomsg.h struct _io_write

For send message use:
io_t wiov[2];

my_data.type = MY_MSG_TYPE;
my_data.TextLen = strlen( Text ) + 1;
my_data.data = ???;
SETIOV( wiov + 0, &my_data, sizeof( struct _my_data_t ) );
SETIOV( wiov + 1, Text, my_data.TextLen );
MsgSendv( fd, wiov, 2, NULL, 0 );

For read message
int NonPtMsgHandler( void *data, int rcvid, void *msg, size_t msglen)
{
struct _my_data_t mydata;
msg_t *msg_type;
int number=rcvd_msg->data;
iov_t riov[1];
char *text;

// Check if message is my
msg_type = (msg_t *) msg;
if( (*msg_type) != MY_MSG_TYPE )
return Pt_CONTINUE;

// check if you have whoe message in buffer or you can to use
PtResizeEventMsg for allocate enough space when application is start.
Then you can use struct _my_data_t *my_data_ptr = (struct my_data_t *) msg;

if( msglen < sizeof( struct _my_data_t ) )
{
// read message again
SETIOV( riov + 0, &my_data, sizeof( struct _my_data_t ) );
MsgRead( rcvid, riov, 1, 0 );
}
else
{
// make only copy form buffer
my_data = * ((struct _my_data_t *) msg);
}

// Allocate text buffer
text = ( char * ) malloc (rcvd_msg->TextLen );

// Read text
SETIOV( riov + 0, text, rcvd_msg->TextLen );
MsgReadv( rcvid, riov, 1, sizeof( struct _my_data_t) );

printf( “I have message: %s”, text );

free( text );
return Pt_HALT;
}

I hope it will help to you
Marian.