Joseph Bane avatar Joseph Bane committed e88c103 Merge

Merged.

Comments (0)

Files changed (8)

 
 bool Message::hasArrived() const
 {
-    return (TTA == 0);
+    return (TTA <= 0);
 }
+
+bool operator<(const Message &LHS, const Message &RHS)
+{
+    return LHS.requestID < RHS.requestID;
+}
+
+
 
 class Message
 {
+  friend bool operator<(const Message &LHS, const Message &RHS);
+
 public:
   // Possible values for a message vote; null means hasn't been voted on.
   class vote
 
 };
 
+
+bool operator<(const Message &LHS, const Message &RHS);
+
 #endif
 #include <math.h>
 #include <algorithm>
 #include <list>
-#include <vector>
+#include <set>
 #include "Simulation.hpp"
 #include "Process.hpp"
 #include "Request.hpp"
 
 void Process::pushRequest(Request r)
 {
-  requests.push_back(r);
+  requests.insert(r);
 }
 
 
 void Process::pushMessage(Message m)
 {
-  messages.push_back(m);
+  messages.insert(m);
 }
 
 
 
 void Process::readRequests()
 {
-  for (vector<Request>::iterator r=requests.begin(); r!=requests.end(); ++r) {
-    r->tick();
+  set<Request>::iterator r;
+  for (r=requests.begin(); r!=requests.end(); ++r) {
+    //r->tick();
     if (r->getTTA() <= 0) {
       vote(*r);
     }
 
 void Process::readMessages()
 {
-  for (vector<Message>::iterator m=messages.begin(); m!=messages.end(); ++m) {
-    m->tick();
+  for (set<Message>::iterator m=messages.begin(); m!=messages.end(); ++m) {
+    //m->tick();
     if (m->getTTA() <= 0) {
       vote(*m);
     }
 }
 
 
+bool Process::isUp()
+{
+  if (up) {
+    return true;
+  }
+  return false;
+}
+
+
+void Process::removeRequest(Request r)
+{
+  requests.erase(requests.find(r));
+}
+
+
+void Process::removeMessage(Message m)
+{
+  messages.erase(messages.find(m));
+}
+
 #include <stdlib.h>
 #include <queue>
 #include <iostream>
-#include <vector>
+#include <set>
 
 class Simulation;
 #include "Request.hpp"
   void pushRequest(Request request);  // "sends" Request to Process, simulates time lag
   void pushMessage(Message m);
   void tick();
-
+  bool isUp();
 
 private:
   Simulation* simulation;
   float prob;  // probability of generating a request
   type::time delta;
   bool up;
-  std::vector<Request> requests;  // set of generated Requests
-  std::vector<Message> messages;  // set of pointers to incoming Messages
+  std::set<Request> requests;  // set of generated Requests
+  std::set<Message> messages;  // set of pointers to incoming Messages
   void vote(Request r);
   void vote(Message m);
   void readRequests();  // Deal with incoming Requests
   void readMessages();  // Deal with incoming Messages
+  void removeRequest(Request r);
+  void removeMessage(Message m);
 
 };
 
   --TTA;
 }
 
+bool Request::hasArrived() const
+{
+    return (TTA <= 0);
+}
+
+bool operator<(const Request &LHS, const Request &RHS)
+{
+    return LHS.ID < RHS.ID;
+}
+
 static const type::time MaximumRequestTTA = 100;
 
 class Request {
+  friend bool operator<(const Request &LHS, const Request &RHS);
 
 public:
   // Processes pass a sequential request ID
   Request(Process *p, type::time ts, type::requestID simID);
-
   void display() const;
-
   type::requestID getID() const;
   Process *getOwner() const;
   type::time getTimestamp() const;
   type::time getTTA() const;
-
+  bool hasArrived() const;
   void tick();
 
 private:
 
 };
 
+
+bool operator<(const Request &LHS, const Request &RHS);
+
 #endif // REQUEST_HPP_
 
 
 using namespace std;
 
-namespace
-{
-    bool message_has_arrived(const Message& Message)
-    {
-        return Message.hasArrived();
-    }
-}
-
 Simulation::Simulation(const unsigned int ProcessCount)
 {
     Time = 0;
+    majority = 2;  // based on knowing there are 3 total nodes
     highestRequest = 1;
     const float FailureProbability = 0.2f;
     const int Delta = 20;
 
 void Simulation::tick()
 {
-    //Update messages
-    for(size_t i = 0; i < ProcessMessageQueues.size(); ++i)
-    {
-        for(size_t j = 0; j < ProcessMessageQueues[i].size(); ++j)
-        {
-            ProcessMessageQueues[i][j].tick();
-        }
-    }
+    // Iterate over Processes
     for(size_t i = 0; i < Processes.size(); ++i)
     {
       Processes[i].tick();
-      for(size_t j = 0; j < ProcessMessageQueues[i].size(); ++j)
-      {
-          if(ProcessMessageQueues[i][j].hasArrived())
-          {
-              Processes[i].pushMessage(ProcessMessageQueues[i][j]);
-          }
-      }
     }
-    //Remove processed messages from message queues
-    for(size_t i = 0; i < Processes.size(); ++i)
-    {
-        vector<Message>::iterator NewEnd = std::remove_if(ProcessMessageQueues[i].begin(), ProcessMessageQueues[i].end(), message_has_arrived);
-        ProcessMessageQueues[i] = vector<Message>(ProcessMessageQueues[i].begin(), NewEnd);
-    }
+
     ++Time;
 }
 
 
 void Simulation::sendMessage(const Message& MessageToSend)
 {
-    ProcessMessageQueues[MessageToSend.getTo()].push_back(MessageToSend);
+
 }
+
+
+bool Simulation::majorityPresent()
+{
+  int upNodes = 0;
+  for(size_t i = 0; i < Processes.size(); ++i) {
+    if (Processes[i].isUp()) {
+      upNodes++;
+    }
+  }
+  if (upNodes >= majority) {
+    return true;
+  }
+  return false;
+}
     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;
+
 };
 
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.