This project concludes the network-oriented series that we began at the start of
ID: 3710071 • Letter: T
Question
This project concludes the network-oriented series that we began at the start of the semester. Its purpose is to act as a foundation to a simulation you can easily expand upon and add features to, while demonstrating a large number of the concepts we’ve covered so far in the course.
FOR 50% EXTRA CREDIT ON THIS PROJECT: ?In the last lecture, we discussed the use of the averaging many random trials to determine the expected behavior of a system. In order to do this, we can sample the important variables of the system from a (often uniform) probability distribution for each computation. After each “run” of the system with random values, we can compute the results we’re interested in and take an average.
The average results we’re interested in ?for the extra credit are: 1. Average percentage of packets lost. 2. Average travel time of a packet.
Regardless of whether or not you would like to do the extra credit, ?we’ll want to generate the following variables randomly. Where they are generated should be fairly obvious based on what they do. 1. numberOfNodes?: The number of nodes in the linked list (i.e. the number of network objects). 2. probabilityOfPacketLoss?: How often a node will drop a packet 3. maxPackets?: How many packets each NetworkObject can hold. This should be the size of your ?packets? Queue. For simplicity, this value can apply to all NetworkObject nodes, rather than generating a different value for each node. For your own practice, you might try doing this anyway.
For simplicity in this project, we will hold the packet source and target fixed at the beginning and end of the linked list. The modifications for this project can take place in main() and whatever helper functions you see fit to create. However, the following additions must be made to the NetworkObject class:
NetworkObject 1. Define the randomly generated integer “?maxPackets?” 2. Define a function ?addPackets?, which adds an array of packet objects to the packets? queue. 3. Define a function ?getPackets?, which returns a number ?n ?packets from the packets? queue. The packet objects should obviously be removed from the queue, then returned. 4. Define a function ?update, ?which removes as many packets as possible, up to the “?maxPackets?” value, and adds them to the “?packets?” queue in the next node. For each packet, use the ?“probabilityOfPacketLoss”? variable to determine if a packet should be added to the next NetworkObject, or simply removed. ?Keep count of how many packets are dropped?. This will be important later. Note: ?There are a few things to consider here: a. There may not be a next node. In that case, the packets should just be removed from the queue. b. Do you want to compute travel time in this function when there is no next node? c. The nextNetworkObject may not be able to accept all of those packets! Only remove the packets that can be added successfully to the nextNetworkObject’s queue.
?Main 1. Define a random number of NetworkObjects and link them together in a Linked List as we’ve been doing in the previous projects. 2. Loop over the following operations a large number of times (e.g. 1,000 times): a. Add a random number of packet objects to the ?first? NetworkObject. Keep a running total of this number. b. Call the ?update() ?function on ?every? NetworkObject in the linked list. 3. When the loop is done, output the following: a. How many nodes were in the linked list b. How many packets traveled through the system c. How many packets were lost. This can be expressed as a percentage by dividing the total number of packets lost by the total number of packets created, and multiplying by 100. 4. For the extra credit, perform steps 1, 2, and 3 a large number of times, and output the average of the results above.
Here is my previous project:
#include <string>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
class Packet2{
private:
int targetID;
int sourceID;
string data_Network_Object;
public:
void setTargetID(int t);
void setSourceID(int s);
void setDateNetworkObject(string d);
int getTargetID();
int getSourceID();
string getDatanetworkObject();
};
void Packet2::setTargetID(int t){
targetID=t;
}
void Packet2::setSourceID(int s){
sourceID=s;
}
void Packet2::setDateNetworkObject(string d){
data_Network_Object=d;
}
int Packet2::getTargetID(){
return targetID;
}
int Packet2::getSourceID(){
return sourceID;
}
string Packet2::getDatanetworkObject(){
return data_Network_Object;
}
//Server class is being derived from NetworkObject.
class NetworkObject{
public:
NetworkObject(int objectId){this->objectId = objectId;}
NetworkObject(){}
int getObjectId(){return objectId;}
queue <Packet2*> getPacketQueue(){ return packets;}
private:
int objectId;
queue <Packet2*> packets;
};
int main()
{
//declare 10 NetworkObjects with id 1,2,3,4,5,6,7,8,9.
NetworkObject objects1[10];
for(int i = 0; i <10 ; i++) {
objects1[i] = i;
cout << "objectId: " << objects1[i].getObjectId()<< endl;
}
stringstream ss;
queue <Packet2*> q=objects1[0].getPacketQueue();
for(int i=1;i<=6;i++){
Packet2* p1=new Packet2();
p1->setTargetID(i);
p1->setSourceID(i+6);
int val=objects1[0].getObjectId();
ss << val;
p1->setDateNetworkObject(ss.str());
q.push(p1);
}
cout<<"The size of queue is "<<q.size()<<endl;
while(q.size()>0){
cout<<"Removing packet with targetID and sourceID as : "<<(q.front())->getTargetID()<<" "<<(q.front())->getSourceID()<<endl;
q.pop();
}
return 0;
}
Can you please put all the steps in the answer.
Explanation / Answer
ANS:-
GIVEN THAT,
#include<iostream>
#include<queue>
#include "Packet.h"
using namespace std;
class NetworkObject
//beginning Network Object class definition
{
public:
//Constructor
NetworkObject(int objId): packets(),objectId(objId){
//Parameterized constructor
NetworkObject();
//push packet into the queue that belong to one particular NetworkObject
void setPacket(Packet *pckt)
{
packets.push(pckt);
}
//pop packet one by one that belongs one particular NetworkObject
Packet *popPacket()
{
Packet *pkt;
pkt = packets.front();
packets.pop();
return pkt;
}
//retrieve all the packets of a particular NetworkObject
void getPacketInfo()
{
Packet *pkt;
while(!packets.empty())
{
pkt = popPacket();
cout <<" Packets from source "<<pkt->getSourceID()<< " to destination "<<pkt->getTargetID() <<" with data "<<pkt->getData();
}
}
//returns objectId
int getObjectId(){
return objectId;
}
private:
//private member
int objectId;
queue<Packet*> packets; //packets variable to store the packets in an queue
};
//server class constructor is called from networkobject class parameterized constructor
class Server : public NetworkObject
{
public:
Server(int objId): NetworkObject(objId){}
};
int main()
{
//declare 6 NetworkObjects with id 4,8,16, 32, 64, 128.
NetworkObject at[6] = {{4}, {8}, {16}, {32}, {64}, {128}};
//Instantiate two objects of type Packet
Packet *pckt1 = new Packet(8,4,"8 to 4");
Packet *pckt2 = new Packet(8,5,"8 to 5");
//push the above created packets in the queue
at[1].setPacket(pckt1);
at[1].setPacket(pckt2);
for(int i=0; i<6;i++)
{
//prints the object IDs of the network objects along with packet information
cout << " Object ID of Network Object: "<< at[i].getObjectId() << endl;
at[i].getPacketInfo();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.