private void ChangeLightRequest(int clientIndex, string actionValue) { // Send the message to the client MessageProtocol changeLightMessage = new MessageProtocol(connectedClients[clientIndex].GetID(), "ChangeLight", actionValue); sendString(changeLightMessage.GetMessageToSend(), connectedClients[clientIndex].GetIPAddress()); // Update the state on TrafficLightClient object if (actionValue.Equals("Red_On")) { connectedClients[clientIndex].SetIsRedOn(true); } if (actionValue.Equals("Amber_On")) { connectedClients[clientIndex].SetIsAmberOn(true); } if (actionValue.Equals("Green_On")) { connectedClients[clientIndex].SetIsGreenOn(true); } if (actionValue.Equals("Red_Off")) { connectedClients[clientIndex].SetIsRedOn(false); } if (actionValue.Equals("Amber_Off")) { connectedClients[clientIndex].SetIsAmberOn(false); } if (actionValue.Equals("Green_Off")) { connectedClients[clientIndex].SetIsGreenOn(false); } }
/// <summary> /// Disconnects all of the clients connected to server /// </summary> private void DisconnectAllClients() { // Send disconnect message to every client for (int i = (connectedClients.Count - 1); i >= 0; i--) { MessageProtocol changeLightMessage = new MessageProtocol(connectedClients[i].GetID(), "ClientDisconnect", "-"); sendString(changeLightMessage.GetMessageToSend(), connectedClients[i].GetIPAddress()); // Remove clients from the list connectedClients.RemoveAt(i); } }
//*********************************************************************// // 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; // Display the message with the timestamp listBoxOutput.Items.Add(DateTime.Now.ToString("h:mm:ss tt") + " " + message); // Convert the received message to message protocol object MessageProtocol receivedMessage = new MessageProtocol(message); // If server receives request for connection if (receivedMessage.GetActionType().Equals("ConnectionRequest")) { // Create a new ID newestConnectedID++; string newID = (newestConnectedID).ToString(); // Get the IP address from the message that was sent string newIPAddress = GetIPFromMessage(message); // Create a traffic light object & add to the connected Clients list int trafficLightIndex = connectedClients.Count; TrafficLightClient newTrafficLight = new TrafficLightClient(newID, newIPAddress, trafficLightIndex, trafficLightsPanel.Width, trafficLightsPanel.Height); connectedClients.Add(newTrafficLight); // Assign the client a unique ID MessageProtocol requestAccepted = new MessageProtocol("#", "ConnectionRequestAccepted", newID); sendString(requestAccepted.GetMessageToSend(), newTrafficLight.GetIPAddress()); } // If recieves request to disconnect else if (receivedMessage.GetActionType().Equals("ClientDisconnecting")) { DisconnectAllClients(); } // If receives a car from traffic light client else if (receivedMessage.GetActionType().Equals("CarSent")) { string clientIDToUpdate = receivedMessage.GetID(); // Find the client id that the car was sent from for (int i = 0; i < connectedClients.Count; i++) { if (connectedClients[i].GetID() == clientIDToUpdate) { // Add car to the client if found connectedClients[i].AddCar(); break; } } } }