Commits

Joseph Bane committed 47bef33 Merge

Merged with Graham after extreme programming session.

Comments (0)

Files changed (5)

 
 void Process::vote(Request r)
 {
+  if (!up) { return; }  // Do nothing if Process down
+
   if (simulation->getTime() - r.getTimestamp() > delta) {
     // Do nothing.  This represents rejection of the original request.
   }
 
 void Process::vote(Message m)
 {
+  if (!up) { return; }  // Do nothing if Process down
+
   if (simulation->getTime() - m.getRequestTime() > delta*2) {
     // Create a new message object to reject
        simulation->sendMessage( Message(m.getTo(), m.getFrom(), m.getRequestTime(), m.getRequestID(), Message::vote::reject) );
 void Process::tick()
 {
   destabilize();  // Process might go down
-  if (!up) { return; }  // Do nothing if Process down
   readRequests();  // Deal with incoming Requests
   readMessages();  // Deal with incoming Messages
 
+  if (!up) { return; }  // Do nothing else if Process down
+
   // Stochastically generate a Request
   const float RequestGenerationProbability = 0.05f;
   float Random = frand();
 {
   set<Request>::iterator r;
   for (r=requests.begin(); r!=requests.end(); ++r) {
-    //r->tick();
-    if (r->getTTA() <= 0) {
+
+    // decrement TTA
+    Request* r2 = new Request(this, r->getTimestamp(), r->getID(), r->getTTA()-1);
+    requests.erase(requests.find(*r));
+    requests.insert(*r2);
+
+    if (r->getTTA() == 0) {
       vote(*r);
+      r->display();
+      removeRequest(*r);
     }
   }
 }
 void Process::readMessages()
 {
   for (set<Message>::iterator m=messages.begin(); m!=messages.end(); ++m) {
-    //m->tick();
-    if (m->getTTA() <= 0) {
+
+    // decrement TTA
+    Message* m2 = new Message(m->getTo(), m->getFrom(), m->getRequestTime(), m->getRequestID(), m->getVote() );
+    messages.erase(messages.find(*m));
+    m2->tick();
+    messages.insert(*m2);
+
+    if (m->getTTA() == 0) {
       vote(*m);
+      m->display();
+      removeMessage(*m);
     }
   }
 }
 
 bool Process::isUp()
 {
-  if (up) {
-    return true;
-  }
-  return false;
+  return up;
 }
 
 
   TTA = (rand() % MaximumRequestTTA) + 1;
 }
 
+Request::Request(Process *p, type::time ts, type::requestID simID, type::time t)
+{
+  ID = simID;
+  owner = p;
+  timestamp = ts;
+  TTA = t;
+}
+
 void Request::display() const
 {
   std::cout << "Request Message (" << getID() << "): \n";
 public:
   // Processes pass a sequential request ID
   Request(Process *p, type::time ts, type::requestID simID);
+  Request(Process *p, type::time ts, type::requestID simID, type::time TTA);
   void display() const;
   type::requestID getID() const;
   Process *getOwner() const;
 
 void Simulation::tick()
 {
-    // Iterate over Processes
-    for(size_t i = 0; i < Processes.size(); ++i)
-    {
-      Processes[i].tick();
-    }
+  display();
 
-    ++Time;
+  // Iterate over Processes
+  for(size_t i = 0; i < Processes.size(); ++i) {
+    Processes[i].tick();
+  }
+
+  ++Time;
 }
 
+
+void Simulation::display()
+{
+  // Display basic info
+  if (majorityPresent()) {
+    cout << "MAJORITY\n";
+  }
+  else {
+    cout << "SYSTEM DOWN\n";
+  }
+}
+
+
 type::time Simulation::getTime()
 {
     return Time;
 
 void Simulation::sendMessage(const Message& MessageToSend)
 {
-
+  int pID = MessageToSend.getTo();
+  Processes[pID].pushMessage(MessageToSend);
 }
 
 
 {
 public:
     Simulation(const unsigned int ProcessCount);
-    //Updates the simulation by one timestep
-    void tick();
+    void tick();  // Updates the simulation by one timestep
     void sendMessage(const Message& MessageToSend);
     type::time getTime();
     type::requestID highestRequest;
+    void display();
 
 private:
     type::time Time;
     std::vector<Process> Processes;
-    //Indexed by Process ID, returns the message queue for that process.
-    std::vector<std::vector<Message> > ProcessMessageQueues;
     bool majorityPresent();
     int majority;