Routing Simulation - Implementation of Design Scheme (4)

zhaozj2021-02-16  48

3, set the routing table

Method Name: CentralRout :: setRoutTable

Method parameters: no

Return value: no

The purpose of the method: fill in the routing table. Local variable int ** Result, filling by routing calculation method, is the description of the target router label and the next router label that should be transmitted, and the core process is the setting of the routing table through this variable.

Possible error: File operation failed: the system exits

// Set the routing table

Void CentralRout :: setrouttable (void)

{

Int ** result = null;

Int routnum = routTable.GetrOutnum ();

Tablenode _RoutTable [MAX_ROUT_TABLE_SIZE];

INT selfid = 0;

INT * index = null;

Unsigned int ** addr = null;

Std :: fstream indexfile;

Char * indexfilename = "indexaddr.txt";

// Application Space

Result = new int * [routnum];

For (int i = 0; i

Result [i] = new int [2];

Index = new int [routnum];

AddR = new unsigned int * [routnum];

For (int i = 0; i

AddR [i] = new unsigned int [ip_address_length];

// Open the "Route Number and Address Correspondence" file

Indexfile.open (IndexFileName, std :: ios_base :: in | std :: os_base :: out);

IF (indexfile.fail ()) {std :: cout << "/ n / n file (" << indexfilename << ") opens failed ./n/n"; char ch; std :: cin >> CH; exit (-1);

For (int i = 0; i

{

IndexFile >> INDEX [I];

For (int J = 0; j

IndexFile >> Addr [i] [j];

}

/ / Get this route number

For (int i = 0; i

IF (AddRequal (Selfaddress, Addr [i]))

{

Selfid = i;

Break;

}

// Route algorithm

IF (routCompute! = NULL)

(* RoutCompute) (NetArray, Valarray, Result, RoutNum, Selfid);

Else {

Std :: cout << "/ n / n routing calculation has not been set, the system exits ... / N / N";

EXIT (-1);

}

/ / Calculate the routing table of this router

For (int i = 0; i

{

For (int J = 0; j

IF (Result [i] [0] == Index [J]) _ ROUTTABLE [I] .SetaddresSto (AddR [J]);

For (int J = 0; j

IF (Result [I] [1] == Index [J])

_RoutTable [i] .SetAddressNexthop (addr [j]);

}

ROUTTABLE.SETTABLE (_ROUTTABLE);

Indexfile.close ();

/ * The comment section is a print routing table

Std :: cout << "/ n ----------------------------------------- -------- ";

For (int i = 0; i

{

Std :: cout << "/ n";

For (int J = 0; j

Std :: cout << _ routTable [i] .addressto [J];

Std :: cout << "->;

For (int J = 0; j

Std :: cout << _ routTable [i] .addressNexthop [J];

Std :: cout << "/ n";

}

Std :: cout << "----------------------------------------- -------- / n ";

* /

delete [] result;

Delete [] index;

delete [] addr;

}

Fourth, the transmission of network data

This is another key part. The implementation of this section is mostly analog to the primary language of network transmission. This is especially paid to the completeness of semantics, considering various factors in the distribution environment in network transmission. Of course, the implementation of this program is still simple. Code expansion can be performed if the network environment is real-time.

1, the network sends data to the router port

Method Name: Centralrout :: NetWriteData

Method parameters: NetData & _netData

Return Value: Bool type, True is successful, otherwise the network has been muted.

The process of the method: the process of writing data to the router port in the network. Handle IPDATA and Message respectively. If success, call the router to read port data.

Possible error: no

/ / The network sends data to the router port

Bool CentralRout :: NetWriteData (NetData & _netdata)

{

/ / Display position to the console

Std :: cout << "/ nat rout: / t";

For (int i = 0; i

Std :: cout << SelfadDress [i];

Std :: cout << std :: endl;

// Write an IP packet

IF (! _ netdata.IsMessage () && address (_netdata.getnexthop (), Selfaddress))

{// id is a key variable for the correspondence between the tracking data packet and the answer message in the router

ID = ID 0.000001;

NetData Message (TRUE);

Message.id = _netdata.id;

_netdata.id = ID;

/ / Return to the message to receive success

Message.SetaddressFrom (Selfaddress);

Message.SetaddresTo (_netdata.getprehop ());

Message.SetPrehop (SelfadDress);

Message.setnexthop (_netdata.getprehop ());

// Packet has arrived at the destination router

IF (ADDREQUAL (_NetData.Getaddressto ()

Std :: cout << "/ n / nok! surcess for one ipdata! / n / n";

ELSE {// packet does not reach the destination router, continue to process

DataListLength ;

IF (DataListLength> = MAX_DATA_QUEUE_LENGTH)

{// Network Surriness

STD: COUT << "/ n port data package too much, network surge, write packet failed ... / N";

Return False;

} // end of if

// write packet

DataINList.push_back (_netdata);

Std :: cout << "/ n / t receives IPDATA success ... / N";

// Router reads the packet

Receivedata ();

} // End of else

// Send a reply message

DataOutlist.push_front (message);

} ELSE // Writing is a web message

IF (_NetData.IsMessage () && addrequal (_netdata.getnexthop (), Selfaddress))

{// After receiving a response message, it is deleted a successful packet.

Std :: cout << "/ n / t receives Message Success ... / N";

DataTer = DataOutlist.begin ();

Dataiter! = DataOutlist.end (); Dataiter )

{

/ / Delete IP packets that have been sent successfully

IF (_netdata.id == DataITER-> ID)

{

DataOutlist.rase (Dataiter);

Dataiter = DataOutlist.begin ();

}

} // End of for

} // End of else IF

Return True;

}

转载请注明原文地址:https://www.9cbs.com/read-25382.html

New Post(0)