Source

COS 520 Lamport 2 / Request.cpp

/*
 * Request.cpp
 *
 * Author: Joseph Bane
 *
 */

#include <cstdlib> // For rand()
#include <iostream>

#include "Request.hpp"
#include "Process.hpp"

Request::Request(Process *p, type::time ts, type::requestID simID)
{
  ID = simID;
  owner = p;
  timestamp = ts;
  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";
  std::cout << "\tOwner process: " << getOwner()->getID() << "\n";
  std::cout << "\tTimestamp: " << getTimestamp() << "\n";
  std::cout << "\tTTA: " << getTTA() << "\n";
}

type::requestID Request::getID() const
{
  return ID;
}

Process *Request::getOwner() const
{
  return owner;
}

type::time Request::getTimestamp() const
{
  return timestamp;
}

type::time Request::getTTA() const
{
  return TTA;
}

void Request::tick() const
{
  --TTA;
}

bool Request::hasArrived() const
{
    return (TTA <= 0);
}

bool operator<(const Request &LHS, const Request &RHS)
{
    return LHS.ID < RHS.ID;
}