Question
In this task you will be implementing the functions in the msgpass.cpp which is given below You may reuse your code in the implementation of
In this task you will be implementing the functions in the msgpass.cpp which is given below
You may reuse your code in the implementation of
the functions from these tasks :
- https://www.chegg.com/homework-help/questions-and-answers/write-cpp-program-serverh-ifndef-serverh-define-serverh-include-include-include-include-us-q30759618
-https://www.chegg.com/homework-help/questions-and-answers/aim-task-remove-deadlock-code-implemented-https-wwwcheggcom-homework-help-questions-answer-q30856370
This task still has the same scenario where each client can send their piece to the printer, however this time you decide to use a message queue to emulate message passing
between the clients/threads. The idea is that after each client/thread has requested the piece from the server, that piece, along with the client/thread index, will be sent (in this
case by pushing them onto the message queue) to a single client/thread who is acting as a mediator for the print job.
Once all pieces have been pushed onto the queue, the mediator client/thread will receive (in this case by popping the pieces o the message queue individually) all the messages and send them to the printer in reverse order.
/*msgpass.cpp*/
#include
#include
#include
#include
#include
#include "../Server.h"
using namespace std;
struct Message
{
string payload;
unsigned int t_ind;
};
class MsgPass
{
public:
MsgPass(unsigned int upper_b)
{
up_bound = upper_b;
}
~MsgPass(){}
void send(struct Message m)
{
msgqueue.push(m);
}
struct Message recv()
{
struct Message msg = msgqueue.front();
msgqueue.pop();
return msg;
}
bool isFull()
{
bool toReturn = false;
if (msgqueue.size() == up_bound)
toReturn = true;
return toReturn;
}
unsigned int getSize()
{
return msgqueue.size();
}
bool empty()
{
return msgqueue.empty();
}
private:
queue
unsigned int up_bound;
};
Server *server;
MsgPass *m;
void print(string toPrint)
{
}
void output(string out)
{
}
void lock(int choice)
{
}
void unlock(int choice)
{
}
void getAndWrite()
{
/* Receive all the messages from the message queue and print them in reverse order according to t_ind */
}
void passAlong(int index)
{
try
{
if (m->isFull())
{
getAndWrite();
}
else
{
lock(0); // server
print("Thread " + to_string(index) + ": Lock acquired for Server ");
string piece = server->getPiece(index);
unlock(0);
print("Thread " + to_string(index) + ": Lock released for Server ");
/* Construct the message using the Message struct. Call it msg */
m->send(msg);
lock(1);
output("Thread " + to_string(index) + " sending message ");
unlock(1);
}
}
catch (const char* err)
{
cout
throw new exception();
}
}
int main(int argc, char const *argv[])
{
/* cin here */
if (/*input 2*/ != 0 && /*input 1*/ != 0)
{
server = new Server(/*input 2*/, /*input 1*/);
m = new MsgPass(/*input 1*/);
/* Fill in the main function code here */
delete server;
delete m;
}
else
{
cout
}
return 0;
}
//end of cpp
The Message struct This struct has been given to you in the msgpass.cpp file. The elements are as follows: 1. string payload - This is a string that will contain the piece of the ASCII art to print. 2. uint t^ind - This is the thread index of the thread that is sending this message. The MsgPass Class This class has been implemented for you, however mutual exclusion has not been added for access to the message queue. Notice that there is an instance (called m) of this class already created for you in the file Implement: You must implement mutual exclusion within this class for reading from and writing to the queue. Please keep the scenario above in mind when implementing this. The functions in this class are as follows: 1. MsgPass(uint upper-b) - The constructor simply initialises an upper bound 2. void send(struct Message m) - This adds the message m to the message 3. struct Message recv() - This removes and returns the first message in the 4. bool isFull() - Uses the upper bound specified in the constructor to check whether for the queue size queue. message queue the number of messages in the queue has reached the upper bound and returns true if it has and false otherwise. 5. uint getSize() - returns the number of messages on the queue. The Message struct This struct has been given to you in the msgpass.cpp file. The elements are as follows: 1. string payload - This is a string that will contain the piece of the ASCII art to print. 2. uint t^ind - This is the thread index of the thread that is sending this message. The MsgPass Class This class has been implemented for you, however mutual exclusion has not been added for access to the message queue. Notice that there is an instance (called m) of this class already created for you in the file Implement: You must implement mutual exclusion within this class for reading from and writing to the queue. Please keep the scenario above in mind when implementing this. The functions in this class are as follows: 1. MsgPass(uint upper-b) - The constructor simply initialises an upper bound 2. void send(struct Message m) - This adds the message m to the message 3. struct Message recv() - This removes and returns the first message in the 4. bool isFull() - Uses the upper bound specified in the constructor to check whether for the queue size queue. message queue the number of messages in the queue has reached the upper bound and returns true if it has and false otherwise. 5. uint getSize() - returns the number of messages on the queueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started