Question

In: Computer Science

In Java, C, C++ or Python Develop a simulation program to simulate an 8-port Ethernet switch....

In Java, C, C++ or Python

Develop a simulation program to simulate an 8-port Ethernet switch. The switch initially has no knowledge about the hosts connected to each port. It learns frame addresses and stores-and-forwards the frames. The input text file "in.txt" contains information of the incoming frames, one frame per line. There are 4 pieces of data per line: frame ID, arrival port, frame source address, and frame destination address. The frames arrive at the switch in the order of which they appear in the input file. Destination address "X" indicate a broadcast frame. The output text file "out.txt" has 8 lines. Each line lists all the frames departing from each port, Here is an example:

"in.txt" contains 5 incoming frames:

F1 P2 B--A

F2 P6 D--C

F3 P5 E--B

F4 P7 F--D

F5 P6 D--X

"out.txt" should list the departing frames on each port:

P1: F1 F2 F5

P2: F2 F3 F5

P3: F1 F2 F5

P4: F1 F2 F5

P5: F1 F2 F5

P6: F1 F4

P7: F1 F2 F5

P8: F1 F2 F5

please  output out.txt for this input file input.txt

F1      P1      A--G
F2      P1      B--H
F3      P1      C--I
F4      P2      D--E
F5      P2      D--F
F6      P7      G--A
F7      P8      H--B
F8      P8      I--C
F9      P4      E--D
F10     P4      F--D
F11     P7      G--X
F12     P1      B--G
F13     P2      D--G
F14     P4      E--G
F15     P8      H--G
F16     P1      A--D
F17     P1      B--E
F18     P1      C--F
F19     P2      D--H
F20     P8      H--I

Solutions

Expert Solution

CONSIDERING THE PARAMETERS FROM QUESTION WE SHOW THE OUTPUT AS:

C program:

#include <stdio.h>

int main(){
/*
* INPUT FILE : in.txt
* OUTPUT FILE : out.txt
*/
char frameID;
char port;
char source;
char destination;
  
//Mapping of port - source Address
//if portMap[i] contains character 'A' means, port Pi is the destination of device A
char portMap[8];
  
//output port - frame number mapping
//out[i][0] --> contains total number of outgoing frames from port Pi
//followed by ids of outgoing frames.
//if out[i][j] = k means, frame Fk went out of port Pi in sequence j
char out[8][100];
  
FILE *ifile, *ofile;
int i, j;
bool success;
  
//initializing portMap to unknown and out frames
for(i = 0; i < 8; i ++){
portMap[i] = '*';
out[i][0] = '1';
}
  
//open input file
ifile = fopen("C:\\Users\\Retheesh\\Desktop\\test\\in.txt", "r");
  
//iterate throughout the file inputs
//read line by line
while(fscanf(ifile, "F%c P%c %c--%c\n", &frameID, &port, &source, &destination) != EOF){
//checking the source address and upidating in portMap
if(portMap[port - '1'] == '*')
portMap[port - '1'] = source;
  
//broadcast frame received. forwarding to all ports
if(destination == 'X'){
for(int i = 0; i < 8; i++){
out[i][out[i][0] - '0'] = frameID;
out[i][0]++;
}
continue;
}
  
//forwarding input frame from port Pi only to correct destination port
//if destination port is known
success = false;
for(int i = 0; i < 8; i++){
if(portMap[i] == destination){
out[i][out[i][0] - '0'] = frameID;
out[i][0]++;
success = true;
break;
}
}
  
//continue to next input
if(success)
continue;
  
//destination port is unknown.
//forwarding input frame from port Pi to all other ports except Pi
for(int i = 0; i < 8; i++){
if(portMap[i] != source){
out[i][out[i][0] - '0'] = frameID;
out[i][0]++;
}
}
}
  
fclose(ifile);
  
//open output file
ofile = fopen("C:\\Users\\Retheesh\\Desktop\\test\\out.txt", "w");
  
//writing output to file
for(i = 0; i < 8; i++){
fprintf(ofile, "P%d: ", i + 1);
printf("P%d: ", i + 1);
for(j = 1; j < out[i][0] - '0'; j++){
printf("F%c ", out[i][j]);
fprintf(ofile, "F%c ", out[i][j]);
}
fprintf(ofile, "\n");
printf("\n");
}
  
//close output file
fclose(ofile);
  
return 0;
}

PLEASE UPVOTE ITS VERY NECESSARY FOR ME


Related Solutions

make a C / C++, Java, or Python program with two processes, a producer and a...
make a C / C++, Java, or Python program with two processes, a producer and a consumer. The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers...
Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line.    The numbers drawn cannot repeat Sort...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
You are to write a program using Java that will simulate a slot machine with four...
You are to write a program using Java that will simulate a slot machine with four wheels. It will determine the amount of money won for each spin. The four wheels spin and stop showing a value between 0 and 9 inclusive. It costs $2 to play. •You win $500 (but not the $2 you bet) if you get 4 wheels the same. •You win $10 (but not the $2 you bet) if you get exactly 3 of a kind....
IN PYTHON Develop a program in python that includes a number of functions for the multi-server...
IN PYTHON Develop a program in python that includes a number of functions for the multi-server queue. The program accepts arrival and services rates and the number of servers and calls your functions to output the average number of customers and average waiting time in the system.
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT