/* File: phone.cp Author: R. P. Futrelle Date: 8/1/98 Notice: Copyright 1998 by R. P. Futrelle and the College of Computer Science, Northeastern University Class: COM1204, Object-Oriented Design, Summer 1998. Proj: PS2.µ Metrowerks Code Warrior Mac. Done in IDE version 2.0.1. Purpose: Phone class methods defined. Modification history: 8/1/98: RPF. Started. */ #include "includes.h" int Phone::next_id = 0; Phone:: Phone(int num) : p_number(num), pickup_timer(new Timer), hangup_timer(new Timer), answered(false), dialing(false), me(false), picked_up(false), sound(false) { id = next_id++; } bool Phone:: is_state(bool A, bool D, bool M, bool P, bool S) { return((answered == A) && (dialing == D) && (me == M) && (picked_up == P) && (sound == S)); } void Phone:: set_state(bool A, bool D, bool M, bool P, bool S) { answered = A; dialing = D; me = M; picked_up = P; sound = S; } void Phone:: run() { do_states(); } void Phone:: do_states() { // Succesively check states and respond to messages // State: Quiescent = FFFFF = false, false, false, false, false if(is_state(false, false, false, false, false)) { if(!pickup_timer->is_on()) pickup_timer->start(5,12); if(pickup_timer->rang()) { pickup_timer->shut_off(); pickup_ph(); set_state(false, false, true, true, false); // picked up cout << "picked up phone: " << id << " at " << sys->time << endl; return; } } // State: Picked Up = FFTTF = false, false, true, true, false if(is_state(false, false, true, true, false)) { if(!hangup_timer->is_on()) hangup_timer->start(3,6); if(hangup_timer->rang()) { hangup_timer->shut_off(); hangup_ph(); set_state(false, false, false, false, false); // hung up cout << "hung up phone: " << id << " at " << sys->time << endl; return; } } } // note that in messages from phones, the source number for the phone // is not given, since the switch knows which phone is which. // (this will be different for cell phones) void Phone:: pickup_ph() { // create a pickup packet and place in the // packet slot of the wire in connector out connector->out->packet = new Packet(pickup, null_string, -1, -1, -1); } void Phone:: hangup_ph() { // similar to pickup // create a hangup packet and place in the // packet slot of the wire in connector out connector->out->packet = new Packet(hangup, null_string, -1, -1, -1); }