void connectUserToATC(User user, Connection connection) { string atcID = connection.destinationNumber[0].ToString() + connection.destinationNumber[1].ToString(); ATC atc = connectedATCs.Find(x => x.id == atcID); if (atc == null) { log($"User {user.id} requested ATC {ATCNameService.GetName(atcID)} ({atcID}) that is not connected to the ATC."); foreach (ATC anotherATC in connectedATCs) { log($"Requested ATC {anotherATC.name} to connect user {user.id} to ATC {ATCNameService.GetName(atcID)} ({atcID})."); anotherATC.connectUserToATC(user, connection); } } else { string existingConnectionID = ATC.FindExistingConnectionID(this.id, connection, atc, this.id); if (existingConnectionID != null) { atc.handleIncomingConnection(existingConnectionID, this, getUserId(connection.destinationNumber), connection.id, user.GlobalID); log($"Connected User {user.id} to ATC {ATCNameService.GetName(atcID)} ({atcID}). Connection ID: {connection.id}"); } else { string connectionID = atc.handleIncomingConnection(this, getUserId(connection.destinationNumber), connection.id, user.GlobalID); connection.ConnectATC(atc, connectionID); log($"Connected User {user.id} to ATC {ATCNameService.GetName(atcID)} ({atcID}). Connection ID: {connection.id}"); } } }
void handleIncomingBusyOffline(Signal signal, Connection connection, string atcID) { string sourceAtcID = getATCid(signal.sender.id); ATC atcToRemove = connection.connectedATCs.Keys.ToList().Find(a => a.id == sourceAtcID); if (atcToRemove != null) { connection.connectedATCs.Remove(atcToRemove); log($"Removed ATC {atcToRemove.name} from connection {connection.id}."); } Signal busyOfflineSignal = new Signal((Transmitter)this, signal.type, $"{ATCNameService.GetName(sourceAtcID)} ({sourceAtcID})"); Signal cancelSignal = new Signal((Transmitter)this, SignalType.cancel, $"{ATCNameService.GetName(sourceAtcID)} ({sourceAtcID})"); foreach (User user in connection.users) { user.handle(busyOfflineSignal); user.handle(cancelSignal); log($"Sended User {user.id} a cancel signal. Connection ID: {connection.id}."); } foreach (ATC connectedATC in connection.connectedATCs.Keys) { connectedATC.handleSignalFromATC(signal, this.id, connection.connectedATCs[connectedATC]); log($"Sended ATC {connectedATC.name} a cancel signal. Connection ID: {connection.id}."); } if (connection.users.Count + connection.connectedATCs.Keys.Count == 1) { Signal connectionClosedSignal = new Signal((Transmitter)this, SignalType.tone, "Connection closed."); if (connection.users.Count == 1) { User lastUser = connection.users.First(); lastUser.handle(connectionClosedSignal); log($"Sended User {lastUser.id} a tone signal. Connection ID: {connection.id}. Connection Closed."); } else { ATC lastATC = connection.connectedATCs.Keys.First(); lastATC.handleSignalFromATC(connectionClosedSignal, this.id, connection.connectedATCs[lastATC]); log($"Sended ATC {lastATC.name} a tone signal. Connection ID: {connection.id}. Connection Closed."); } connections.Remove(connection); connectionsChanged(); } }
public static bool Save(string atcName, string id) { Dictionary <string, string> ATCNameServiceDataBase = ATCNameService.Load(); if (ATCNameServiceDataBase == null) { ATCNameServiceDataBase = new Dictionary <string, string>(); } ATCNameServiceDataBase[id] = atcName; BinaryFormatter bf = new BinaryFormatter(); FileStream fsout = new FileStream($"ANS.dat", FileMode.Create, FileAccess.Write, FileShare.None); try { using (fsout) { bf.Serialize(fsout, ATCNameServiceDataBase); return(true); } } catch (Exception err) { Console.WriteLine("Error occured:" + err.Message); return(false); } finally { if (fsout != null) { fsout.Close(); } } }
void handleCancel(Signal signal, Connection connection, string atcID) { if (connection.status == ConnectionStatus.TryingToAddAnotherUser && signal.sender is User) { connection.status = ConnectionStatus.TryingToAddAnotherUser; connection.destinationNumber = ""; (signal.sender as User).handle(new Signal((Transmitter)this, SignalType.tone, $"Destination number cleared.")); if (isGoingToMakeOutgoingCall.ContainsKey(signal.sender.id)) { isGoingToMakeOutgoingCall.Remove(signal.sender.id); } return; } string sourceAtcID = ""; if (signal.sender is User) { sourceAtcID = getATCid((signal.sender as User).GlobalID); } else { sourceAtcID = signal.sender.id; } bool isSignalLocal = sourceAtcID == this.id; if (isSignalLocal) { connection.users.Remove(signal.sender as User); log($"Removed User {signal.sender.id} from connection {connection.id}."); } else { ATC atcToRemove = connection.connectedATCs.Keys.ToList().Find(a => a.id == sourceAtcID); if (atcToRemove != null) { connection.connectedATCs.Remove(atcToRemove); log($"Removed ATC {atcToRemove.name} from connection {connection.id}."); } } Signal cancelSignal = new Signal((Transmitter)this, SignalType.cancel, isSignalLocal ? signal.sender.id : $"{ATCNameService.GetName(sourceAtcID)} ({sourceAtcID})"); foreach (User user in connection.users) { user.handle(cancelSignal); log($"Sended User {user.id} a cancel signal. Connection ID: {connection.id}."); } foreach (ATC connectedATC in connection.connectedATCs.Keys) { if (connectedATC.id != atcID) { connectedATC.handleSignalFromATC(signal, this.id, connection.connectedATCs[connectedATC]); log($"Sended ATC {connectedATC.name} a cancel signal. Connection ID: {connection.id}."); } } if (connection.users.Count + connection.connectedATCs.Keys.Count == 1) { Signal connectionClosedSignal = new Signal((Transmitter)this, SignalType.tone, "Connection closed."); if (connection.users.Count == 1) { User lastUser = connection.users.First(); lastUser.handle(connectionClosedSignal); log($"Sended User {lastUser.id} a tone signal. Connection ID: {connection.id}. Connection Closed."); } else { ATC lastATC = connection.connectedATCs.Keys.First(); if (lastATC.id != atcID) { lastATC.handleSignalFromATC(connectionClosedSignal, this.id, connection.connectedATCs[lastATC]); log($"Sended ATC {lastATC.name} a tone signal. Connection ID: {connection.id}. Connection Closed."); } } connections.Remove(connection); connectionsChanged(); } }
public ATC(string id, string name) : base(id) { ATCNameService.Save(name, id); }