@@ -13,7 +13,6 @@ Router.on('error', (err) => {
1313 Router . close ( ) ;
1414} ) ;
1515
16- // TODO: How is Client meant to find out what Routers are connected to Switcher and their IP and Port??
1716interface Router {
1817 routerIn ?: String ; // eg E1
1918 routerOut ?: String ; // eg R2
@@ -42,7 +41,9 @@ Router.on('message', (msg, senderInfo) => {
4241 case 0 :
4342 // Type 0: Message from Server - accepted this as new Router on network
4443 console . log ( "Switcher has accepted this Router" ) ;
44+
4545 routerId = receivedMessage . message . routerId ;
46+ currentRouter . routerId = routerId ;
4647 let { address : routerAddress , port : routerPort } = receivedMessage . message ;
4748 console . log ( `Router active on ${ routerAddress } :${ routerPort } ` ) ;
4849 break ;
@@ -59,6 +60,7 @@ Router.on('message', (msg, senderInfo) => {
5960 break ;
6061 case 6 :
6162 // Type 6: Message from Switcher - updated information about this Router received
63+ console . log ( "Switcher sent updated information about this Router" )
6264 currentRouter = { ...currentRouter , ...receivedMessage } ;
6365 break ;
6466
@@ -67,23 +69,21 @@ Router.on('message', (msg, senderInfo) => {
6769 break ;
6870 }
6971
72+ console . log ( { currentRouter } ) ;
7073
7174 console . log ( JSON . parse ( msg . toString ( ) ) ) ;
72- // const { origin, serverTime, messageCount, data, message } = JSON.parse(msg.toString());
73- // console.log({ origin, serverTime, messageCount, data, message })
74- // console.log({ message: message.toString(), senderInfo });
7575} ) ;
7676
7777// Connect router to Switcher
7878function connectToSwitcher ( ) {
79- if ( routerId ) return console . log ( "Router already connected to Switcher" ) ;
79+ if ( routerId ) return console . log ( "Router already connected to Switcher" ) ;
8080 const message = prepareMessage ( 1 , "Router" ) ;
8181 sendMessageToSwitcher ( message ) ;
8282}
8383
8484// Get information about Clients from Switcher from Flow Table
8585function getFlowTableFromSwitcher ( ) {
86- if ( ! routerId ) return console . log ( "Router not connected to Switcher and cannot query" ) ;
86+ if ( ! routerId ) return console . log ( "Router not connected to Switcher and cannot query" ) ;
8787 const message = prepareMessage ( 2 , { routerId } ) ;
8888 sendMessageToSwitcher ( message ) ;
8989}
@@ -92,16 +92,16 @@ function getFlowTableFromSwitcher() {
9292function sendMessageToSwitcher ( message : Buffer ) {
9393 Router . send ( message , switcherPort , 'localhost' , ( err ) => {
9494 if ( err ) {
95- console . log ( 'Error sending data to Server ' )
95+ console . log ( 'Error sending data to Switcher ' )
9696 Router . close ( ) ;
9797 } else {
98- console . log ( 'Data sent to Server ' )
98+ console . log ( 'Data sent to Switcher ' )
9999 }
100100 } ) ;
101101}
102102
103103// Prepare message to be sent to Switcher
104- function prepareMessage ( type : number , message : any ) {
104+ function prepareMessage ( type : number , message : any ) {
105105 return Buffer . from ( JSON . stringify ( { type, message } ) ) ;
106106}
107107
0 commit comments