Commit 84da2a61 authored by Jonathon Duerig's avatar Jonathon Duerig

magent is now compilable. Added all the extra files I've added which I forgot to do before.

parent 139af9f6
// CircularTraffic.cc
#include "lib.h"
#include "log.h"
#include "CircularTraffic.h"
#include "Command.h"
#include "ConnectionModel.h"
using namespace std;
CircularTraffic::CircularTraffic()
{
begin = 0;
usedCount = 0;
current = 0;
writes.resize(DEFAULT_SIZE);
}
CircularTraffic::~CircularTraffic()
{
}
auto_ptr<TrafficModel> CircularTraffic::clone(void)
{
auto_ptr<CircularTraffic> result;
result->begin = begin;
result->usedCount = usedCount;
result->current = current;
result->writes = writes;
auto_ptr<TrafficModel> model(result.release());
return model;
}
Time CircularTraffic::addWrite(TrafficWriteCommand const & newWrite,
Time const & deadline)
{
if (usedCount < static_cast<int>(writes.size()))
{
writes[usedCount] = newWrite;
++usedCount;
}
else
{
writes[begin] = newWrite;
begin = (begin + 1) % writes.size();
}
if (deadline == Time())
{
return getCurrentTime() + writes[current].delta;
}
else
{
return Time();
}
}
WriteResult CircularTraffic::writeToPeer(ConnectionModel * peer,
Time const & previousTime)
{
if (usedCount > 0)
{
WriteResult result;
current = (current + 1) % usedCount;
peer->writeMessage(writes[current].size, result);
result.nextWrite = previousTime + writes[current].delta;
return result;
}
else
{
logWrite(ERROR, "writeToPeer() called without addWrite() being "
"called first. This should be impossible.");
WriteResult result;
result.isConnected = peer->isConnected();
result.bufferFull = false;
result.nextWrite = Time();
return result;
}
}
// CircularTraffic.h
#ifndef CIRCULAR_TRAFFIC_H_STUB_2
#define CIRCULAR_TRAFFIC_H_STUB_2
#include "TrafficModel.h"
class CircularTraffic : public TrafficModel
{
public:
enum { DEFAULT_SIZE = 20 };
public:
CircularTraffic();
virtual ~CircularTraffic();
virtual std::auto_ptr<TrafficModel> clone(void);
virtual Time addWrite(TrafficWriteCommand const & newWrite,
Time const & deadline);
virtual WriteResult writeToPeer(ConnectionModel * peer,
Time const & previousTime);
private:
int begin;
int usedCount;
int current;
std::vector<TrafficWriteCommand> writes;
};
#endif
......@@ -6,6 +6,7 @@
#include "Connection.h"
#include "ConnectionModel.h"
#include "TrafficModel.h"
#include "CircularTraffic.h"
using namespace std;
......@@ -30,6 +31,8 @@ void NewConnectionCommand::runConnect(Connection *,
void TrafficModelCommand::runConnect(Connection * conn,
std::multimap<Time, Connection *> &)
{
std::auto_ptr<TrafficModel> model(new CircularTraffic());
conn->setTraffic(model);
}
//-----------------------
......
......@@ -22,6 +22,7 @@ protected:
virtual void runConnect(Connection * conn,
std::multimap<Time, Connection *> & schedule)=0;
public:
// We use a key here and look up the connection only on a run()
// because some commands delete a connection and we don't want later
// commands to keep the reference around.
......@@ -49,6 +50,7 @@ class ConnectionModelCommand : public Command
protected:
virtual void runConnect(Connection * conn,
std::multimap<Time, Connection *> &);
public:
int type;
int value;
};
......@@ -60,7 +62,7 @@ protected:
std::multimap<Time, Connection *> &);
public:
int type;
std::vector<char> parameters;
// std::vector<char> parameters;
};
class ConnectCommand : public Command
......
......@@ -8,6 +8,9 @@
#ifndef COMMAND_OUTPUT_H_STUB_2
#define COMMAND_OUTPUT_H_STUB_2
#include "log.h"
#include "saveload.h"
class CommandOutput
{
public:
......@@ -15,28 +18,38 @@ public:
{
SENDING_MESSAGE = 0,
DISCARDING_MESSAGE
}
};
enum PathDirection
{
FORWARD_PATH,
BACKWARD_PATH
};
public:
virtual ~CommandOutput() {}
void eventMessage(std::string const & message, Order const & key,
char direction)
PathDirection dir=FORWARD_PATH)
{
if (message.size() <= 0xffff && message.size() > 0)
{
Header prefix;
prefix.type = EVENT_TO_MONITOR;
if (dir == FORWARD_PATH)
{
prefix.type = EVENT_FORWARD_PATH;
}
else
{
prefix.type = EVENT_BACKWARD_PATH;
}
prefix.size = message.size();
prefix.key = key;
char headerBuffer[Header::headerSize];
saveHeader(headerBuffer, prefix);
int result = startMessage(Header::headerSize + sizeof(direction)
+ message.size());
int result = startMessage(Header::headerSize + message.size());
if (result == SENDING_MESSAGE)
{
writeMessage(headerBuffer, Header::headerSize);
writeMessage(&direction, sizeof(direction));
writeMessage(message.c_str(), message.size());
finishMessage();
endMessage();
}
}
else
......
......@@ -10,7 +10,7 @@ class ConnectionModel
public:
virtual ~ConnectionModel() {}
virtual std::auto_ptr<ConnectionModel> clone(void)=0;
virtual void connect(void)=0;
virtual void connect(Order & planet)=0;
virtual void addParam(ConnectionModelCommand const & param)=0;
// Returns the number of bytes actually written or -1 if there was
// an error. Errno is not preserved.
......@@ -26,17 +26,18 @@ public:
{
return std::auto_ptr<ConnectionModel>(new ConnectionModelNull());
}
virtual void connect(void) {}
virtual void connect(Order &) {}
virtual void addParam(ConnectionModelCommand const &) {}
static void init(void) {}
static void addNewPeer(fd_set *) {}
static void readFromPeers(fd_set *) {}
static void packetCapture(fd_set *) {}
virtual void writeMessage(int, WriteResult & result)
virtual int writeMessage(int, WriteResult & result)
{
result.isConnected = false;
result.bufferFull = false;
return 0;
}
virtual bool isConnected(void)
{
......
// DelaySensor.cc
#include "lib.h"
#include "DelaySensor.h"
#include "PacketSensor.h"
#include "Time.h"
using namespace std;
DelaySensor::DelaySensor(PacketSensor * newPacketHistory)
{
lastDelay = 0;
packetHistory = newPacketHistory;
}
int DelaySensor::getLastDelay(void) const
{
return lastDelay;
}
void DelaySensor::localSend(PacketInfo *)
{
}
void DelaySensor::localAck(PacketInfo * packet)
{
Time diff = packet->packetTime - packetHistory->getAckedSendTime();
lastDelay = diff.toMilliseconds();
}
// DelaySensor.h
#ifndef DELAY_SENSOR_H_STUB_2
#define DELAY_SENSOR_H_STUB_2
#include "Sensor.h"
class PacketSensor;
class DelaySensor : public Sensor
{
public:
DelaySensor(PacketSensor * newPacketHistory);
int getLastDelay(void) const;
protected:
virtual void localSend(PacketInfo * packet);
virtual void localAck(PacketInfo * packet);
private:
int lastDelay;
PacketSensor * packetHistory;
};
#endif
// DirectInput.cc
#include "lib.h"
#include "log.h"
#include "DirectInput.h"
using namespace std;
DirectInput::DirectInput()
: state(ACCEPTING)
, monitorAccept(-1)
, monitorSocket(-1)
, index(0)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
logWrite(ERROR, "Unable to generate a command accept socket. "
"No incoming command connections will ever be accepted: %s",
strerror(errno));
}
else
{
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(global::monitorServerPort);
address.sin_addr.s_addr = INADDR_ANY;
int error = bind(sockfd, reinterpret_cast<struct sockaddr *>(&address),
sizeof(struct sockaddr));
if (error == -1)
{
logWrite(ERROR, "Unable to bind a command accept socket. "
"No incoming command connections will ever be accepted: %s",
strerror(errno));
close(sockfd);
}
else
{
setDescriptor(sockfd);
monitorAccept = sockfd;
}
}
}
DirectInput::~DirectInput()
{
if (monitorAccept != -1)
{
close(monitorAccept);
}
if (monitorSocket != -1)
{
close(monitorSocket);
}
}
void DirectInput::nextCommand(fd_set * readable)
{
if (state == ACCEPTING && monitorAccept != -1
&& FD_ISSET(monitorAccept, readable))
{
}
if (monitorSocket == -1)
{
state = ACCEPTING;
}
if (state == HEADER && FD_ISSET(monitorSocket, readable))
{
int error = recv(monitorSocket, headerBuffer + index,
Header::headerSize - index, 0);
if (error == Header::headerSize - index)
{
loadHeader(headerBuffer, &commandHeader);
index = 0;
state = BODY;
}
else if (error > 0)
{
index += error;
}
else if (error == 0)
{
disconnect();
}
else if (error == -1)
{
logWrite(EXCEPTION, "Failed read on monitorSocket state HEADER: %s",
strerror(errno));
}
}
if (state == BODY && FD_ISSET(monitorSocket, readable))
{
int error = recv(monitorSocket, bodyBuffer + index,
commandHeader.size - index, 0);
if (error == commandHeader.size - index)
{
currentCommand = loadCommand(&commandHeader, bodyBuffer);
index = 0;
state = HEADER;
}
else if (error > 0)
{
index += error;
}
else if (error == 0)
{
disconnect();
}
else if (error == -1)
{
logWrite(EXCEPTION, "Failed read on monitorSocket state BODY: %s",
strerror(errno));
}
}
}
int DirectInput::getMonitorSocket(void)
{
return monitorSocket;
}
void DirectInput::disconnect(void)
{
if (monitorSocket != -1)
{
clearDescriptor(monitorSocket);
close(monitorSocket);
}
index = 0;
state = ACCEPTING;
}
// DirectInput.h
#ifndef DIRECT_INPUT_H_STUB_2
#define DIRECT_INPUT_H_STUB_2
#include "CommandInput.h"
#include "saveload.h"
class DirectInput : public CommandInput
{
public:
DirectInput();
virtual ~DirectInput();
virtual void nextCommand(fd_set * readable);
virtual int getMonitorSocket(void);
virtual void disconnect(void);
private:
enum MonitorState
{
ACCEPTING,
HEADER,
BODY
};
private:
MonitorState state;
int monitorAccept;
int monitorSocket;
int index;
char headerBuffer[Header::headerSize];
Header commandHeader;
enum { bodyBufferSize = 0xffff };
char bodyBuffer[bodyBufferSize];
};
#endif
This diff is collapsed.
......@@ -5,7 +5,6 @@
#include "ConnectionModel.h"
enum ConnectionState
{
DISCONNECTED,
......@@ -38,10 +37,6 @@ public:
static void addNewPeer(fd_set * readable);
static void readFromPeers(fd_set * readable);
static void packetCapture(fd_set * readable);
private:
static bool changeSocket(int sockfd, int level, int optname, int value,
std::string optstring)
};
#endif
all: magent
magent: Command.o Connection.o Decayer.o KernelTcp.o SensorList.o Time.o log.o main.o
g++ -I. -g -Wall Command.o Connection.o Decayer.o KernelTcp.o SensorList.o Time.o log.o main.o -lm -lpcap -o magent
magent: CircularTraffic.o Command.o Connection.o Decayer.o DelaySensor.o DirectInput.o KernelTcp.o MaxDelaySensor.o MinDelaySensor.o PacketSensor.o Sensor.o SensorList.o ThroughputSensor.o Time.o TrivialCommandOutput.o log.o main.o saveload.o
g++ -I. -g -Wall CircularTraffic.o Command.o Connection.o Decayer.o DelaySensor.o DirectInput.o KernelTcp.o MaxDelaySensor.o MinDelaySensor.o PacketSensor.o Sensor.o SensorList.o ThroughputSensor.o Time.o TrivialCommandOutput.o log.o main.o saveload.o -lm -lpcap -o magent
Command.o: Command.cc lib.h Command.h Sensor.h Connection.h ConnectionModel.h TrafficModel.h
CircularTraffic.o: CircularTraffic.cc lib.h log.h TrafficModel.h CircularTraffic.h Command.h ConnectionModel.h
g++ -I. -g -Wall -c CircularTraffic.cc
Command.o: Command.cc lib.h Command.h Sensor.h Connection.h ConnectionModel.h TrafficModel.h CircularTraffic.h
g++ -I. -g -Wall -c Command.cc
Connection.o: Connection.cc lib.h log.h Connection.h Time.h ConnectionModel.h TrafficModel.h Sensor.h
g++ -I. -g -Wall -c Connection.cc
Decayer.o: Decayer.cc lib.h Decayer.h
g++ -I. -g -Wall -c Decayer.cc
KernelTcp.o: KernelTcp.cc lib.h log.h KernelTcp.h
DelaySensor.o: DelaySensor.cc lib.h Sensor.h DelaySensor.h PacketSensor.h Time.h
g++ -I. -g -Wall -c DelaySensor.cc
DirectInput.o: DirectInput.cc lib.h log.h CommandInput.h saveload.h DirectInput.h
g++ -I. -g -Wall -c DirectInput.cc
KernelTcp.o: KernelTcp.cc lib.h log.h KernelTcp.h Command.h
g++ -I. -g -Wall -c KernelTcp.cc
SensorList.o: SensorList.cc lib.h log.h SensorList.h Sensor.h Command.h
MaxDelaySensor.o: MaxDelaySensor.cc lib.h Sensor.h Decayer.h MaxDelaySensor.h DelaySensor.h log.h saveload.h CommandOutput.h
g++ -I. -g -Wall -c MaxDelaySensor.cc
MinDelaySensor.o: MinDelaySensor.cc lib.h Sensor.h Decayer.h MinDelaySensor.h DelaySensor.h log.h saveload.h CommandOutput.h
g++ -I. -g -Wall -c MinDelaySensor.cc
PacketSensor.o: PacketSensor.cc lib.h Sensor.h PacketSensor.h
g++ -I. -g -Wall -c PacketSensor.cc
Sensor.o: Sensor.cc lib.h Sensor.h
g++ -I. -g -Wall -c Sensor.cc
SensorList.o: SensorList.cc lib.h log.h SensorList.h Sensor.h Command.h PacketSensor.h DelaySensor.h MinDelaySensor.h MaxDelaySensor.h ThroughputSensor.h
g++ -I. -g -Wall -c SensorList.cc
ThroughputSensor.o: ThroughputSensor.cc lib.h Sensor.h ThroughputSensor.h PacketSensor.h
g++ -I. -g -Wall -c ThroughputSensor.cc
Time.o: Time.cc lib.h Time.h
g++ -I. -g -Wall -c Time.cc
TrivialCommandOutput.o: TrivialCommandOutput.cc lib.h log.h saveload.h CommandOutput.h TrivialCommandOutput.h CommandInput.h
g++ -I. -g -Wall -c TrivialCommandOutput.cc
log.o: log.cc lib.h log.h
g++ -I. -g -Wall -c log.cc
main.o: main.cc lib.h log.h CommandInput.h CommandOutput.h Command.h Time.h Connection.h Sensor.h TrafficModel.h KernelTcp.h
main.o: main.cc lib.h log.h CommandInput.h CommandOutput.h Command.h Time.h Connection.h Sensor.h TrafficModel.h KernelTcp.h saveload.h
g++ -I. -g -Wall -c main.cc
saveload.o: saveload.cc lib.h log.h saveload.h Command.h
g++ -I. -g -Wall -c saveload.cc
clean:
rm *.o magent
// MaxDelaySensor.cc
#include "lib.h"
#include "MaxDelaySensor.h"
#include "DelaySensor.h"
#include "CommandOutput.h"
using namespace std;
MaxDelaySensor::MaxDelaySensor(DelaySensor * newDelay)
: maximum(0, 0.01)
{
delay = newDelay;
}
void MaxDelaySensor::localSend(PacketInfo *)
{
}
void MaxDelaySensor::localAck(PacketInfo * packet)
{
int current = delay->getLastDelay();
if (current > maximum && current != 0)
{
ostringstream buffer;
buffer << "MAXINQ=" << current;
maximum.reset(current);
global::output->eventMessage(buffer.str(), packet->elab);
}
else
{
maximum.decay();
}
}
// MaxDelaySensor.h
#ifndef MAX_DELAY_SENSOR_H_STUB_2
#define MAX_DELAY_SENSOR_H_STUB_2
#include "Sensor.h"
#include "Decayer.h"
class DelaySensor;
class MaxDelaySensor : public Sensor
{
public:
MaxDelaySensor(DelaySensor * newDelay);
protected:
virtual void localSend(PacketInfo * packet);
virtual void localAck(PacketInfo * packet);
private:
Decayer maximum;
DelaySensor * delay;
};
#endif
// MinDelaySensor.cc
#include "lib.h"
#include "MinDelaySensor.h"
#include "DelaySensor.h"
#include "CommandOutput.h"
using namespace std;
MinDelaySensor::MinDelaySensor(DelaySensor * newDelay)
: minimum(1000000, -0.01)
{
delay = newDelay;
}
void MinDelaySensor::localSend(PacketInfo *)
{
}
void MinDelaySensor::localAck(PacketInfo * packet)
{
int current = delay->getLastDelay();
if (current < minimum && current != 0)
{
ostringstream buffer;
buffer << "delay=" << current/2;
minimum.reset(current);
global::output->eventMessage(buffer.str(), packet->elab,
CommandOutput::FORWARD_PATH);
global::output->eventMessage(buffer.str(), packet->elab,
CommandOutput::BACKWARD_PATH);
}
else
{
minimum.decay();
}
}
// MinDelaySensor.h
// This sensor keeps track of the lowest delay seen recently. The
// lowest value it has decays slowly as new measurements come in.
#ifndef MIN_DELAY_SENSOR_H_STUB_2
#define MIN_DELAY_SENSOR_H_STUB_2
#include "Sensor.h"
#include "Decayer.h"
class DelaySensor;
class MinDelaySensor : public Sensor
{
public:
MinDelaySensor(DelaySensor * newDelay);
protected:
virtual void localSend(PacketInfo * packet);
virtual void localAck(PacketInfo * packet);
private:
Decayer minimum;
DelaySensor * delay;
};
#endif
// PacketSensor.cc
#include "lib.h"
#include "PacketSensor.h"
using namespace std;
PacketSensor::PacketSensor()
{
ackedSize = 0;
ackedSendTime = Time();
}
int PacketSensor::getAckedSize(void) const
{
if (ackedSendTime == Time())
{
logWrite(ERROR, "PacketSensor::getAckedSize() called before localAck()");
}
return ackedSize;
}
Time const & PacketSensor::getAckedSendTime(void) const
{
if (ackedSendTime == Time())
{
logWrite(ERROR, "PacketSensor::getAckedSendTime() called before"
" localAck()");
}
return ackedSendTime;
}
void PacketSensor::localSend(PacketInfo * packet)
{
unsigned int startSequence = ntohl(packet->tcp->seq);
if (globalSequence.inSequenceBlock(startSequence))
{
list<SentPacket>::iterator pos = unacked.begin();
list<SentPacket>::iterator limit = unacked.end();
bool done = false;
for (; pos != limit && !done; ++pos)
{
if (pos->inSequenceBlock(startSequence))
{
pos->timestamp = packet->packetTime;
done = true;
}
}
}
else
{
SentPacket record;
record.seqStart = ntohl(startSequence);
unsigned int sequenceLength = packet->packetLength -
sizeof(struct ether_header) - IP_HL(packet->ip)*4 -
sizeof(struct tcphdr);
record.seqEnd = record.seqStart + sequenceLength;
record.totalLength = packet->packetLength;
record.timestamp = packet->packetTime;
globalSequence.seqEnd = record.seqEnd;
if (unacked.empty())