private void handleSimServerRequests(MessageWrapper wrappedMessage, ConsoleTcpListenerArgs args) { //retreive actual message payload and handle the different message types switch (wrappedMessage.MessageType) { case MessageBase.MESSAGE_JOIN_REQUEST: XMLSimJoinRequest message = (XMLSimJoinRequest)wrappedMessage.Message; if (message.valid()) { try { //accept the connection from the SA XMLSimJoinAcceptance joinAccept = new XMLSimJoinAcceptance(); joinAccept.ParticipantId = message.ParticipantId; //echo back joinAccept.ParticipantIp = message.ParticipantIp; joinAccept.ElementId = SimulationManagementHelper.generateElementId(); joinAccept.RequestConfig = true; //wrap XML message with DEM header and send out MessageWrapper wrap = new MessageWrapper(); wrap.ApplicationId = _simConfig.APP_ID; wrap.ApplicationRole = _simConfig.APP_ROLE; wrap.MessageType = MessageBase.MESSAGE_JOIN_ACCEPTANCE; wrap.Message = joinAccept; System.Console.WriteLine("Sending: \n" + wrap.toXmlString()); byte[] data = Encoding.UTF8.GetBytes(wrap.toXmlString()); NetworkStream netstream; netstream = args.Client.GetStream(); netstream.Write(data, 0, data.Length); String incoming = String.Empty; bool done = false; System.Console.WriteLine("Waiting for SA "); //wait for the config from the SA while (!done) { netstream = args.Client.GetStream(); if (netstream.DataAvailable && netstream != null) { data = new Byte[args.Client.ReceiveBufferSize]; netstream.Read(data, 0, (int)args.Client.ReceiveBufferSize); incoming = Encoding.UTF8.GetString(data); //System.Console.WriteLine("Received Data"+incoming); XmlSerializer deserializer = new XmlSerializer(typeof(MessageWrapper)); MessageWrapper incomingWrappedMessage; //deserialize using (TextReader reader = new StringReader(incoming)) { incomingWrappedMessage = (MessageWrapper)deserializer.Deserialize(reader); } //accepted to join the simulation, send config if (String.Equals(incomingWrappedMessage.MessageType, MessageBase.MESSAGE_SIM_APP_CONFIG)) { System.Console.WriteLine("Received:" + incomingWrappedMessage.toXmlString()); XMLSimApplication current = (XMLSimApplication)incomingWrappedMessage.Message; //add the SA to the list SimulationParticipant participant = new SimulationParticipant(current,new NetworkConnectionState(args.Client)); _participantList.Add(current.ParticipantId, participant); done = true; if (_participantList.Values.Count > 1) { List<KeyValuePair<string,SimulationParticipant>> allParticipants = _participantList.ToList(); List<XMLSimApplication> allApplications = new List<XMLSimApplication>(); //build a list of applications to update the new app that joined foreach (var app in allParticipants) { allApplications.Add(app.Value.SimulationApplication); } XMLParticipantList updatedList = new XMLParticipantList(); updatedList.Participants = allApplications; //wrap XML message with DEM header and send out wrap = new MessageWrapper(); wrap.ApplicationId = _simConfig.APP_ID; wrap.ApplicationRole = _simConfig.APP_ROLE; wrap.MessageType = MessageBase.MESSAGE_UPDATE_PARTICPANT_LIST; wrap.Message = updatedList; //notifyParticipants(wrap);//tell everybody } } } }//end while } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { args.Stream.Close(); args.Client.Close(); } } break; } }
//getsw all tcp messages public void tcpSimulationServer_MessageReceived(ConsoleTcpListenerArgs args) { if (String.IsNullOrEmpty(args.Message)) { logToConsole("Console Tcp Listener received empty message"); return; } XmlSerializer deserializer = new XmlSerializer(typeof(MessageWrapper)); MessageWrapper wrappedMessage; //deserialize using (TextReader reader = new StringReader(args.Message)) { wrappedMessage = (MessageWrapper)deserializer.Deserialize(reader); } if (wrappedMessage != null) handleSimServerRequests(wrappedMessage, args); //handle the message - pass to big switch statement }