209 lines
5.4 KiB
C++
209 lines
5.4 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Introspection module - Base server structs
|
|
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it 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.
|
|
|
|
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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "piintrospection_server_p.h"
|
|
#include "pichunkstream.h"
|
|
#include "piinit.h"
|
|
#include "pisysteminfo.h"
|
|
#include "piobject.h"
|
|
|
|
|
|
const uint PIIntrospection::sign = 0x0F1C2B3A;
|
|
|
|
|
|
|
|
|
|
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::ObjectInfo> PIIntrospection::getObjects() {
|
|
PIVector<PIIntrospection::ObjectInfo> ret;
|
|
PIObject::mutexObjects().lock();
|
|
const PIVector<PIObject * > & 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 & s, const std::unordered_map<uint, PIIntrospectionContainers::Type> & v) {
|
|
PIMap<uint, PIIntrospectionContainers::Type> m;
|
|
for (typename std::unordered_map<uint, PIIntrospectionContainers::Type>::const_iterator i = v.cbegin(); i != v.cend(); ++i) {
|
|
m[i->first] = i->second;
|
|
}
|
|
s << m;
|
|
return s;
|
|
}
|
|
|
|
|
|
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::packContainers() {
|
|
PIByteArray ret;
|
|
PIIntrospectionContainers * p = PIINTROSPECTION_CONTAINERS->p;
|
|
p->mutex.lock();
|
|
std::unordered_map<uint, PIIntrospectionContainers::Type> data = p->data;
|
|
std::unordered_map<uint, std::string> typenames = p->typenames;
|
|
p->mutex.unlock();
|
|
ret << data << typenames;
|
|
return ret;
|
|
}
|
|
|
|
|
|
void PIIntrospection::unpackContainers(PIByteArray & ba, PIMap<uint, PIIntrospectionContainers::Type> & data, PIMap<uint, PIString> & typenames) {
|
|
data.clear();
|
|
typenames.clear();
|
|
ba >> data >> typenames;
|
|
}
|
|
|
|
|
|
PIByteArray PIIntrospection::packThreads() {
|
|
PIByteArray ret;
|
|
PIIntrospectionThreads * p = PIINTROSPECTION_THREADS->p;
|
|
p->mutex.lock();
|
|
ret << p->threads.values();
|
|
p->mutex.unlock();
|
|
return ret;
|
|
}
|
|
|
|
|
|
void PIIntrospection::unpackThreads(PIByteArray & ba, PIVector<PIIntrospectionThreads::ThreadInfo> & threads) {
|
|
threads.clear();
|
|
ba >> threads;
|
|
}
|
|
|
|
|
|
PIByteArray PIIntrospection::packObjects() {
|
|
PIByteArray ret;
|
|
ret << getObjects();
|
|
return ret;
|
|
}
|
|
|
|
|
|
void PIIntrospection::unpackObjects(PIByteArray & ba, PIVector<PIIntrospection::ObjectInfo> & objects) {
|
|
objects.clear();
|
|
ba >> objects;
|
|
}
|