Commits

Joseph Bane committed 8f30c90 Merge

Merged.

  • Participants
  • Parent commits 3b88d86, ba3bff0

Comments (0)

Files changed (8)

 }
 
 
+
+
 
 Request Process::generateRequest()
 {
-  return Request(this, simulation->getTime(), simulation->highestRequest);
-  simulation->highestRequest++;
+  return Request(this, simulation->getTime());
 }
 
 
 {
   if (!up) { return; }  // Do nothing if Process down
 
-  const int timeSince = simulation->getTime() - m.getRequestTime();
-
-  // vote on a received Message
+  // automatically accept on a vote request
   if (int(m.getVote()) == int(Message::vote::null)) {
-    if (timeSince > delta*2) {
-      // Create a new message object to reject
-      simulation->sendMessage( Message(m.getTo(), m.getFrom(), m.getRequestTime(), m.getRequestID(), Message::vote::reject) );
-      cout << "PID: " << id << "  REJECT: Request " << m.getRequestID() << " (" << timeSince << ", " << delta*2 << ")\n";
-      
-    }
-    else {
-      // Create a new message object to accept
-      simulation->sendMessage( Message(m.getTo(), m.getFrom(), m.getRequestTime(), m.getRequestID(), Message::vote::accept) );
-      cout << "PID: " << id << "  ACCEPT: Request " << m.getRequestID() << " (" << timeSince << ", " << delta*2 << ") " << " [" << simulation->status() << "]\n";
-    }
+    // Create a new message object to accept
+    simulation->sendMessage( Message(m.getTo(), m.getFrom(), m.getRequestTime(), m.getRequestID(), Message::vote::accept) );
   }
 
   // read vote received from other Process
   else {
-    if (int(m.getVote()) == int(Message::vote::accept)) {
+    const int timeSince = simulation->getTime() - m.getRequestTime();
+    
+    if ( (int(m.getVote()) == int(Message::vote::accept))
+	 && (timeSince < delta*2) ) {
+      
       // Accept Request
-      cout << "PID: " << id << "  ACCEPT: Request " << m.getRequestID() << " (" << timeSince << ", " << delta*2 << ") " << " [" << simulation->status() << "]\n";
-    }
-    else {
-      // Reject Request-- not enough votes
-      cout << "PID: " << id << "  REJECT: Request " << m.getRequestID() << " {" << m.getVote() << "}\n";
+      cout << "PID: " << id << "  ACCEPT: Request " << m.getRequestID() << " (" << timeSince << ", " << delta*2 << ")\n";
+      set<Request>::iterator r = requestByID(m.getRequestID());
+      if (timeSince > delta*2) {
+	cout << "ERORR: Time too long: " << timeSince << endl;
+	exit(100);
+      }
+      
+      else {
+	// Reject Request-- not enough votes
+	cout << "PID: " << id << "  REJECT: Request " << m.getRequestID() << " {" << m.getVote() << "}\n";
+      }
     }
   }
 }
 }
 
 
-bool Process::isUp()
+bool Process::isUp() const
 {
   return up;
 }
   messages.erase(messages.find(m));
 }
 
+
 void Process::goDown()
 {
     messages.clear();
     requests.clear();
 }
+
+
+set<Request>::iterator Process::requestByID(type::requestID id)
+{
+  for (set<Request>::iterator r=requests.begin(); r!=requests.end(); ++r) {
+    if (r->getID() == id) {
+      return r;
+    }
+  }
+  return requests.end();
+}
   void pushRequest(Request request);  // "sends" Request to Process, simulates time lag
   void pushMessage(Message m);
   void tick();
-  bool isUp();
+  bool isUp() const;
 
 private:
   Simulation* simulation;
   void removeRequest(Request r);
   void removeMessage(Message m);
   void goDown();
+  std::set<Request>::iterator requestByID(type::requestID id);
 
 };
 

File Reliable Lamport Processes.vcproj

 				UsePrecompiledHeader="0"
 				WarningLevel="3"
 				DebugInformationFormat="4"
+				EnablePREfast="true"
 			/>
 			<Tool
 				Name="VCManagedResourceCompilerTool"
 #include "Request.hpp"
 #include "Process.hpp"
 
-Request::Request(Process *p, type::time ts, type::requestID simID)
+unsigned int Request::NextRequestID = 0;
+
+Request::Request(Process *p, type::time ts)
 {
-  ID = simID;
+  ID = NextRequestID++;
   owner = p;
   timestamp = ts;
   TTA = (rand() % MaximumRequestTTA) + 1;
+  accepted = false;
 }
 
-Request::Request(Process *p, type::time ts, type::requestID simID, type::time t)
+Request::Request(Process *p, type::time ts, type::time t)
 {
-  ID = simID;
+  ID = NextRequestID++;
   owner = p;
   timestamp = ts;
   TTA = t;
+  accepted = false;
 }
 
 void Request::display() const
     return LHS.ID < RHS.ID;
 }
 
+
+bool Request::correct(type::time currentTime, bool majority, const int delta)
+{
+  if (timestamp - currentTime > delta) {
+    return false;
+  }
+
+  return true;
+}
+
 
 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);
+  Request(Process *p, type::time ts);
+  Request(Process *p, type::time ts, type::time TTA);
   void display() const;
   type::requestID getID() const;
   Process *getOwner() const;
   type::time getTTA() const;
   bool hasArrived() const;
   void tick() const;
+  bool accepted;
+  bool correct(type::time currentTime, bool majority, const int delta);
 
 private:
   type::requestID ID;
   Process *owner;
   type::time timestamp;
   mutable type::time TTA;
+  static unsigned int NextRequestID;
 
 };
 

File Simulation.cpp

File contents unchanged.

File Simulation.hpp

     void tick();  // Updates the simulation by one timestep
     void sendMessage(const Message& MessageToSend);
     type::time getTime();
-    type::requestID highestRequest;
     void display();
-    std::string status();
+    std::string status() const;
+    bool majorityPresent() const;
 
 private:
     type::time Time;
     std::vector<Process> Processes;
-    bool majorityPresent();
     int majority;
 
 };