Sleds/cppcore/cppcore-test/concreteobserver.cpp

203 lines
6.7 KiB
C++
Raw Normal View History

2025-03-13 21:28:38 +00:00
// Copyright (c) 2014, IOnU Security Inc.
#include "concreteobserver.h"
#include <iostream>
#include "../../cppjson/jsonobject.h"
#include "../cppcoreobjects/cloudguardurn.h"
namespace
{
const char *GetError(int nError)
{
switch (nError)
{
case sequencelogic::PeerObserver::ERROR_CODES::ERR_DUPLICATE_DEVICE:
return "Duplicate Device connected to K2.";
break;
case sequencelogic::PeerObserver::ERROR_CODES::ERR_NO_NETWORK_SVC:
return "No nerwork service object provided.";
break;
default:
return "UNKNOWN";
break;
}
return "ERROR!";
}
};
////// Observer must cough up a directory for storing limited unencrypted work data
std::string ConcreteObserver::getWorkingDirectory()
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Getting working directory of './'" << std::endl;
return "./";
}
////// SyncAndMessageDispatch / PubSub initiated
/**
* Invoked when a new message was received.
*/
void ConcreteObserver::onSubscribedMessageReceived(const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Subscribed message received:\n" <<
msgObj << std::endl;
}
/**
* Invoked by PeerOutboundThread when a reply message was sent.
* Typically observer has little work to perform, other than perhaps doing some UX status updating.
* @param msg
*/
void ConcreteObserver::onResponseSent(const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Response message sent:\n" <<
msgObj << std::endl;
}
/**
* Called when message is disposed.
* @param fm
*/
void ConcreteObserver::onMessageDisposed(const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Message disposed:\n" <<
msgObj << std::endl;
}
/**
* Incoming subscribed messages that require manual user reply/action will fire this method from the peer
* system after the message is persisted.
* @param msg
*/
void ConcreteObserver::onMessagePersisted(const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Message persisted:\n" <<
msgObj << std::endl;
}
/**
* Called when message is removed.
* @param msg
*/
void ConcreteObserver::onMessageRemoved(const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Message removed:\n" <<
msgObj << std::endl;
}
/**
* Normally PubSubMessages that return true when isManualUserReplyRequired is invoked
* require manual user intervention. This observer method allows an automatic "bot" reply
* to those normally manual reply messages.
* @param msg An isManualUserReplyRequired message
* @param reply A reply message to msg; the responseData will need to be indicated by observer
* @return null if normal message persistence is required; non-null reply object if a "bot" reply
* is desired
*
* @return A JSON string to reply to the message with. We will take ownership of the memory.
*/
std::string ConcreteObserver::automaticReplyToManualMessage(const std::string &msg, const std::string &reply)
{
sequencelogic::JSONObject msgObj(msg);
sequencelogic::JSONObject replyObj(reply);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Automatic message:\n" <<
msgObj << "\n" << "(ConcreteObserver:" << __LINE__ << ")INFO: Reply:\n" <<
replyObj << std::endl;
return "";
}
//////// SAMD internal
void ConcreteObserver::onInactivityTimeReached()
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Inactivity timeout reached." << std::endl;
}
//////// StorageGuardService initiated
void ConcreteObserver::onSyncStart(const std::string &node)
{
sequencelogic::JSONObject nodeObj(node);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Sync started:\n" <<
nodeObj << std::endl;
}
void ConcreteObserver::onSyncComplete(const std::string &node)
{
sequencelogic::JSONObject nodeObj(node);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Sync completed:\n" <<
nodeObj << std::endl;
}
void ConcreteObserver::onSyncError(const std::string &node, const std::string &ex)
{
sequencelogic::JSONObject nodeObj(node);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Sync error:\n" <<
nodeObj << std::endl;
}
void ConcreteObserver::onSyncUploaded(const std::string &eye)
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Uploaded:\n" <<
eye << std::endl;
}
void ConcreteObserver::onSyncDownload(const std::string &eye)
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Downloaded:\n" <<
eye << std::endl;
}
void ConcreteObserver::onSyncDelete(const std::string &path)
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Deleted:\n" <<
path << std::endl;
}
//////// SAMD messages...
/**
* Notify the client that sync and message dispatch is starting.
*
* @param pUrn The device URN that is being sync'd
*/
void ConcreteObserver::onSyncAndDispatchStart(const std::string &urn)
{
sequencelogic::CloudGuardURN urnObj(urn);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Sync and dispatch started:\n" <<
urnObj.getUrn() << std::endl;
}
/**
* Notify the client that sync and message dispatch has ended.
*
* @param pUrn The device URN that is being sync'd
*/
void ConcreteObserver::onSyncAndDispatchStop(const std::string &urn)
{
sequencelogic::CloudGuardURN urnObj(urn);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Sync and dispatch ended:\n" <<
urnObj.getUrn() << std::endl;
}
//////// State and Errors
/**
* Was in an error (disconnected) state and now connected again.
* @return false to skip internal sync logic; true to internally invoke subscribe
*/
bool ConcreteObserver::onConnectionRestored()
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Connection restored." << std::endl;
return true;
}
void ConcreteObserver::onFatalError(int code, const std::string &msg)
{
sequencelogic::JSONObject msgObj(msg);
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Fatal error: " << GetError(code) << "(ConcreteObserver:" << __LINE__ << ")INFO: Message\n" <<
msgObj << std::endl;
}
void ConcreteObserver::onServiceException(const std::string &urlOrIdentifier, const std::string &ex)
{
std::cout << "(ConcreteObserver:" << __LINE__ << ")INFO: Service exception: " <<
urlOrIdentifier << "\n(ConcreteObserver:" << __LINE__ << ")INFO: Exception: " <<
ex << std::endl;
}