/* PIP - Platform Independent Primitives Introspection module - Base server structs Copyright (C) 2020 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 . */ #include "piintrospection_server_p.h" #include "pichunkstream.h" #include "piinit.h" #include "pisysteminfo.h" #include "piobject.h" const uint PIIntrospection::sign = 0x0F1C2B3A; PIIntrospection::RequiredInfo::RequiredInfo() { types = itInfo; } PIIntrospection::ProcessInfo::ProcessInfo() { processorsCount = 0; } PIIntrospection::ObjectInfo::ObjectInfo() { queued_events = 0; } PIIntrospection::ProcessInfo PIIntrospection::getInfo() { PIIntrospection::ProcessInfo ret; PISystemInfo * si = PISystemInfo::instance(); ret.architecture = si->architecture; ret.execCommand = si->execCommand; ret.execDateTime = si->execDateTime; ret.hostname = si->hostname; ret.OS_name = si->OS_name; ret.OS_version = si->OS_version; ret.processorsCount = si->processorsCount; ret.user = si->user; ret.build_options = PIInit::buildOptions(); return ret; } PIVector PIIntrospection::getObjects() { PIVector ret; PIObject::mutexObjects().lock(); const PIVector & ao(PIObject::objects()); ret.resize(ao.size()); for (int i = 0; i < ao.size_s(); ++i) { ret[i].classname = PIStringAscii(ao[i]->className()); ret[i].name = ao[i]->name(); ret[i].properties = ao[i]->properties(); ret[i].parents = ao[i]->scopeList(); ao[i]->mutex_queue.lock(); ret[i].queued_events = ao[i]->events_queue.size_s(); ao[i]->mutex_queue.unlock(); } PIObject::mutexObjects().unlock(); return ret; } PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::RequiredInfo & v) { PIChunkStream cs; cs.add(1, v.types); b << cs.data(); return b; } PIByteArray & operator >>(PIByteArray & b, PIIntrospection::RequiredInfo & v) { PIByteArray csba; b >> csba; PIChunkStream cs(csba); while (!cs.atEnd()) { switch (cs.read()) { case 1: cs.get(v.types); break; default: break; } } return b; } PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::ProcessInfo & v) { 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); b << cs.data(); return b; } PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ProcessInfo & v) { PIByteArray csba; b >> 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 b; } PIByteArray & operator <<(PIByteArray & b, const PIIntrospection::ObjectInfo & v) { PIChunkStream cs; cs.add(1, v.classname).add(2, v.name).add(3, v.parents).add(4, v.properties).add(5, v.queued_events); b << cs.data(); return b; } PIByteArray & operator >>(PIByteArray & b, PIIntrospection::ObjectInfo & v) { PIByteArray csba; b >> 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 b; } PIByteArray PIIntrospection::packInfo() { PIByteArray ret; ret << getInfo(); return ret; } void PIIntrospection::unpackInfo(PIByteArray & ba, PIIntrospection::ProcessInfo & info) { ba >> info; } PIByteArray PIIntrospection::packProcStat(PISystemMonitor * sm) { ProcessStat ps; if (sm) { ps.proc = sm->statistic(); ps.threads = sm->threadsStatistic(); } PIByteArray ret; ret << ps.proc << ps.threads; return ret; } void PIIntrospection::unpackProcStat(PIByteArray & ba, PIIntrospection::ProcessStat & info) { ba >> info.proc >> info.threads; } PIByteArray PIIntrospection::packContainers() { PIByteArray ret; PIVector data; PIIntrospectionContainers * p = 0; #ifdef PIP_INTROSPECTION p = PIINTROSPECTION_CONTAINERS->p; #endif if (p) { data = p->getInfo(); } ret << data; return ret; } void PIIntrospection::unpackContainers(PIByteArray & ba, PIVector & data) { data.clear(); ba >> data; } PIByteArray PIIntrospection::packThreads() { PIByteArray ret; PIIntrospectionThreads * p = 0; #ifdef PIP_INTROSPECTION p = PIINTROSPECTION_THREADS->p; #endif if (p) { p->mutex.lock(); PIMap & tm(p->threads); for (PIMap::iterator i = tm.begin(); i != tm.end(); ++i) { i.value().classname = PIStringAscii(i.key()->className()); i.value().name = i.key()->name(); } ret << tm.values(); p->mutex.unlock(); } else { ret << PIVector(); } return ret; } void PIIntrospection::unpackThreads(PIByteArray & ba, PIVector & threads) { threads.clear(); ba >> threads; } PIByteArray PIIntrospection::packObjects() { PIByteArray ret; ret << getObjects(); return ret; } void PIIntrospection::unpackObjects(PIByteArray & ba, PIVector & objects) { objects.clear(); ba >> objects; }