/*
    PIP - Platform Independent Primitives
    Introspection module - Base server structs
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIINTROSPECTION_SERVER_P_H
#define PIINTROSPECTION_SERVER_P_H

#include "pichunkstream.h"
#include "piintrospection_containers.h"
#include "piintrospection_containers_p.h"
#include "piintrospection_threads.h"
#include "piintrospection_threads_p.h"
#include "pisystemmonitor.h"


class PIP_EXPORT PIIntrospection {
public:
	enum InfoTypes {
		itInfo       = 0x01,
		itProcStat   = 0x02,
		itContainers = 0x04,
		itObjects    = 0x08,
		itThreads    = 0x10,
	};

	struct PIP_EXPORT RequiredInfo {
		RequiredInfo();
		PIFlags<InfoTypes> types;
	};

	struct PIP_EXPORT ProcessInfo {
		ProcessInfo();

		PIString execCommand, hostname, user, OS_name, OS_version, architecture;
		PIDateTime execDateTime;
		int processorsCount;

		PIStringList build_options;
	};

	struct PIP_EXPORT ProcessStat {
		ProcessStat() {}

		PISystemMonitor::ProcessStats proc;
		PIVector<PISystemMonitor::ThreadStats> threads;
	};

	struct PIP_EXPORT ObjectInfo {
		ObjectInfo();

		PIString classname, name;
		PIStringList parents;
		PIMap<PIString, PIVariant> properties;
		int queued_events;
	};

	static const uint sign;

	static ProcessInfo getInfo();
	static PIVector<ObjectInfo> getObjects();

	static PIByteArray packInfo();
	static void unpackInfo(PIByteArray & ba, ProcessInfo & info);

	static PIByteArray packProcStat(PISystemMonitor * sm);
	static void unpackProcStat(PIByteArray & ba, ProcessStat & info);

	static PIByteArray packContainers();
	static void unpackContainers(PIByteArray & ba, PIVector<PIIntrospectionContainers::TypeInfo> & data);

	static PIByteArray packThreads();
	static void unpackThreads(PIByteArray & ba, PIVector<PIIntrospectionThreads::ThreadInfo> & threads);

	static PIByteArray packObjects();
	static void unpackObjects(PIByteArray & ba, PIVector<PIIntrospection::ObjectInfo> & objects);
};


BINARY_STREAM_WRITE(PIIntrospection::RequiredInfo) {
	PIChunkStream cs;
	cs.add(1, v.types);
	s << cs.data();
	return s;
}
BINARY_STREAM_READ(PIIntrospection::RequiredInfo) {
	PIByteArray csba;
	s >> csba;
	PIChunkStream cs(csba);
	while (!cs.atEnd()) {
		switch (cs.read()) {
		case 1: cs.get(v.types); break;
		default: break;
		}
	}
	return s;
}

BINARY_STREAM_WRITE(PIIntrospection::ProcessInfo) {
	PIChunkStream cs;
	cs.add(1, v.architecture)
		.add(2, v.execCommand)
		.add(3, v.execDateTime)
		.add(4, v.hostname)
		.add(5, v.OS_name)
		.add(6, v.OS_version)
		.add(7, v.processorsCount)
		.add(8, v.user)
		.add(9, v.build_options);
	s << cs.data();
	return s;
}
BINARY_STREAM_READ(PIIntrospection::ProcessInfo) {
	PIByteArray csba;
	s >> csba;
	PIChunkStream cs(csba);
	while (!cs.atEnd()) {
		switch (cs.read()) {
		case 1: cs.get(v.architecture); break;
		case 2: cs.get(v.execCommand); break;
		case 3: cs.get(v.execDateTime); break;
		case 4: cs.get(v.hostname); break;
		case 5: cs.get(v.OS_name); break;
		case 6: cs.get(v.OS_version); break;
		case 7: cs.get(v.processorsCount); break;
		case 8: cs.get(v.user); break;
		case 9: cs.get(v.build_options); break;
		default: break;
		}
	}
	return s;
}

BINARY_STREAM_WRITE(PIIntrospection::ObjectInfo) {
	PIChunkStream cs;
	cs.add(1, v.classname).add(2, v.name).add(3, v.parents).add(4, v.properties).add(5, v.queued_events);
	s << cs.data();
	return s;
}
BINARY_STREAM_READ(PIIntrospection::ObjectInfo) {
	PIByteArray csba;
	s >> csba;
	PIChunkStream cs(csba);
	while (!cs.atEnd()) {
		switch (cs.read()) {
		case 1: cs.get(v.classname); break;
		case 2: cs.get(v.name); break;
		case 3: cs.get(v.parents); break;
		case 4: cs.get(v.properties); break;
		case 5: cs.get(v.queued_events); break;
		default: break;
		}
	}
	return s;
}

#endif // PIINTROSPECTION_SERVER_P_H
