/// <summary> /// Display connection prompt and connect to master /// </summary> private void ConnectToMaster() { try { //show dialog box to input server address var dialog = new Connect(address); if (dialog.ShowDialog() != true) { dialog.Close(); } else { address = dialog.ResponseText; if (address == "") { address = "localhost:50002/ATCMaster"; } //connect to the server DuplexChannelFactory<IATCMasterController> IATCMasterFactory; NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue; tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue; string sURL = "net.tcp://" + address; IATCMasterFactory = new DuplexChannelFactory<IATCMasterController>(this, tcpBinding, sURL); m_ATCMaster = IATCMasterFactory.CreateChannel(); //get the current state of the airports m_airports = m_ATCMaster.GetAirportData(); //add airports to the airport listbox listBoxAirports.Items.Clear(); foreach (Airport airport in m_airports) { ListBoxItem lbItem = new ListBoxItem(); lbItem.Content = airport.name; listBoxAirports.Items.Add(lbItem); } //select the first airport listBoxAirports.SelectedIndex = 0; //refresh the lists this.Refresh(); } } //Exceptions: displays the message then prompts again for a server catch (EndpointNotFoundException exception) { MessageBox.Show("Unable to connect!\n\n" + exception.Message); ConnectToMaster(); } catch (NullReferenceException exception) { MessageBox.Show("Server is not in a state to accept clients - check to see if all your slaves are connected!\n\n" + exception.Message); ConnectToMaster(); } catch (TimeoutException exception) { MessageBox.Show("Server timed out. Try to fix the problem then reconnect.\n\n" + exception.Message); ConnectToMaster(); } catch (CommunicationException exception) { MessageBox.Show("Communication Exception - try restarting the Master/Slaves\n\n" + exception.Message); ConnectToMaster(); } catch (Exception exception) { MessageBox.Show(exception.Message); Application.Current.Shutdown(); } }
/// <summary> /// Connects to the Master server and initialises the slave, getting back the allocated airport /// </summary> private void Initialise() { try { //connect to the server DuplexChannelFactory<IATCMasterController> IATCMasterFactory; NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue; tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue; string sURL = "net.tcp://" + address; IATCMasterFactory = new DuplexChannelFactory<IATCMasterController>(this, tcpBinding, sURL); m_ATCMaster = IATCMasterFactory.CreateChannel(); //add callback to Master and get allocated airport m_Airport = m_ATCMaster.InitialiseSlave(); //set simulation time to 0 m_SimTime = 0; //display the allocated airport (for debug purposes) System.Console.WriteLine(m_Airport.name); } catch (EndpointNotFoundException exception) { //if you get one of these four exceptions, print the error and try again, asking for a new address System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (CommunicationObjectFaultedException exception) { System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (CommunicationObjectAbortedException exception) { System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (NullReferenceException exception) { //usually this exception happens when there is four slaves connected and the master rejects the InitialiseSlave call System.Console.WriteLine("Server connection failed - Maybe there are already the max slaves connected?\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } }
/// <summary> /// Display connection prompt and connect to master /// </summary> private void ConnectToMaster() { try { //show dialog box to input server address var dialog = new Connect(address); if (dialog.ShowDialog() != true) { dialog.Close(); } else { address = dialog.ResponseText; if (address == "") { address = "localhost:50002/ATCMaster"; } //connect to the server DuplexChannelFactory <IATCMasterController> IATCMasterFactory; NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue; tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue; string sURL = "net.tcp://" + address; IATCMasterFactory = new DuplexChannelFactory <IATCMasterController>(this, tcpBinding, sURL); m_ATCMaster = IATCMasterFactory.CreateChannel(); //get the current state of the airports m_airports = m_ATCMaster.GetAirportData(); //add airports to the airport listbox listBoxAirports.Items.Clear(); foreach (Airport airport in m_airports) { ListBoxItem lbItem = new ListBoxItem(); lbItem.Content = airport.name; listBoxAirports.Items.Add(lbItem); } //select the first airport listBoxAirports.SelectedIndex = 0; //refresh the lists this.Refresh(); } } //Exceptions: displays the message then prompts again for a server catch (EndpointNotFoundException exception) { MessageBox.Show("Unable to connect!\n\n" + exception.Message); ConnectToMaster(); } catch (NullReferenceException exception) { MessageBox.Show("Server is not in a state to accept clients - check to see if all your slaves are connected!\n\n" + exception.Message); ConnectToMaster(); } catch (TimeoutException exception) { MessageBox.Show("Server timed out. Try to fix the problem then reconnect.\n\n" + exception.Message); ConnectToMaster(); } catch (CommunicationException exception) { MessageBox.Show("Communication Exception - try restarting the Master/Slaves\n\n" + exception.Message); ConnectToMaster(); } catch (Exception exception) { MessageBox.Show(exception.Message); Application.Current.Shutdown(); } }
/// <summary> /// Connects to the Master server and initialises the slave, getting back the allocated airport /// </summary> private void Initialise() { try { //connect to the server DuplexChannelFactory <IATCMasterController> IATCMasterFactory; NetTcpBinding tcpBinding = new NetTcpBinding(); tcpBinding.MaxReceivedMessageSize = System.Int32.MaxValue; tcpBinding.ReaderQuotas.MaxArrayLength = System.Int32.MaxValue; string sURL = "net.tcp://" + address; IATCMasterFactory = new DuplexChannelFactory <IATCMasterController>(this, tcpBinding, sURL); m_ATCMaster = IATCMasterFactory.CreateChannel(); //add callback to Master and get allocated airport m_Airport = m_ATCMaster.InitialiseSlave(); //set simulation time to 0 m_SimTime = 0; //display the allocated airport (for debug purposes) System.Console.WriteLine(m_Airport.name); } catch (EndpointNotFoundException exception) { //if you get one of these four exceptions, print the error and try again, asking for a new address System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (CommunicationObjectFaultedException exception) { System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (CommunicationObjectAbortedException exception) { System.Console.WriteLine("Server connection failed\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } catch (NullReferenceException exception) { //usually this exception happens when there is four slaves connected and the master rejects the InitialiseSlave call System.Console.WriteLine("Server connection failed - Maybe there are already the max slaves connected?\n\n" + exception.Message + "\n\nInput Server Address (leave blank for localhost:50002/ATCMaster)"); address = System.Console.ReadLine(); if (address == "") { address = "localhost:50002/ATCMaster"; } Initialise(); } }