Skip to content

Commit

Permalink
Support node-transmit events
Browse files Browse the repository at this point in the history
See: #40
  • Loading branch information
bpe2 committed Feb 8, 2022
1 parent 2a8f02f commit a0899ea
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 9 deletions.
52 changes: 52 additions & 0 deletions parser/handler/JsonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ void JsonHandler::do_parse(JsonHandler::Section section, const util::json::JsonO
parseNodeOrientationEvent(object);
else if (type == "node-color")
parseNodeColorChangeEvent(object);
else if (type == "node-transmit")
parseTransmitEvent(object);
else if (type == "decoration-position")
parseDecorationMoveEvent(object);
else if (type == "decoration-orientation")
Expand Down Expand Up @@ -481,6 +483,33 @@ void JsonHandler::parseMoveEvent(const util::json::JsonObject &object) {
updateLocationBounds(event.targetPosition);

updateEndTime(event.time);
processEndTransmits(event.time);
fileParser.sceneEvents.emplace_back(event);
}

void JsonHandler::parseTransmitEvent(const util::json::JsonObject &object) {
requiredFields(object, {"id", "milliseconds", "duration", "target-size"});
parser::TransmitEvent event;

event.nodeId = object["id"].get<int>();
event.time = object["milliseconds"].get<double>();
event.duration = object["duration"].get<double>();
event.targetSize = object["target-size"].get<double>();
event.color = colorFromObject(object["color"].object());

updateEndTime(event.time);
processEndTransmits(event.time);

// End any previous transmits by this Node
// Even if they're incomplete
const auto &transmittingIter = transmittingNodes.find(event.nodeId);
if (transmittingIter != transmittingNodes.end() && transmittingIter->second.has_value()) {
parser::TransmitEndEvent endEvent;
endEvent.time = event.time;
endEvent.startEvent = transmittingIter->second.value();
fileParser.sceneEvents.emplace_back(endEvent);
}
transmittingNodes[event.nodeId] = event;
fileParser.sceneEvents.emplace_back(event);
}

Expand All @@ -497,6 +526,7 @@ void JsonHandler::parseDecorationMoveEvent(const util::json::JsonObject &object)
updateLocationBounds(event.targetPosition);

updateEndTime(event.time);
processEndTransmits(event.time);
fileParser.sceneEvents.emplace_back(event);
}

Expand All @@ -511,6 +541,7 @@ void JsonHandler::parseNodeOrientationEvent(const util::json::JsonObject &object
event.targetOrientation[2] = object["z"].get<double>();

updateEndTime(event.time);
processEndTransmits(event.time);
fileParser.sceneEvents.emplace_back(event);
}

Expand All @@ -525,6 +556,7 @@ void JsonHandler::parseDecorationOrientationEvent(const util::json::JsonObject &
event.targetOrientation[2] = object["z"].get<double>();

updateEndTime(event.time);
processEndTransmits(event.time);
fileParser.sceneEvents.emplace_back(event);
}

Expand All @@ -547,6 +579,7 @@ void JsonHandler::parseNodeColorChangeEvent(const util::json::JsonObject &object
event.targetColor = colorFromObject(object["color"].object());

updateEndTime(event.time);
processEndTransmits(event.time);
fileParser.sceneEvents.emplace_back(event);
}

Expand Down Expand Up @@ -736,6 +769,25 @@ void JsonHandler::updateEndTime(double milliseconds) {
fileParser.globalConfiguration.endTime = milliseconds;
}

void JsonHandler::processEndTransmits(double milliseconds) {
for (auto &[nodeId, transmitEvent] : transmittingNodes) {
if (!transmitEvent.has_value())
return;

const auto endTransmitTime = transmitEvent.value().time + transmitEvent.value().duration;
if (milliseconds < endTransmitTime)
return;

parser::TransmitEndEvent endEvent;
endEvent.time = milliseconds;
endEvent.startEvent = transmitEvent.value();
endEvent.nodeId = endEvent.startEvent.nodeId;
fileParser.sceneEvents.emplace_back(endEvent);

transmitEvent.reset();
}
}

JsonHandler::JsonHandler(parser::FileParser &parser) : fileParser(parser) {
}

Expand Down
25 changes: 25 additions & 0 deletions parser/handler/JsonHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <rapidjson/reader.h>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

class JsonHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JsonHandler> {
Expand All @@ -52,6 +53,7 @@ class JsonHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JsonH
enum class Section { None, Areas, Buildings, Configuration, Decorations, Events, Links, Nodes, Series, Streams };

parser::FileParser &fileParser;
std::unordered_map<unsigned int, std::optional<parser::TransmitEvent>> transmittingNodes;

/**
* Parse a section from a string.
Expand Down Expand Up @@ -194,6 +196,14 @@ class JsonHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JsonH
*/
void parseMoveEvent(const util::json::JsonObject &object);

/**
* Parse and emplace a transmit event
*
* @param object
* The object from the 'events' section with the 'node-transmit' type
*/
void parseTransmitEvent(const util::json::JsonObject &object);

/**
* Parse and emplace a DecorationMoveEvent
*
Expand Down Expand Up @@ -315,6 +325,21 @@ class JsonHandler : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, JsonH
*/
void updateEndTime(double milliseconds);

/**
* Run through the list of transmitting nodes
* and insert `TransmitEndEvent`s for those
* that are done transmitting before `milliseconds`.
*
* Potentially modifies `fileParser.sceneEvents`
*
* Should be called while processing each scene event,
* but before appending the event.
*
* @param milliseconds
* The time of the current event being processed.
*/
void processEndTransmits(double milliseconds);

public:
explicit JsonHandler(parser::FileParser &parser);

Expand Down
66 changes: 62 additions & 4 deletions parser/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,63 @@ struct MoveEvent {
Ns3Coordinate targetPosition;
};

/**
* Event that indicates a Node has begun transmitting
*/
struct TransmitEvent {
/**
* The simulation time (in milliseconds)
* for when the event should be run
*/
double time = 0.0;

/**
* The Node that triggered the event
*/
uint32_t nodeId = 0;

/**
* How long the transmission sphere should
* expand, in milliseconds
*/
double duration = 50.0;

/**
* The size the transmission sphere should grow to,
* by the time `time + duration` milliseconds have
* passed. In ns-3 units
*/
double targetSize = 2.0;

/**
* The color to use as the base color of
* the transmission bubble
*/
Ns3Color3 color;
};

/**
* Event inserted by the parser when a transmission is supposed to end
*/
struct TransmitEndEvent {
/**
* The simulation time (in milliseconds)
* for when the event should be run
*/
double time = 0.0;

/**
* The Node that triggered the event.
* Included for consistency with other events
*/
uint32_t nodeId = 0;

/**
* The event that started the transmission
*/
TransmitEvent startEvent;
};

/**
* Event that changes the position of the indicated Decoration
*/
Expand Down Expand Up @@ -439,14 +496,15 @@ struct StreamAppendEvent {
/**
* Variant defined for every event model
*/
using Event = std::variant<MoveEvent, DecorationMoveEvent, NodeOrientationChangeEvent, DecorationOrientationChangeEvent,
XYSeriesAddValue, XYSeriesAddValues, XYSeriesClear, StreamAppendEvent>;
using Event = std::variant<MoveEvent, TransmitEvent, DecorationMoveEvent, NodeOrientationChangeEvent,
DecorationOrientationChangeEvent, XYSeriesAddValue, XYSeriesAddValues, XYSeriesClear,
StreamAppendEvent>;

/**
* Events which affect the rendered scene
*/
using SceneEvent = std::variant<MoveEvent, NodeOrientationChangeEvent, NodeColorChangeEvent, DecorationMoveEvent,
DecorationOrientationChangeEvent>;
using SceneEvent = std::variant<MoveEvent, TransmitEvent, TransmitEndEvent, NodeOrientationChangeEvent,
NodeColorChangeEvent, DecorationMoveEvent, DecorationOrientationChangeEvent>;

/**
* Event types specific to the charts model
Expand Down
37 changes: 37 additions & 0 deletions src/group/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ glm::vec3 Node::getCenter() const {
return position;
}

const Node::TransmitInfo &Node::getTransmitInfo() const {
return transmitInfo;
}

void Node::addWiredLink(WiredLink *link) {
wiredLinks.emplace_back(link);
link->notifyNodeMoved(ns3Node.id, getCenter());
Expand Down Expand Up @@ -155,6 +159,28 @@ void Node::handle(const undo::MoveEvent &e) {
}
}

undo::TransmitEvent Node::handle(const parser::TransmitEvent &e) {
transmitInfo.isTransmitting = true;
transmitInfo.startTime = e.time;
transmitInfo.targetSize = e.targetSize;
transmitInfo.duration = e.duration;
transmitInfo.color = toRenderColor(e.color);

undo::TransmitEvent undo;
undo.stopTime = transmitInfo.startTime + transmitInfo.duration;
undo.event = e;
return undo;
}

undo::TransmitEndEvent Node::handle(const parser::TransmitEndEvent &e) {
transmitInfo.isTransmitting = false;

undo::TransmitEndEvent undo;
undo.event = e;

return undo;
}

void Node::handle(const undo::NodeOrientationChangeEvent &e) {
model.setRotate(e.orientation[0], e.orientation[2], e.orientation[1]);
}
Expand All @@ -173,4 +199,15 @@ void Node::handle(const undo::NodeColorChangeEvent &e) {
}
}

void Node::handle(const undo::TransmitEvent &e) {
transmitInfo.startTime = e.event.time;
transmitInfo.duration = e.event.duration;
}

void Node::handle(const undo::TransmitEndEvent &e) {
const auto &startEvent = e.event.startEvent;
transmitInfo.startTime = startEvent.time;
transmitInfo.duration = startEvent.duration;
}

} // namespace netsimulyzer
17 changes: 17 additions & 0 deletions src/group/node/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,47 @@
#include "src/group/link/WiredLink.h"
#include <glm/glm.hpp>
#include <model.h>
#include <optional>
#include <vector>

namespace netsimulyzer {

class Node {
public:
struct TransmitInfo {
bool isTransmitting{false};
double startTime;
double targetSize{2.0};
double duration{50.0};
glm::vec3 color;
};

private:
Model model;
parser::Node ns3Node;
glm::vec3 offset;
std::vector<WiredLink *> wiredLinks;
TransmitInfo transmitInfo;

public:
Node(const Model &model, parser::Node ns3Node);
[[nodiscard]] const Model &getModel() const;
[[nodiscard]] const parser::Node &getNs3Model() const;
[[nodiscard]] bool visible() const;
[[nodiscard]] glm::vec3 getCenter() const;
[[nodiscard]] const TransmitInfo &getTransmitInfo() const;

void addWiredLink(WiredLink *link);

undo::MoveEvent handle(const parser::MoveEvent &e);
undo::TransmitEvent handle(const parser::TransmitEvent &e);
undo::TransmitEndEvent handle(const parser::TransmitEndEvent &e);
undo::NodeOrientationChangeEvent handle(const parser::NodeOrientationChangeEvent &e);
undo::NodeColorChangeEvent handle(const parser::NodeColorChangeEvent &e);

void handle(const undo::MoveEvent &e);
void handle(const undo::TransmitEvent &e);
void handle(const undo::TransmitEndEvent &e);
void handle(const undo::NodeOrientationChangeEvent &e);
void handle(const undo::NodeColorChangeEvent &e);
};
Expand Down
17 changes: 15 additions & 2 deletions src/util/undo-events.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ struct MoveEvent {
parser::MoveEvent event;
};

/**
* An event which undoes a `parser::TransmitEvent`
*/
struct TransmitEvent {
double stopTime{0.0};
parser::TransmitEvent event;
};

struct TransmitEndEvent {
parser::TransmitEndEvent event;
};

/**
* An event which undoes a `parser::DecorationMoveEvent`
*/
Expand Down Expand Up @@ -181,8 +193,9 @@ struct StreamAppendEvent {
parser::StreamAppendEvent event;
};

using SceneUndoEvent = std::variant<MoveEvent, DecorationMoveEvent, NodeOrientationChangeEvent, NodeColorChangeEvent,
DecorationOrientationChangeEvent, XYSeriesAddValue, StreamAppendEvent>;
using SceneUndoEvent =
std::variant<MoveEvent, TransmitEvent, TransmitEndEvent, DecorationMoveEvent, NodeOrientationChangeEvent,
NodeColorChangeEvent, DecorationOrientationChangeEvent, XYSeriesAddValue, StreamAppendEvent>;

using ChartUndoEvent = std::variant<XYSeriesAddValue, XYSeriesAddValues, XYSeriesClear, CategorySeriesAddValue>;

Expand Down
Loading

0 comments on commit a0899ea

Please sign in to comment.