private void ProcessCarMessage(string str) { CarMessage msg = CarMessage.Create(str); if (msg.Type == MessageType.Request) { requestQueue.Add(msg); CarMessage reply = GenerateMessage(MessageType.Reply); if (!onTheBridge) // check not in CS { SendMessage(reply, msg.Id); } else { deferedRequests.Add(new DifferedMessage(reply, msg.Id)); } } else if (msg.Type == MessageType.Reply) { repliesNo++; } else if (msg.Type == MessageType.Release) { CarMessage m = requestQueue.Find(t => t.Id == msg.Id); if (m != null) { requestQueue.Remove(m); } } }
private CarMessage GenerateMessage(char type) { DateTime now = DateTime.Now; long span = now.Ticks; CarMessage message = new CarMessage(type, this.Id, span); return(message); }
private void AnnounceRelease() { CarMessage releaseMsg = GenerateMessage(MessageType.Release); foreach (int carId in RequestSet) { SendMessage(releaseMsg, carId); } }
private void WantPermissionToCS() { CarMessage request = GenerateMessage(MessageType.Request); requestQueue.Add(request); foreach (int carId in RequestSet) { SendMessage(request, carId); } }
public static CarMessage Create(string text) { string[] items = text.Split('-'); char ty = items[0][0]; int id = int.Parse(items[1]); long span = long.Parse(items[2]); CarMessage msg = new CarMessage(ty, id, span); return(msg); }
public void SendMessage(CarMessage message, int serverPort) { try { Client client = GetClientOf(serverPort); if (client != null) { client.SendData(message.ToString()); } } catch { //WriteMessage(String.Format("Exception sending {0} msg from {1} to {2}", type, id, serverPort)); } }
public void Move() { if (InRoad != null) { if (InRoad.IsBridge) { WantPermissionToCS(); while (true) { if (repliesNo == RequestSet.Count && requestQueue.Count > 0 && requestQueue[0].Id == this.Id) { repliesNo = 0; onTheBridge = true; break; } } } while (moving) { AdjustSpeed(); Move1(); } if (InRoad.IsBridge) { if (InRoad.ChangedDirection) { InRoad.ChangeDirection(); InRoad.ChangedDirection = false; } onTheBridge = false; foreach (DifferedMessage msg in deferedRequests) { SendMessage(msg.Message, msg.Receiver); } deferedRequests.Clear(); AnnounceRelease(); CarMessage ownRequest = requestQueue.Find(m => m.Id == this.Id); if (ownRequest != null) { requestQueue.Remove(ownRequest); } } } }
public DifferedMessage(CarMessage m, int s) { this.Message = m; this.Receiver = s; }