Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C processor.c #include processor.h MESSAGE messageCache[CACHE_SIZE]; void addMessageToCache(char *inputLine) { // TODO See the description of the Task 10 } void messageDispatcher(void) { //

in C

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 4 #define NUM_OF_DOUBLES 5 #define NUM_OF_STRINGS 3 #define LENGTH_OF_STRINGS 7 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]; double 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

#include "processor.h" int main(void) { char inputBuffer[BUFFER_SIZE]; while(true) { // get a line of input from the user printf("Enter a message, or type \"%s\" to stop > ", SENTINEL); scanf(" %[^ ]", inputBuffer); inputBuffer[BUFFER_SIZE-1] = '\0'; // ensure buffer is null-terminated if (strncmp(inputBuffer, SENTINEL, SENTINEL_LEN) == 0) { // stop when the SENTINEL is read in, but still process the remaining messages first messageDispatcher(); printStatistics(); break; } // add the message to the cache addMessageToCache(inputBuffer); } } 

Your task is to implement a program that processes batches of messages.

Messages will come in 4 types:

  • MSG_TYPE_1 : An arbitrary string
  • MSG_TYPE_2 : 4 integers, separated by spaces
  • MSG_TYPE_3 : 5 doubles, separated by spaces
  • MSG_TYPE_4 : 3 words, each containing 7 characters, separated by spaces

Each entered message will be preceded by an integer representing a type of message. A couple examples:

  • 1 I like to eat beans. Denotes a message of type MSG_TYPE_1 whose content is "I like to eat beans."
  • 2 1 2 3 4 Denotes a message of type MSG_TYPE_2 whose content is the 4 integers "1 2 3 4".

You will need to implement (in processor.c) a function addMessageToCache which reads a message in string form from the inputLine (where the user's input is placed) and parses it to populate an instance of the MESSAGE struct.

Note that addMessageToCache will need to know where in the messageCache array the new message should be populated, so you will need an integer to track the current index in the messageCache.

Also note that addMessageToCache will first need to ensure that there is room in the cache! If the cache is full, it should call the messageDispatcher (discussed below) to process / empty the cache.

These message will be stored in a cache (an array of messages with a preset size). When the cache is full (or the user quits), the messages in the cache will need to be processed. This will be done with two functions:

  • processMessage :
    • takes a MESSAGE * as an argument
    • prints the message type and message contents
    • increments counters (tracking how many of each message type has been processed).
    • frees any manually allocated message contents
  • messageDispatcher :
    • processes all new messages in the cache
    • increments a counter for the number of batches processed
    • resets the messageCache index to 0; now that all messages in the cache have been processed, we can overwrite them.

Finally, you will need to implemente the printStatistics function, which will be called at the end of a run and which should display:

  • The number of batches processed
  • The total number of messages processed
  • The number of each type of message processed

Your task is to complete the 4 functions described above, whose empty definitions are provided in processor.c. Everything in processor_test.c and in processor.h is done, and does not need to be edited (though you may wish to make the CACHE_SIZE in processor.h smaller, to make it easier to test with a full cache).

 
 

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_2

Step: 3

blur-text-image_3

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What is your view of spirituality in the workplace?

Answered: 1 week ago

Question

Explain the different types of marketing strategies.

Answered: 1 week ago

Question

Explain product positioning.

Answered: 1 week ago

Question

Explain Industrial market segment.

Answered: 1 week ago