(UPDATED WITH INSTRUCTIONS) Hello everyone, I need some assistance completing th
ID: 3756225 • Letter: #
Question
(UPDATED WITH INSTRUCTIONS)
Hello everyone,
I need some assistance completing this program assignment. In this assignment only it is needed to add specific code to the QMain.cpp . (Only add the code to QMain.cpp, paste your answer and take a screenshot of the output of the code). Thank you, I will give you excellent rating for your answer.
Here are the instructions:
-To use a queue as a buffer between a data transmission source and a data receiver.
- The data transmission source is implemented with the Sender class. The data transmission source sends data at a rate of 500 characters per second. Once a Sender object is created, it will be an open data stream and will attempt to send data at a rate of 500 characters per second. When the Sender object is finished sending all of its data, the data stream will be closed.
-The Sender member function isStreamOpen() gives the status of the data stream (open or closed).
-For this assignment, the Sender object is attempting to transmit 131 characters. The Sender object member function int readFromStream(int arrayLength, char rv[]) is how the data is acquired from the Sender object.
-If the Sender object has data to transmit, then the data will be inserted, in order, starting from array index 0 into the rv[] array and the number of characters sent is returned. If no characters were sent, then the return value is zero.
-The caller needs to provide the array rv[] and the array length (arrayLength). The array needs to be large enough to hold the maximum message length. For this task, make the array 200 characters in length.
-The data receiver is implemented with the Receiver class. The data receiver can receive data at a rate of 200 characters per second. Once a Receiver object is created, it is able to receive data.
-The Receiver object member function int howManyCharToRx() indicates the maximum number of characters that Receiver object can receive at that time. You cannot send more data than that number.
-The Receiver object member function void sendChars(int arrayLength, char x[]) sends the number of characters (specified by arrayLength) that are contained in array x[] to the Receiver object. In the main() function, a Sender object (called tx), a Receiver object (called rx) and a queue object (called q) are created. The code should be added between the comments of "add your code here".
The Queue class is provided to you.
In the main() function, you should create a polling loop.
This polling loop should check the status of the Sender object to see if the stream is open and if there are any characters that need to be sent. If characters are retrieved from the Sender object, then they should be put on the queue.
the polling loop should check the status of the Receiver object to see if characters can be received. If characters can be received and there are characters on the queue to send, then take the number of allowed characters off the queue and send them to the Receiver object.
The polling loop should be terminated when the Sender class stream is closed and the queue is empty. Implement your main polling loop in the indicated space in the Main.cpp file.
The results of your data transfer will be contained in the Qresults.txt. file in the working directory.
Here are the code files:
(1) QMain.cpp (Note: only add code in the commented section "// put your code here" . Not need to modified the other code files)
#include
#include
#include "Sender.h"
#include "Receiver.h"
#include "Queue.h"
#include "MyFileIO.h"
int main()
{
Sender tx;
Receiver rx;
Queue q;
// put your code here (below)
// put your code here (above)
std::string FILENAME = "Qresults.txt";
std::string outString;
char ch[10000]; // array large enough for the file output
int char_ptr = 0; // file index pointer
char_ptr = build_file_array(rx.getRxedMsg(), char_ptr, ch);
char_ptr = build_file_array(getCRLF(), char_ptr, ch);
char_ptr = build_file_array(getCode(char_ptr, ch), char_ptr, ch);
std::string max = "The maximum queue depth = ";
char_ptr = build_file_array(max + valueOf(q.getMaxSize()), char_ptr, ch);
char_ptr = build_file_array(getCRLF(), char_ptr, ch);
char_ptr = build_file_array(nowtoString(), char_ptr, ch);
writefile(FILENAME, ch, char_ptr);
return 0;
}
(2) MyFileIO.h
#ifndef MYFILEIO_H
#define MYFILEIO_H
#include
bool fileExists(std::string st);
bool writefile(std::string filename, char *ptr, int length);
std::string nowtoString();
void getChars(int beginst, int endst, char data[], int ptr, std::string st);
int build_file_array(std::string st, int ptr, char data[]);
std::string valueOf(int num);
std::string getCRLF();
std::string getCode(int ptr, char data[]);
#endif
(3) MyFileIO.cpp
#include
#include
#include
#include
#include
#include
#include "MyFileIO.h"
bool fileExists(std::string st)
{
bool result;
std::ifstream infile(st.c_str());
result = infile.good();
if (infile.is_open() == true)
{
infile.close();
}
return (result);
}
bool writefile(std::string filename, char *ptr, int length)
{
bool result = false;
std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);
if (outfile.is_open() == true)
{
outfile.write(ptr, length);
outfile.close();
result = true;
}
return(result);
}
std::string nowtoString()
{
time_t now = time(0);
tmtstruct;
char buf[80];
localtime_s(&tstruct, &now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
std::string st(buf);
return st;
}
int build_file_array(std::string st, int ptr, char data[])
{
getChars(0, st.length(), data, ptr, st);
return (ptr + st.length());
}
void getChars(int beginst, int endst, char data[], int ptr, std::string st)
{
int i;
for (i = 0; i < (endst - beginst); i++)
{
data[i + ptr] = st[beginst + i];
}
}
std::string valueOf(int num)
{
std::stringstream ss;
ss << num;
return(ss.str());
}
std::string getCRLF()
{
std::string CRLF = " ";
return CRLF;
}
std::string getCode(int ptr, char data[])
{
int code = 0;
for (int i = 0; i < ptr; i++)
{
code = code + (int)data[i];
}
return (valueOf(code) + getCRLF());
}
(4) Queue.h
#ifndef QUEUE_H
#define QUEUE_H
const int QUEUE_CAPACITY = 1024;
typedef char QueueElement;
class Queue
{
:
empty() const;
enqueue(const QueueElement & value);
display(std::ostream & out) const;
front() const;
dequeue();
int Queue::getMaxSize() const;
private:
/***** Data Members *****/
int myFront, myBack;
int maxQueueSize;
int size;
QueueElement myArray[QUEUE_CAPACITY];
}; // end of class declaration
#endif
(5) Queue.cpp
/*-- Queue.cpp-----------------------------------------------------------
This file implements Queue member functions.
-------------------------------------------------------------------------*/
#include
#include "Queue.h"
//--- Definition of Queue constructor
Queue::Queue()
: myFront(0), myBack(0), maxQueueSize(0), size(0)
{}
//--- Definition of empty()
bool Queue::empty() const
{
return (myFront == myBack);
}
//--- Definition of enqueue()
void Queue::enqueue(const QueueElement & value)
{
int newBack = (myBack + 1) % QUEUE_CAPACITY;
if (newBack != myFront) // queue isn't full
{
myArray[myBack] = value;
myBack = newBack;
size++;
if (size > maxQueueSize)
{
maxQueueSize = size;
}
}
else
{
std::cerr << "*** Queue full -- can't add new value *** "
"Must increase value of QUEUE_CAPACITY in Queue.h ";
exit(1);
}
}
//--- Definition of display()
void Queue::display(std::ostream & out) const
{
for (int i = myFront; i != myBack; i = (i + 1) % QUEUE_CAPACITY)
out << myArray[i] << " ";
std::cout << std::endl;
}
//--- Definition of front()
QueueElement Queue::front() const
{
if (!empty())
return (myArray[myFront]);
else
{
std::cerr << "*** Queue is empty -- returning garbage value *** ";
QueueElement garbage[1];
return garbage[0];
}
}
//--- Definition of dequeue()
void Queue::dequeue()
{
if (!empty())
{
myFront = (myFront + 1) % QUEUE_CAPACITY;
size--;
}
else
{
std::cerr << "*** Queue is empty -- "
"can't remove a value *** ";
}
}
//--- Definition of getMaxSize()
int Queue::getMaxSize() const
{
return maxQueueSize;
}
(6) Receiver.h
#include
#include
#include
#ifndef RECEIVER_H
#define RECEIVER_H
class Receiver
{
public:
Receiver();
~Receiver();
int Receiver::howManyCharToRx();
void Receiver::sendChars(int arrayLength, char x[]);
std::string getRxedMsg();
private:
std::clock_t start;
char rxArray[10000]; // reserve enough room for this task
int freq;
double timeBase;
int ptr; // points to the rxed data array
int allowedChars; // how many chars can be sent at once
};
(7) Receiver.cpp
#include
#include "Receiver.h"
Receiver::Receiver()
{
start = std::clock(); // capture the start time
freq = 2; // receive 2 char per 10 msec
ptr = 0; // points to the rxed data array
timeBase = .01; // in seconds (10 msec)
allowedChars = 0; // how many chars can be sent at once
}
int Receiver::howManyCharToRx()
{
int num = 0; // counts the chars for this request cycle
int count;
double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
if (duration > timeBase)
{
start = std::clock();
count = (int)(duration / timeBase);
num = count * freq;
}
allowedChars = allowedChars + num;
return allowedChars;
}
void Receiver::sendChars(int arrayLength, char x[])
{
int count = arrayLength;
if (count > allowedChars)
{ // if requesting too many chars to send then
count = allowedChars;
}
for (int i = 0; i < count; i++)
{
rxArray[ptr+i] = x[i] - 1;
}
ptr = ptr + count;
allowedChars = allowedChars - count;
}
std::string Receiver::getRxedMsg()
{
std::string s(rxArray, ptr);
return s;
}
Receiver::~Receiver()
{
}
(8) Sender.h
#include
#include
#ifndef SENDER_H
#define SENDER_H
class Sender
{
public:
Sender();
~Sender();
bool isStreamOpen();
int Sender::readFromStream(int arrayLength, char rv[]);
private:
std::clock_t start;
int ptr;
char *sendArray;
int charToSend;
int freq;
double timeBase;
};
(9) Sender.cpp
#include
#include "Sender.h"
Sender::Sender()
{
start = std::clock();
sendArray = "Dpohsbuvmbujpot-!Zpv!ibwf!tvddfttgvmmz!vtfe!b!rvfvf!bt!b!cvggfs!cfuxffo!bo!btzodispopvt!usbotnjuujoh!tpvsdf!boe!b!sfdfjwjoh!tpvsdf/";
charToSend = strlen(sendArray);
freq = 5; // char per 10 msec
timeBase = .01; // in seconds (10 msec)
}
bool Sender::isStreamOpen()
{
bool rv = true;
if (charToSend == 0)
rv = false;
return rv;
}
int Sender::readFromStream(int arrayLength, char rv[])
{
int count;
int num = 0;
double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
if (duration > timeBase)
{
start = std::clock();
count = (int)(duration / timeBase);
num = count * freq;
if (num > arrayLength)
{
num = arrayLength;
}
if (num > charToSend)
{
num = charToSend;
}
for (int i = 0; i < num; i++)
{
rv[i] = sendArray[ptr+i];
}
ptr = ptr + num;
charToSend = charToSend - num;
}
return num;
}
Sender::~Sender()
{
}
Explanation / Answer
#include
#include
#include "Sender.h"
#include "Receiver.h"
#include "Queue.h"
#include "MyFileIO.h"
int main()
{
Sender tx;
Receiver rx;
Queue q;
// put your code here (below)
// put your code here (above)
std::string FILENAME = "Qresults.txt";
std::string outString;
char ch[10000]; // array large enough for the file output
int char_ptr = 0; // file index pointer
char_ptr = build_file_array(rx.getRxedMsg(), char_ptr, ch);
char_ptr = build_file_array(getCRLF(), char_ptr, ch);
char_ptr = build_file_array(getCode(char_ptr, ch), char_ptr, ch);
std::string max = "The maximum queue depth = ";
char_ptr = build_file_array(max + valueOf(q.getMaxSize()), char_ptr, ch);
char_ptr = build_file_array(getCRLF(), char_ptr, ch);
char_ptr = build_file_array(nowtoString(), char_ptr, ch);
writefile(FILENAME, ch, char_ptr);
return 0;
}
(2) MyFileIO.h
#ifndef MYFILEIO_H
#define MYFILEIO_H
#include
bool fileExists(std::string st);
bool writefile(std::string filename, char *ptr, int length);
std::string nowtoString();
void getChars(int beginst, int endst, char data[], int ptr, std::string st);
int build_file_array(std::string st, int ptr, char data[]);
std::string valueOf(int num);
std::string getCRLF();
std::string getCode(int ptr, char data[]);
#endif
(3) MyFileIO.cpp
#include
#include
#include
#include
#include
#include
#include "MyFileIO.h"
bool fileExists(std::string st)
{
bool result;
std::ifstream infile(st.c_str());
result = infile.good();
if (infile.is_open() == true)
{
infile.close();
}
return (result);
}
bool writefile(std::string filename, char *ptr, int length)
{
bool result = false;
std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);
if (outfile.is_open() == true)
{
outfile.write(ptr, length);
outfile.close();
result = true;
}
return(result);
}
std::string nowtoString()
{
time_t now = time(0);
tmtstruct;
char buf[80];
localtime_s(&tstruct, &now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
std::string st(buf);
return st;
}
int build_file_array(std::string st, int ptr, char data[])
{
getChars(0, st.length(), data, ptr, st);
return (ptr + st.length());
}
void getChars(int beginst, int endst, char data[], int ptr, std::string st)
{
int i;
for (i = 0; i < (endst - beginst); i++)
{
data[i + ptr] = st[beginst + i];
}
}
std::string valueOf(int num)
{
std::stringstream ss;
ss << num;
return(ss.str());
}
std::string getCRLF()
{
std::string CRLF = " ";
return CRLF;
}
std::string getCode(int ptr, char data[])
{
int code = 0;
for (int i = 0; i < ptr; i++)
{
code = code + (int)data[i];
}
return (valueOf(code) + getCRLF());
}
(4) Queue.h
#ifndef QUEUE_H
#define QUEUE_H
const int QUEUE_CAPACITY = 1024;
typedef char QueueElement;
class Queue
{
:
empty() const;
enqueue(const QueueElement & value);
display(std::ostream & out) const;
front() const;
dequeue();
int Queue::getMaxSize() const;
private:
/***** Data Members *****/
int myFront, myBack;
int maxQueueSize;
int size;
QueueElement myArray[QUEUE_CAPACITY];
}; // end of class declaration
#endif
(5) Queue.cpp
/*-- Queue.cpp-----------------------------------------------------------
This file implements Queue member functions.
-------------------------------------------------------------------------*/
#include
#include "Queue.h"
//--- Definition of Queue constructor
Queue::Queue()
: myFront(0), myBack(0), maxQueueSize(0), size(0)
{}
//--- Definition of empty()
bool Queue::empty() const
{
return (myFront == myBack);
}
//--- Definition of enqueue()
void Queue::enqueue(const QueueElement & value)
{
int newBack = (myBack + 1) % QUEUE_CAPACITY;
if (newBack != myFront) // queue isn't full
{
myArray[myBack] = value;
myBack = newBack;
size++;
if (size > maxQueueSize)
{
maxQueueSize = size;
}
}
else
{
std::cerr << "*** Queue full -- can't add new value *** "
"Must increase value of QUEUE_CAPACITY in Queue.h ";
exit(1);
}
}
//--- Definition of display()
void Queue::display(std::ostream & out) const
{
for (int i = myFront; i != myBack; i = (i + 1) % QUEUE_CAPACITY)
out << myArray[i] << " ";
std::cout << std::endl;
}
//--- Definition of front()
QueueElement Queue::front() const
{
if (!empty())
return (myArray[myFront]);
else
{
std::cerr << "*** Queue is empty -- returning garbage value *** ";
QueueElement garbage[1];
return garbage[0];
}
}
//--- Definition of dequeue()
void Queue::dequeue()
{
if (!empty())
{
myFront = (myFront + 1) % QUEUE_CAPACITY;
size--;
}
else
{
std::cerr << "*** Queue is empty -- "
"can't remove a value *** ";
}
}
//--- Definition of getMaxSize()
int Queue::getMaxSize() const
{
return maxQueueSize;
}
(6) Receiver.h
#include
#include
#include
#ifndef RECEIVER_H
#define RECEIVER_H
class Receiver
{
public:
Receiver();
~Receiver();
int Receiver::howManyCharToRx();
void Receiver::sendChars(int arrayLength, char x[]);
std::string getRxedMsg();
private:
std::clock_t start;
char rxArray[10000]; // reserve enough room for this task
int freq;
double timeBase;
int ptr; // points to the rxed data array
int allowedChars; // how many chars can be sent at once
};
(7) Receiver.cpp
#include
#include "Receiver.h"
Receiver::Receiver()
{
start = std::clock(); // capture the start time
freq = 2; // receive 2 char per 10 msec
ptr = 0; // points to the rxed data array
timeBase = .01; // in seconds (10 msec)
allowedChars = 0; // how many chars can be sent at once
}
int Receiver::howManyCharToRx()
{
int num = 0; // counts the chars for this request cycle
int count;
double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
if (duration > timeBase)
{
start = std::clock();
count = (int)(duration / timeBase);
num = count * freq;
}
allowedChars = allowedChars + num;
return allowedChars;
}
void Receiver::sendChars(int arrayLength, char x[])
{
int count = arrayLength;
if (count > allowedChars)
{ // if requesting too many chars to send then
count = allowedChars;
}
for (int i = 0; i < count; i++)
{
rxArray[ptr+i] = x[i] - 1;
}
ptr = ptr + count;
allowedChars = allowedChars - count;
}
std::string Receiver::getRxedMsg()
{
std::string s(rxArray, ptr);
return s;
}
Receiver::~Receiver()
{
}
(8) Sender.h
#include
#include
#ifndef SENDER_H
#define SENDER_H
class Sender
{
public:
Sender();
~Sender();
bool isStreamOpen();
int Sender::readFromStream(int arrayLength, char rv[]);
private:
std::clock_t start;
int ptr;
char *sendArray;
int charToSend;
int freq;
double timeBase;
};
(9) Sender.cpp
#include
#include "Sender.h"
Sender::Sender()
{
start = std::clock();
sendArray = "Dpohsbuvmbujpot-!Zpv!ibwf!tvddfttgvmmz!vtfe!b!rvfvf!bt!b!cvggfs!cfuxffo!bo!btzodispopvt!usbotnjuujoh!tpvsdf!boe!b!sfdfjwjoh!tpvsdf/";
charToSend = strlen(sendArray);
freq = 5; // char per 10 msec
timeBase = .01; // in seconds (10 msec)
}
bool Sender::isStreamOpen()
{
bool rv = true;
if (charToSend == 0)
rv = false;
return rv;
}
int Sender::readFromStream(int arrayLength, char rv[])
{
int count;
int num = 0;
double duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
if (duration > timeBase)
{
start = std::clock();
count = (int)(duration / timeBase);
num = count * freq;
if (num > arrayLength)
{
num = arrayLength;
}
if (num > charToSend)
{
num = charToSend;
}
for (int i = 0; i < num; i++)
{
rv[i] = sendArray[ptr+i];
}
ptr = ptr + num;
charToSend = charToSend - num;
}
return num;
}
Sender::~Sender()
{
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.