Discuss the below:
Q: Implementation Approach to Class Communication
Interested in a sample program where two classes communicate with each other per the following requirements:
Consider two classes: Foo and Bar, where Foo is a sender, and Bar the receiver.
Foo allocates 3-1 megabyte chunks of memory. The first chunk is filled with the value 0xA5, the next chunk with value 0xB5 and the third - 0xC5.
In a header file, that Foo will transmit to Bar. The struct _order_ will advise Bar of the order in which to expect the 3 1 MiB chunks.
So now:
chunk_id size
0 1 megabyte
1 1 megabyte
2 1 megabyte
struct order { unsigned int chunk_id; unsigned int size; };
struct process{ unsigned int chunk_id; unsigned int step; };
struct header_info
{
order co[3];
process po[3];
};
header_info hdr;
hdr.co[0].chunk_id = 0;
hdr.co[0].size = 0x100000;
hdr.co[1].chunk_id = 1;
hdr.co[1].size = 0x100000;
hdr.co[2].chunk_id = 2;
hdr.co[3].size = 0x100000;
Similarly, Foo will fill out the processing order via the process struct. The process struct will advise Bar of when to 'start processing'.
So now - assume the order is as follows:
chunk_id steps
1 2
0 1
2 1
hdr.po[0].chunk_id = 1;
hdr.po[0].step = 2;
hdr.po[1].chunk_id = 0;
hdr.po[1].step = 1;
hdr.po[2].chunk_id = 2;
hdr.po[3].step = 1;
So the header - header_info (first) gets transmitted from Foo to Bar.
Upon receipt of this header Bar is aware of the order the information will arrive based on the struct _order_. Bar also knows the order to start processing based on the struct process.
So now - Foo transmits the data to Bar. i.e 1 megabyte of 0xA5. Upon receipt of the data, Bar will 'store the data', then check the initial chunk_id of order (0) versus the chunk_id of process (1). In this case, both id's aren't equivalent so Bar is available for more data. To do this. Bar informs Foo of it's availability to receive more data.
Foo then transmits the next chunk corresponding to chunk_id 1. Data values 0xB5. Upon receipt of the data, Bar - yet again, 'stores the data', then checks the chunk_id of order (1) versus the chunk_id of process (1). It's a match. Bar will now start processing. Processing here is simple. Bar will multiply the data - in this case - 0xB5 by 2. Bar will do this twice since the parameter 'step is set to 2'. Bar will 'move to the next processing chunk_id'. Per the example the id is 0. Since Bar has already received chunk_id 0 (Bar will need to do housekeeping to keep track of chunk_id's received based on the order struct), Bar will process the data. Processing here is multiplying 0xA5 by 3. We'll do this once, since step is 1.
At this point Bar informs Foo of it's availability to receive more data.
At this point Foo transmits data value 0xC5 to Bar. Bar will once again validate (always validate to ensure we didn't receive a bogus chunk_id) the chunk_id of 'order' with the chunk_id of 'process'. In this case both id's are 2 (recall that we're -i.e Bar - told in advance the order the data will arrive - via the order struct. That said it's up to Bar to do all the housekeeping when to start processing and if it's ok to continue processing based on previously received data). Bar once again does processing. i.e multiplies 0xC5 by 4 and doing so once (step 1)