//*********************************************************************// // Change the status of the lights. // //*********************************************************************// private void ChangeLights(MessageProtocol command) { if (command == null) { return; // Nothing to do. } // Determine which light to turn on/off if (command.GetActionValue().Equals("Red_On")) { labelRed.Visible = true; } if (command.GetActionValue().Equals("Amber_On")) { labelAmber.Visible = true; } if (command.GetActionValue().Equals("Green_On")) { labelGreen.Visible = true; } if (command.GetActionValue().Equals("Red_Off")) { labelRed.Visible = false; } if (command.GetActionValue().Equals("Amber_Off")) { labelAmber.Visible = false; } if (command.GetActionValue().Equals("Green_Off")) { labelGreen.Visible = false; } }
//*********************************************************************// // Message was posted back to us. This is to get over the C# threading// // rules whereby we can only touch the UI components from the thread // // that created them, which is the form's main thread. // //*********************************************************************// public void MessageReceived(Object received) { String message = (String)received; listBoxOutput.Items.Add(DateTime.Now.ToString("h:mm:ss tt") + " " + message); // Fit the received message to message protocol MessageProtocol receivedMessage = new MessageProtocol(message); // If the message is for this client and it is a light change request if (receivedMessage.GetID() == uniqueIDNumber && receivedMessage.GetActionType().Equals("ChangeLight")) { ChangeLights(receivedMessage); } // If the connection request is accepted and this client is waiting to connect else if (receivedMessage.GetActionType().Equals("ConnectionRequestAccepted") && waitingToConnect == true) { // Save the assigned ID number by the server uniqueIDNumber = receivedMessage.GetActionValue(); idNumberLabel.Text = uniqueIDNumber; // Enable or disable some parts of UI buttonCarArrived.Enabled = true; randomSendCheckBox.Enabled = true; connectServerButton.Enabled = false; textBoxLightIP.Enabled = false; waitingToConnect = false; } // If the client receives message to disconnect from server else if (receivedMessage.GetActionType().Equals("ClientDisconnect")) { // Turn off the lights labelRed.Visible = false; labelAmber.Visible = false; labelGreen.Visible = false; // Reset ID number assigned to this client uniqueIDNumber = null; idNumberLabel.Text = ""; // Enable or disable parts of UI buttonCarArrived.Enabled = false; connectServerButton.Enabled = true; textBoxLightIP.Enabled = true; } }