/* File: ps2-sampler2.doc 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: Some code and output from the system to help with PA2, the switch coding assignment. The code samples here are fragments only, so the full files have to be studied to understand them fully. At the end, I've appended the PA2 assignment text that was emailed to the class. By studying the previous handout, and this handout, as well as the Web page and the full project code, doing the assignment won't be too hard. The actual amount of code you need to add/modify is not much. Modification history: 8/8/98: RPF. Started. */ // The connector/wire architecture is shown in the two corresponding // class definitions in link.h: class Connector { public: static int next_id; int id; Wire *in; Wire *out; Connector(); }; class Wire { public: static int next_id; int id; Packet *packet; Wire(); }; //You can see how they're wired together in the System constructor: // 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; } } // Using them is shown in the definitions for one of the phone actions, // found in phone.cp // 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); } // The following code is in switch.cp. // Here's a simple test that puts a dial packet in each phone // when time == 50 and looks inside to see what's there. void Switch:: run() { //loop over all phones when time = 3. if(sys->time == 50) { cout << "In switch run test at time == 50" << endl; for(int i = 0; i < num_phones ; i++) {connectors[i]->out->packet = new Packet(ring, null_string, -1, -1, -1); cout << "Control string in switch out line for phone: " << i << " is: " << *(connectors[i]->out->packet->control) << endl; states[i]->set_state(false, false, false, true); cout << "Switch phone state ringing slot for phone: " << i << " is: " << states[i]->ringing << endl; } } } // And finally, here's the results of running 100 time steps: /* Output of tests for version frozen on 8/8/98, 1:25pm is: picked up phone: 1 at 6 picked up phone: 2 at 7 picked up phone: 0 at 9 hung up phone: 1 at 12 hung up phone: 0 at 14 hung up phone: 2 at 14 picked up phone: 0 at 22 picked up phone: 1 at 23 picked up phone: 2 at 23 hung up phone: 0 at 26 hung up phone: 1 at 27 hung up phone: 2 at 28 picked up phone: 0 at 34 picked up phone: 1 at 36 hung up phone: 0 at 40 picked up phone: 2 at 41 hung up phone: 1 at 42 hung up phone: 2 at 47 In switch run test at time == 50 Control string in switch out line for phone: 0 is: ring Switch phone state ringing slot for phone: 0 is: 1 Control string in switch out line for phone: 1 is: ring Switch phone state ringing slot for phone: 1 is: 1 Control string in switch out line for phone: 2 is: ring Switch phone state ringing slot for phone: 2 is: 1 picked up phone: 0 at 51 picked up phone: 1 at 53 hung up phone: 0 at 55 hung up phone: 1 at 59 picked up phone: 2 at 59 picked up phone: 0 at 63 hung up phone: 2 at 66 picked up phone: 1 at 67 hung up phone: 0 at 69 hung up phone: 1 at 73 picked up phone: 2 at 75 picked up phone: 0 at 80 hung up phone: 2 at 82 picked up phone: 1 at 86 hung up phone: 0 at 87 hung up phone: 1 at 92 picked up phone: 2 at 93 picked up phone: 0 at 97 hung up phone: 2 at 98 */ /* Here's the text excerpt from the Web page describing the assignment: "Your Assignment: What you are to do is to add code to the run() method for the switch which is very similar to that for the phone. The switch should scan it's phone states each time it's run (3 of them in the example I did). For each phone it should execute the code corresponding to one of the states Quiescent or Picked up, whichever state it's in. It will be in the quiescent state initially. When looking at a state, e.g., Picked up, it should check the in line for that phone and see what message packet, if any, is there. If there is a hangup packet then the switch should change the state for that phone back to Quiescent. It should delete the packet it just got, because it is not sending it on to any other node, and reset the in packet to NULL. You will not be changing the code for the switches that I've already built. Thus, the phones will pick up and hang up at various times and the switch will notice that activity and change its internal record of the attached phones states. The switch must know each attached phone's state so it will know whether or not it can ring the phone. You are to add cout statements to show that the switch is indeed changing states correctly, in addition to the printouts already installed for the phones. The result of your work should be a Macintosh Code Warrior project on a Mac floppy plus hardcopy of any files to which you've made signifcant changes. You must add your name to the Author: field of the file header of any file you change as well as updating other fields such as the Date and Purpose and Modification History and its dates. Hand in your work even if your code does not compile. Paste in the printout from your test(s) at the end of the main.cp file as a long comment, as I have. Comments -- You must add as many comments as possible to the code that you add or change. Overview comments are the most important, e.g., for an entire function. Line-by-line comments are less crucial and often clutter the code too much. The basics of everything you need to do are implemented and run in the current version of the code (version frozen 8/8/98, 1:25pm). The system is properly constructed with connector and wires, the phone state array in the switch is in and working, the state changing for the phone between hung up and picked up works, and I have also shown how to place messages in the out lines for the switch and how to change the switch states. Memory management (notes for later): Packets are the most volatile objects in the system. The nodes and wires are essentially permanent and unchanging in any simulation run. The general philosophy in this project is the notion of source and sink for packets. Thus, when a packet is produced it then moves through the network to some final destination where it is not used further by any object. At this point it can be safely deleted. If an intermediate node wants to retain the information in a packet, e.g., for logging or debugging purposes, it can always create its own copy and copy the information in the packet into its copy. Since we use pointers universally in our implementation, there is only one actual packet object created for transmission and the pointer to it is handed through from node to node to simulate the "movement" of the packet." */