#include "daemon.h" #include "shared.h" #include "pisysteminfo.h" Daemon::Daemon(): PIPeer("_pisd_" + PISystemInfo::instance()->hostname + "_" + PIString(rand() % 100)) { setName("Daemon"); timer.setName("__S__Daemon_timer"); enabled = false; mode = offset = cur = height = 0; //CONNECTU(&console, keyPressed, this, keyEvent) CONNECTU(&timer, tickEvent, this, timerEvent) timer.addDelimiter(5); timer.start(200); } PIScreenTile * Daemon::tile() const { return new PIScreenTile(); } void Daemon::keyEvent(PIKbdListener::KeyEvent key) { if (!enabled) return; int num = key.key - '0'; switch (mode) { case 0: if (num >= 0 && num <= 9) { connectToDaemon(dnames.value(key.key - '0')); return; } break; case 1: if (num >= 0 && num <= 9) { mode = num + 10; updateConsole(); return; } break; } switch (key.key) { case PIKbdListener::UpArrow: /*cur--; if (cur < 0) cur = 0; if (cur - offset < 2) offset--; if (offset < 0) offset = 0;*/ updateConsole(); break; case PIKbdListener::Space: /*if (cur < 0 || cur >= files.size_s()) return; if (selected.contains(files[cur].name)) selected.removeOne(files[cur].name); else selected << files[cur].name;*/ case PIKbdListener::DownArrow: /*cur++; if (cur >= files.size_s()) cur = files.size_s() - 1; if (cur - offset >= height - 2) offset++; if (offset >= files.size_s() - height) offset = files.size_s() - height;*/ updateConsole(); break; case PIKbdListener::Home: //cur = offset = 0; updateConsole(); break; case PIKbdListener::End: //cur = files.size_s() - 1; //offset = files.size_s() - height; updateConsole(); break; case PIKbdListener::Return: //if (cur < files.size_s() && cur >= 0) { //} break; case 'A': updateConsole(); break; case 'R': updateConsole(); break; case PIKbdListener::Esc: //selected.clear(); //updateConsole(); if (mode == 0) menuRequest(); else { if (mode > 1) { mode = 1; updateConsole(); } else disconnect(); } break; default: break; } } void Daemon::timerEvent(void * _d, int delim) { if (!enabled) return; if (delim == 1) { if (mode == 0) updateConsole(); } if (delim == 5) { if (conn_name.isEmpty()) return; PIByteArray ba; ba << int(RequestHostInfo); send(conn_name, ba); } } PIStringList Daemon::availableDaemons() const { available_daemons.clear(); piForeachC (PIPeer::PeerInfo & p, allPeers()) { if (!p.name.startsWith("_pisd_")) continue; available_daemons << p.name.mid(6); } return available_daemons; } void Daemon::connectToDaemon(const PIString & dn) { if (dn.isEmpty()) return; conn_name = "_pisd_" + dn; mode = 1; updateConsole(); } void Daemon::disconnect() { conn_name.clear(); mode = 0; updateConsole(); } PIString Daemon::connectedDaemon() const { return conn_name.mid(6); } void Daemon::peerDisconnected(const PIString & name) { if (name == conn_name) { conn_name.clear(); mode = 0; } } void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { //if (conn_name != from) return; if (data.size() < 4) return; PIByteArray ba(data), rba; int type; ba >> type; //piCout << "rec from" << from << type; switch (type) { case RequestHostInfo: makeMyHostInfo(); rba << int(ReplyHostInfo) << info_my; break; case ReplyHostInfo: ba >> info_other; makeOtherHostInfo(); break; }; if (!rba.isEmpty()) send(from, rba); } void Daemon::makeMyHostInfo() { info_my.execCommand = PISystemInfo::instance()->execCommand; info_my.hostname = PISystemInfo::instance()->hostname; info_my.user = PISystemInfo::instance()->user; info_my.OS_name = PISystemInfo::instance()->OS_name; info_my.OS_version = PISystemInfo::instance()->OS_version; info_my.architecture = PISystemInfo::instance()->architecture; info_my.execDateTime = PISystemInfo::instance()->execDateTime; info_my.processorsCount = PISystemInfo::instance()->processorsCount; info_my.ID = sys_mon.statistic().ID; info_my.threads = sys_mon.statistic().threads; info_my.priority = sys_mon.statistic().priority; info_my.physical_memsize = sys_mon.statistic().physical_memsize; info_my.share_memsize = sys_mon.statistic().share_memsize; info_my.cpu_load_system = sys_mon.statistic().cpu_load_system; info_my.cpu_load_user = sys_mon.statistic().cpu_load_user; } void Daemon::makeOtherHostInfo() { PISystemMonitor::ProcessStats & ps(const_cast(sys_mon_other.statistic())); ps.ID = info_other.ID; ps.threads = info_other.threads; ps.priority = info_other.priority; ps.physical_memsize = info_other.physical_memsize; ps.share_memsize = info_other.share_memsize; ps.cpu_load_system = info_other.cpu_load_system; ps.cpu_load_user = info_other.cpu_load_user; sys_mon_other.setStatistic(ps); } void Daemon::updateConsole() { if (!enabled) return; switch (mode) { case 0: tabConnect(); break; case 1: tabFeature(); break; case RequestHostInfo: tabInfo(); break; } } void Daemon::tabConnectedHeader(int cols) { /*console.addString("Connected to: " + connectedDaemon(), 1, PIConsole::BackGreen | PIConsole::Bold); for (int i = 2; i <= cols; ++i) console.addString(" ", i, PIConsole::Green | PIConsole::Inverse);*/ } void Daemon::tabConnect() { /*startTab(); console.addString("Available daemons:"); int num = 0; dnames.clear(); PIStringList adl = availableDaemons(); piForeachC (PIString & p, adl) { dnames[num] = p; console.addString(" " + PIString(num++) + " - " + p); } finishTab();*/ } void Daemon::tabFeature() { /*startTab(); tabConnectedHeader(); console.addString("What do you wish to do with this daemon?"); console.addString(" 0 - show host info"); console.addString(" 1 - file manager"); console.addString(" 2 - execute command"); finishTab();*/ } void Daemon::tabInfo() { /*startTab(2); tabConnectedHeader(2); console.addString("Exec command: " + info_other.execCommand); console.addString(" Executed on " + info_other.execDateTime.toString()); console.addString(" Hostname: " + info_other.hostname); console.addString(" Username: " + info_other.user); console.addString(" OS name: " + info_other.OS_name); console.addString(" OS version: " + info_other.OS_version); console.addString("Architecture: " + info_other.architecture); console.addString(" CPU count: " + PIString::fromNumber(info_other.processorsCount)); console.addVariable("of this process", &sys_mon_other, 2); finishTab();*/ }