public void Add(Modem modem, string phoneNumber) { if (modem == null || phoneNumber == null) throw new ArgumentNullException(); else if (!IsDialable(phoneNumber)) throw new ArgumentException(); lock (this._DirectoryLock) { this._Directory.Add(phoneNumber, new Subscriber(modem)); modem.Dial += OnDial; modem.Answer += OnAnswer; modem.Hangup += OnHangup; } }
private void DoHangup() { Modem peer; lock (this._StateLock) { peer = this._Peer; this._Peer = null; this._State = ModemState.Command; } if (peer != null) { peer.ChannelBreak(); this.Hangup(this); } }
private void DoDial(string phoneNumber) { Modem peer = null; ConnectResult result = this.Dial(this, phoneNumber, ref peer); if (result == ConnectResult.Connect && peer != null) { peer.OnOOBReceived(OOBSignal.Ring); } Thread.Sleep(1000); switch (result) { case ConnectResult.Connect: this.ChannelWrite(ConnectMessage); lock (this._StateLock) { this._Peer = peer; this._State = ModemState.Data; } break; case ConnectResult.NoDialtone: this.ChannelWrite(NoDialtoneMessage); break; case ConnectResult.NoAnswer: this.ChannelWrite(NoAnswerMessage); break; case ConnectResult.Busy: this.ChannelWrite(BusyMessage); break; } }
private void DoAnswer() { Modem peer = null; if (this.Answer(this, ref peer)) { lock (this._StateLock) { this._Peer = peer; this._State = ModemState.Data; this._Ringing = false; } } }
public ConnectResult Connect(string callerPhoneNumber, string dstPhoneNumber, out Modem modem) { lock (this._DirectoryLock) { Subscriber caller; Subscriber dst; if (!this._Directory.TryGetValue(callerPhoneNumber, out caller)) throw new ArgumentException(); if (caller.OffHook) { modem = null; return ConnectResult.NoDialtone; // "NO DIALTONE" } caller.OffHook = true; if (!this._Directory.TryGetValue(dstPhoneNumber, out dst)) { caller.OffHook = false; modem = null; return ConnectResult.NoAnswer; // "NO ANSWER" } else if (dst.OffHook) { caller.OffHook = false; modem = null; return ConnectResult.Busy; // "BUSY" } dst.OffHook = true; caller.Peer = dst; dst.Peer = caller; modem = dst.Modem; return ConnectResult.Connect; // "CONNECT" } //lock }
public Subscriber(Modem modem) { this.Modem = modem; this.Peer = null; }
private void OnHangup(Modem sender) { this.Disconnect(sender.PhoneNumber); }
private ConnectResult OnDial(Modem sender, string phoneNumber, ref Modem peer) { return this.Connect(sender.PhoneNumber, phoneNumber, out peer); }
private bool OnAnswer(Modem sender, ref Modem peer) { lock (this._DirectoryLock) { Subscriber receiver; if (!this._Directory.TryGetValue(sender.PhoneNumber, out receiver)) throw new ArgumentException(); if (receiver == null || receiver.Peer == null) return false; peer = receiver.Peer.Modem; } return (peer != null); }