#include "MessageExecutor.h"

MessageExecutor::MessageExecutor() {
}

void MessageExecutor::executeMessage(Message * m) {
	TreeNode * node = m->getTree();
	try {
		if (node->existChild("request")) {
			string value = node->getChild("request")->getValue();
			if (value == "update") {
				ConnectorSingleton::getConnector()->sendUpdate();
			}
		} else if (node->existChild("patch")) {
			string from = node->getChild("patch")->getChild("from")->getValue();
			string to   = node->getChild("patch")->getChild("to")->getValue();
			int fhFrom = atoi(from.c_str());
			int fhTo   = atoi(to.c_str());
			ConnectorSingleton::getConnector()->patch(fhFrom,fhTo);
		} else if (node->existChild("unPatch")) {
			string from = node->getChild("unPatch")->getChild("from")->getValue();
			string to   = node->getChild("unPatch")->getChild("to")->getValue();
			int fhFrom = atoi(from.c_str());
			int fhTo   = atoi(to.c_str());
			ConnectorSingleton::getConnector()->unPatch(fhFrom,fhTo);
		} else if (node->existChild("disconnect")) {
			string con = node->getChild("disconnect")->getChild("connection")->getValue();
			ConnectorSingleton::getConnector()->disconnect(atoi(con.c_str()));
		} else if (node->existChild("changedSince")) {
			ConnectorSingleton::getConnector()->lastChange();
		} else if (node->existChild("shutdown")) {
			ConnectorSingleton::getConnector()->shutDown();
		}
	} catch (string * err) {
		
	}
}
