/* File: system.cp Author: R. P. Futrelle Date: 8/8/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: Method definitions for classes System, Timer, and Switch_Phone_State. Modification history: 8/1/98: RPF. Started. 8/3/98: RPF. Timer finished, tested. System constructor done. 8/5/98: RPF. Problems with vectors. Punt. Switch to ordinary arrrays (of ptrs). Works. 8/8/98: RPF. Changed to Connector/Wire architecture. */ #include "includes.h" // THE SYSTEM // CONSTRUCTION System:: System(int how_many_phones) : time(0), num_phones(how_many_phones), num_switches(1), the_log(new Log) { // first, create the vector objects needed // note that superclass is used for switches and phones // create one switch switches[0] = new Switch; switches[0]->num_phones = num_phones; // create phones and links and link phones to switch // also populate the states array. for(int i = 0; i < num_phones ; i++) { phones[i] = new Phone(i); connector1 = new Connector; connector2 = new Connector; wire1 = new Wire; wire2 = new Wire; phones[i]->connector = connector1; switches[0]->connectors[i] = connector2; // connect wire1 to switch in and phone out switches[0]->connectors[i]->in = wire1; phones[i]->connector->out = wire1; // and wire2 to switch out and phone in switches[0]->connectors[i]->out = wire2; phones[i]->connector->in = wire2; switches[0]->states[i] = new Switch_Phone_State; } } // RUNNING THE SYSTEM void System:: run(int how_many_steps, string *log_file_name, bool c_logging, int rseed) { max_steps = how_many_steps; for(time = 0; time < max_steps; time++) { for(int i = 0; i < num_phones; i++) { phones[i]->run(); } switches[0]->run(); } if(flog || clog) {} // print summary stats to cout in either event } // TIMER IMPLEMENTATION Timer:: Timer() : on(false), set_for(0) { } void Timer:: start(int duration) { set_for = sys->time + duration; on = true; } int Timer:: start(int min_duration, int max_duration){ set_for = sys->time + rand_range(min_duration, max_duration); on = true; return(set_for); } void Timer:: shut_off() { on = false; set_for = 0; } bool Timer:: is_on() { return(on); } bool Timer:: rang() { return(set_for <= sys->time); } int Timer:: remaining (){ return(set_for - sys->time); }