Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the empty functions in the msgclass.cpp Server.h #ifndef SERVER_H #def

ID: 3741941 • Letter: I

Question

Implement the empty functions in the msgclass.cpp

Server.h

#ifndef SERVER_H
#define SERVER_H

#include
#include
#include
#include

using namespace std;

class Server
{
public:
   Server();
   Server(string, int);
   ~Server();
   string getPiece(int);
private:
   string *ascii;
   mutex access;
};

#endif

-------------------------------------------------------------------------------------------------------------------------

Server.cpp

#include "Server.h"
#include
#include

Server::Server(){}

Server::Server(string filename, int threads)
{
   vector loaded;
   ascii = new string[threads];
   ifstream in;
   string line;
   in.open(filename);
   if (!in.is_open())
   {
       cout << "Could not open file " << filename << endl;
       exit(1);
   }
   while(!in.eof())
   {
       getline(in, line);
       loaded.push_back(line);
   }
   in.close();

   int step = loaded.size()/threads;
   string piece = "";

   for (int i = 0; i < threads; ++i)
   {
       for (int j = step*i; j < ((step*i) + step); ++j)
       {
           if (j + 1 < loaded.size())
               piece += loaded.at(j) + " ";
           else
               piece += loaded.at(j);
       }
       ascii[i] = piece;
       piece = "";
   }
}

Server::~Server()
{
   delete []ascii;
}

string Server::getPiece(int piece)
{
   srand(time(NULL));
   if (rand()/static_cast(RAND_MAX) > 0.6)
       throw "500 Internal Server Error";
   cout << "Getting piece number " << piece << endl;
   string toReturn = ascii[piece];
   return toReturn;
}

---------------------------------------------------------------------------------------------------

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 msgqueue;
   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 << err << endl;
       throw new exception();
   }
}

int main(int argc, char const *argv[])
{
   if (/*argument 1*/ != 0 && /*argument 2*/ != 0)
   {
       server = new Server(/*argument 1*/, /*argument 2*/);
       m = new MsgPass(/*argument 2*/);
      
       /* Fill in the main function code here */

       delete server;
       delete m;
   }
   else
   {
       cout << "Please enter a file name and the number of threads to use!" << endl;
   }
   return 0;
}

-----------------------------------------------------------------------------------------------------------------------------

batman.ascii

                      *******************
                  ***************************
               *********************************
           *******   *     *       *    *    *******
        *******   ***      **     **     ***   *******
      ******   *****       *********      *****    *****
    ****** ********       *********       ******    *****
   ****   **********       *********       *********   *****
**** **************    ***********     ************   ****
**** ************************************************* ****
**** *************************************************** ****
**** **************************************************** ****
**** **************************************************** ****
**** *************************************************** ****
**** *******     **** *********** ****     ********* ****
   ****   *****      *      *******      *      ******** ****
    *****   ****             *****             ******   *****
      *****   **              ***              **    ******
       ******   *              *              *   *******
         *******                                *******
            ********                         *******
               *********************************
                  ***************************
                      *******************

This task still lins the same sconario wlioro ench clicnt ean send tieir picco to the printer, however this time you clecide to use a messnge queue to enulate message passiug between the clients/threads. The ilen s tllat after cach elicut/thread ]1nš rcqtcstel the

Explanation / Answer

mutex server;

mutex printer;

condition_variable cv;

mutex thread_no;

mutex screen;

Static int thread_run = 0;

void print(string toPrint)

{

screen.lock();

cout<< toPrint << endl;

screen.unlock();

}

void output(string out)

{

ofStream myfile;

print("Opening...");

myfile.open("deadlock.txt",ios_base::app);

print("Writing...");

myfile << out;

myfile.close();

}

void lock(int choice)

{

if(choice==0){

server.lock();

}

elseif(choice==1){

printer.lock();

}

}

void unlock(int choice)

{

if(choice==0){

server.unlock();

}

elseif(choice==1){

printer.unlock();

}

}

void getAndWrite()

{

Struct Message msg1;

Struct Message msgs[m->getSize()];

if(!m->empty){

msgs[0]=m->recv();

}

int s=1;

while(!m->empty()){

msg1=m->recv();

for(int i=s-1;i>=0;i--){

if(msgs[i].t_ind>msg1.t_ind){

msgs[i+1]=msgs[i];

}

}

msgs[i+1]=msg1;

s++;

}

for(int i=0;i<s;i++){

print(msgs[i].payload);

}

}