Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C, implement a program that processes batches of messages in a loop as follows: reads a message from the standard input into inputBuffer, collects

Using C, implement a program that processes batches of messages in a loop as follows:

reads a message from the standard input into inputBuffer,

collects the messages in messageCache using the function addMessageToCache() that takes inputBuffer as a parameter,

when needed as explained in the next bullet, invokes a function messageDispatcher() to process a batch of messages that are in the cache one by one using the function processMessage(), and

continues to take batches of messages from the input until either:

the capacity of the cache (arbitrarily chosen to be some fixed value; for example 16) is reached in which case the message dispatcher should be called to process the messages in the buffer, and then to continue reading messages, or

a line with a sentinel END is read, in which case the program should process all remaining messages in the cache, and then terminate.

There should be one message per line of input (assume correct input for the valid type). Messages can be in a number of formats:

TYPE 1: a string of unknown size

TYPE 2: 5 integers

TYPE 3: 4 doubles

TYPE 4: 5 five-character words

The input should indicate the type of the message and the content; e.g.:

1 abcdefghijklmnopqrst 3 123.34 23.12345 5439.234 0.000231 4 aaaaa bbbbb ccccc ddddd eeeee 2 123 45 6 7890 87592

After receiving the sentinel your program should finish processing the messages in the buffer and print processing statistics:

the number of batches (including the last one even if incomplete),

the total number of messages processed, and

the number of messages of each type.

The processMessage() function should be invoked from messageDispatcher() for each message in the cache. It should take a single parameter that is the next message from the cache to process. It should recognize what type the message has, and then print the message type along with its content as follows:

for type 1, the complete string,

for type 2, all integers separated by commas,

for type 3, all doubles separated by a slash, and

for type 4, all words separated by spaces.

For example:

TYPE 1: abcdefghijklmnopqrst TYPE 2: 123,45,6,7890,87592 TYPE 3: 123.340000/23.123450/5439.234000/0.000231 TYPE 4: aaaaa bbbbb ccccc ddddd eeeee

After all messages are processed (user entered the SENTINEL value) the final statistics should be printed by calling printStatistics() function:

Number of message batches: 1 Messages processed: Total: 4 Type 1: 1 Type 2: 1 Type 3: 1 Type 4: 1

The cache should be implemented as an array of the struct constructs that include two fields: one for the type of the message and the second should be a union construct to hold the content of the message that is specific to its type. An unsigned short should be used as the data type for the message type field. The type should be used in a switch statement to save the message in the union according to the message type.

processor.c should contain a global declaration of the message cache and a global declaration of a number of messages read, as well as implementations of the functions addMessageToCache() and messageDispatcher(), and processMessage().

addMessageToCache() should use sscanf() to read the type of the message and the message content from the parameter. To do that, an appropriate pattern for sscanf() needs to be designed. A switch statement should be used to select proper handler for each message type.

The program must use dynamic allocation for storing messages of type 1. The memory allocated for that purpose must be freed after the message is processed.

You should have processor.h file for declarations of data structures and declarations of common functions.

test_processor.c should include the main() with the testing driver that implements the main loop of the application.

processor.c

#include "processor.h"

MESSAGE messageCache[CACHE_SIZE];

void addMessageToCache(char *inputLine) { // TODO See the description of the Task 10 }

void messageDispatcher(void) { // TODO See the description of the Task 10 }

void processMessage(MESSAGE *message) { // TODO See the description of the Task 10 }

void printStatistics(void) { // TODO See the description of the Task 10

processor.h

#ifndef __PROCESSOR_H #define __PROCESSOR_H

#include #include #include #include

#define SENTINEL "END" #define SENTINEL_LEN strlen(SENTINEL) #define CACHE_SIZE 16 // TODO For testing, you may want to change it to a smaller value #define BUFFER_SIZE 256 #define NUM_OF_INTEGERS 5 #define NUM_OF_DOUBLES 4 #define NUM_OF_STRINGS 5 #define LENGTH_OF_STRINGS 5

typedef enum { MSG_TYPE_1 = 1, // a string of unknown size MSG_TYPE_2, // 5 integers MSG_TYPE_3, // 4 doubles MSG_TYPE_4, // 5 five-character words } MSG_TYPE;

#define NUMBER_OF_MSG_TYPES sizeof(MSG_TYPE)

typedef union { char *string; int integers[NUM_OF_INTEGERS]; float doubles[NUM_OF_DOUBLES]; char words[NUM_OF_STRINGS][LENGTH_OF_STRINGS + 1]; // +1 to accommodate EOS ('\0') } MSG_CONTENT;

typedef struct { MSG_TYPE type; MSG_CONTENT content; } MESSAGE;

void addMessageToCache(char *); void messageDispatcher(void); void processMessage(MESSAGE *); void printStatistics(void);

#endif // __PROCESSOR_H

processor_test.c

#include "processor.h"

int main(void) { char inputBuffer[BUFFER_SIZE];

while(true) { // get a line of input from he user scanf(" %[^ ]", inputBuffer); if (strncmp(inputBuffer, SENTINEL, SENTINEL_LEN) == 0) // stop when "END" is read in break; // add the message to the cache addMessageToCache(inputBuffer); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions