

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//_/_/
//_/_/  This file is part of PSYCLONE version 2.1.1.260726 (26 July 2026)
//_/_/  
//_/_/  developed by
//_/_/  
//_/_/  Communicative Machines Ltd. 
//_/_/    http://cmlabs.com/
//_/_/  Copyright (c) 2026
//_/_/  
//_/_/  in collaboration with 
//_/_/  
//_/_/  Icelandic Institute for Intelligent Machines ses
//_/_/    Menntavegur 1
//_/_/    101 Reykjavik
//_/_/    ICELAND
//_/_/    http://www.iiim.is/
//_/_/
//_/_/  PSYCLONE 2.1 is released under the GNU Lesser General Public License 
//_/_/  (LGPL) version 3.0, and is subject to the following
//_/_/  
//_/_/  - CADIA Clause: The license granted in and to the software under this 
//_/_/    agreement is a limited-use license. The software may not be used in 
//_/_/    furtherance of: (i) intentionally causing bodily injury or severe 
//_/_/    emotional harm to any person; (ii) invading the personal privacy or 
//_/_/    violating the human rights of any person; or (iii) committing or 
//_/_/    preparing for any act of war. 
//_/_/    
//_/_/  A commercial license for PSYCLONE 2.1 is also available to 
//_/_/  any interested party from the copyright holder.
//_/_/  
//_/_/  You can redistribute this copy of PSYCLONE 2.1 and/or modify it under 
//_/_/  the terms of the GNU General Public License as published by the Free 
//_/_/  Software Foundation, either version 3 of the License, or (at your 
//_/_/  option) any later version.
//_/_/  
//_/_/  You should have received a copy of the GNU General Public License
//_/_/  and the GNU Lesser General Public License along with PSYCLONE 2.1.  
//_/_/  If not, see <http://www.gnu.org/licenses/>.
//_/_/  
//_/_/  PSYCLONE 2.1 is distributed in the hope that it will be useful,
//_/_/  but WITHOUT ANY WARRANTY; without even the implied warranty of
//_/_/  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//_/_/  GNU General Public License for more details.
//_/_/
//_/_/  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//_/_/  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//_/_/  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//_/_/  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
//_/_/  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//_/_/  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//_/_/  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//_/_/  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//_/_/  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//_/_/  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//_/_/  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//_/_/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
#if !defined(_PSYPROBE_H_)
#define _PSYPROBE_H_

#pragma once

#include "MemoryManager.h"
#include "NetworkManager.h"

#define STDIN 0

namespace	cmlabs{

#define PSYPROBE_SYSTEM_IDLE	1
#define PSYPROBE_SYSTEM_INIT	2
#define PSYPROBE_SYSTEM_READY	3

struct WebAlias {
	char name[MAXKEYNAMELEN + 1];
	char suburl[MAXKEYNAMELEN + 1];
	char location[MAXFILENAMELEN + 1];
	char defaultFile[MAXKEYNAMELEN + 1];
};

class Node;
class Logging;

class PsyProbe;
typedef bool (* HTTPFunc)(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
typedef bool (* APIFunc)(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);

class PsyProbe : public Runnable, public NetworkReceiver
{
public:
	friend THREAD_RET THREAD_FUNCTION_CALL PsyProbeRun(THREAD_ARG arg);

	// ***************** Constructors *****************
	PsyProbe();
	virtual ~PsyProbe();

	// ***************** Initialisation *****************
	bool init(Node* parent);
	bool setRootDir(const char* root);
	bool addAlias(const char* name, const char* loc, const char* defaultFile);
	bool addPost(const char* name, PostSpec* postSpec);
	WebAlias* checkForAlias(const char* reqStr);

	bool setSystemReady();

	bool receiveHTTPRequest(HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	std::string getMainXML();
	std::string getSystemXML();
	std::string getSystemTimeXML();
	std::string getPerformanceMatrixXML();

	bool addCustomView(uint32 compID, const char* name, const char* templateURL);

protected:
	Node* node;
	std::string rootdir;
	uint8 status;

	std::map<std::string, HTTPFunc> httpCalls;
	std::map<std::string, APIFunc> apiCalls;
	std::map<std::string, std::string> templates;

	std::map<std::string, WebAlias> aliases;
	std::map<std::string, PostSpec*> posts;
	std::map<uint32, CustomView> customViews;

	// ***************** Running *****************
	bool run();

	std::string getTemplate(std::string name);
	bool replyError(const char* text, HTTPRequest* req, NetworkChannel* channel, uint64 conid);

	bool processXMLRequest(std::string reqString, HTTPRequest* req, NetworkChannel* channel, uint64 conid);

	bool callAPI(const char* apiName, HTTPRequest* req, NetworkChannel* channel, uint64 conid);

	static bool SimplePage(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool MainPage(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool ShutdownPage(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool SubscriptionPage(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool IDPage(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool LoadElement(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);

	static bool XMLMain(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLSystem(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLSystemTime(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLPerformanceMatrix(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLSubscriptions(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLIDs(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);
	static bool XMLComponents(PsyProbe* probe, HTTPRequest* req, NetworkChannel* channel, uint64 conid);

	static bool API_TriggerActivate(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_PostActivate(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_GetComponentData(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_GetComponentKeys(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_GetMessage(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_RetrieveMessages(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_PerformQuery(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_PostMessage(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);
	static bool API_GetLog(PsyProbe* probe, HTTPRequest* req, HTTPReply* reply);

};

THREAD_RET THREAD_FUNCTION_CALL PsyProbeRun(THREAD_ARG arg);

}

#include "Node.h"

#endif // _PSYPROBE_H_
