360 lines
14 KiB
C++
Executable File
360 lines
14 KiB
C++
Executable File
/*
|
|
PIP - Platform Independent Primitives
|
|
PIp System Daemon
|
|
Copyright (C) 2016 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 "pisingleapplication.h"
|
|
#include "pisystemmonitor.h"
|
|
#include "pisysteminfo.h"
|
|
#include "piprocess.h"
|
|
#include "picli.h"
|
|
#include "file_manager.h"
|
|
#include "daemon.h"
|
|
#include "shared.h"
|
|
|
|
class _Init {
|
|
public:
|
|
_Init() {srand(PISystemTime::current().toMicroseconds());}
|
|
};
|
|
_Init _pisd_init;
|
|
|
|
PISystemMonitor sys_mon;
|
|
PIScreen * screen = 0;
|
|
//FileManager file_manager;
|
|
|
|
|
|
class MainMenu: public PITimer {
|
|
PIOBJECT_SUBCLASS(MainMenu, PITimer)
|
|
public:
|
|
MainMenu(Daemon & d): daemon_(d) {
|
|
cur_peer = -1;
|
|
title = new TileSimple("title");
|
|
updateTitle(title);
|
|
title->back_format.color_back = Yellow;
|
|
title->size_policy = Fixed;
|
|
screen->rootTile()->addTile(title);
|
|
|
|
PIScreenTile * center = new PIScreenTile("center");
|
|
center->back_format.color_back = Cyan;
|
|
center->size_policy = Expanding;
|
|
center->setMargins(2, 2, 1, 1);
|
|
screen->rootTile()->addTile(center);
|
|
|
|
PIScreenTile * mt = tmenu = menuTile();
|
|
mt->show(); mt->name() = "main menu";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
mt = tinfo = infoTile();
|
|
mt->hide(); mt->name() = "local info";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
//mt = tfm = daemon_.fm.tile();
|
|
mt->hide(); mt->name() = "file manager";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
mt = tdaemon = daemon_.tile();
|
|
mt->hide(); mt->name() = "daemon";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
mt = tpeer = peerTile();
|
|
mt->hide(); mt->name() = "peer info";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
mt = tpeerdiag = peerDiagTile();
|
|
mt->hide(); mt->name() = "peer diag";
|
|
center->addTile(mt); mtiles << mt;
|
|
|
|
TilePICout * outt = new TilePICout();
|
|
outt->size_policy = PIScreenTypes::Expanding;
|
|
screen->rootTile()->addTile(outt);
|
|
|
|
CONNECTU(screen, tileEvent, this, tileEvent)
|
|
CONNECTU(screen, keyPressed, this, keyEvent)
|
|
CONNECTU(&daemon_, menuRequest, this, menuRequest)
|
|
start(10);
|
|
}
|
|
PIScreenTile * menuTile() {
|
|
TileList * ret = new TileList();
|
|
ret->content << TileList::Row("Show local info", CellFormat());
|
|
ret->content << TileList::Row("Local file manager", CellFormat());
|
|
ret->content << TileList::Row("Connect to another daemon", CellFormat());
|
|
ret->content << TileList::Row("Peer info", CellFormat());
|
|
ret->content << TileList::Row("Peer reinit", CellFormat());
|
|
ret->content << TileList::Row("Peer diagnostics", CellFormat());
|
|
ret->content << TileList::Row("Peer change self name", CellFormat());
|
|
ret->content << TileList::Row("Exit", CellFormat());
|
|
ret->selection_mode = TileList::NoSelection;
|
|
return ret;
|
|
}
|
|
PIScreenTile * infoTile() {
|
|
TileSimple * ret = new TileSimple();
|
|
ret->content << TileSimple::Row("Exec command: " + PISystemInfo::instance()->execCommand, CellFormat());
|
|
ret->content << TileSimple::Row(" Executed on " + PISystemInfo::instance()->execDateTime.toString(), CellFormat());
|
|
ret->content << TileSimple::Row(" Hostname: " + PISystemInfo::instance()->hostname, CellFormat());
|
|
ret->content << TileSimple::Row(" Username: " + PISystemInfo::instance()->user, CellFormat());
|
|
ret->content << TileSimple::Row(" OS name: " + PISystemInfo::instance()->OS_name, CellFormat());
|
|
ret->content << TileSimple::Row(" OS version: " + PISystemInfo::instance()->OS_version, CellFormat());
|
|
ret->content << TileSimple::Row("Architecture: " + PISystemInfo::instance()->architecture, CellFormat());
|
|
ret->content << TileSimple::Row(" CPU count: " + PIString::fromNumber(PISystemInfo::instance()->processorsCount), CellFormat());
|
|
return ret;
|
|
}
|
|
PIScreenTile * peerDiagTile() {
|
|
PIScreenTile * ret = new PIScreenTile();
|
|
TileSimple * htl = new TileSimple();
|
|
htl->size_policy = PIScreenTypes::Fixed;
|
|
ret->direction = PIScreenTypes::Vertical;
|
|
htl->content << TileSimple::Row("Peer: " + daemon_.name() + " | " + daemon_.selfInfo().name, CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
|
|
PIScreenTile * diag = new PIScreenTile();
|
|
diag->direction = PIScreenTypes::Horizontal;
|
|
peerdiagdata_tl = new TileSimple();
|
|
peerdiagservice_tl = new TileSimple();
|
|
ret->addTile(htl);
|
|
ret->addTile(diag);
|
|
diag->addTile(peerdiagdata_tl);
|
|
diag->addTile(peerdiagservice_tl);
|
|
// updatePeerInfo();
|
|
return ret;
|
|
}
|
|
PIScreenTile * peerTile() {
|
|
PIScreenTile* ret = new PIScreenTile();
|
|
ret->direction = PIScreenTypes::Vertical;
|
|
peerinfo_header = new TileSimple();
|
|
peerinfo_header->size_policy = PIScreenTypes::Fixed;
|
|
peerinfo_header->content << TileSimple::Row("Peer: " + daemon_.name() + " | " + daemon_.selfInfo().name, CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
|
|
addrs_tl = new TileList();
|
|
peers_tl = new TileList();
|
|
peerinfo_tl = new TileSimple();
|
|
peermap_tl = new TileList();
|
|
peerinfo_tl->size_policy = PIScreenTypes::Fixed;
|
|
ret->addTile(peerinfo_header);
|
|
ret->addTile(peers_tl);
|
|
ret->addTile(peerinfo_tl);
|
|
ret->addTile(addrs_tl);
|
|
ret->addTile(peermap_tl);
|
|
// updatePeerInfo();
|
|
return ret;
|
|
}
|
|
|
|
void updateTitle(TileSimple * tl) {
|
|
tl->content.clear();
|
|
tl->content << TileSimple::Row("pisd (PI System Daemon, PIP version " + PIPVersion() + ")", CellFormat(Black, Transparent));
|
|
tl->content << TileSimple::Row("This daemon: \"" + daemon_.thisDaemonName() + "\"", CellFormat(Black, Transparent));
|
|
}
|
|
void updatePeerDiag(TileSimple * tl, const PIDiagnostics & diag) {
|
|
tl->content.clear();
|
|
tl->content << TileSimple::Row(diag.name() + " diagnostics", CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
|
|
tl->content << TileSimple::Row("Received count: " + PIString::fromNumber(diag.receiveCount()), CellFormat());
|
|
tl->content << TileSimple::Row("Invalid count: " + PIString::fromNumber(diag.wrongCount()), CellFormat());
|
|
tl->content << TileSimple::Row("Sended count: " + PIString::fromNumber(diag.sendCount()), CellFormat());
|
|
tl->content << TileSimple::Row("Immediate Frequency, Hz: " + PIString::fromNumber(diag.immediateFrequency()), CellFormat());
|
|
tl->content << TileSimple::Row("Integral Frequency, Hz: " + PIString::fromNumber(diag.integralFrequency()), CellFormat());
|
|
tl->content << TileSimple::Row("Receive speed: " + diag.receiveSpeed(), CellFormat());
|
|
tl->content << TileSimple::Row("Send speed: " + diag.sendSpeed(), CellFormat());
|
|
tl->content << TileSimple::Row("Quality: " + PIString::fromNumber((int)diag.quality()), CellFormat());
|
|
}
|
|
void updatePeerInfo() {
|
|
bool pm = daemon_.lockedPeers();
|
|
screen->lock();
|
|
daemon_.lock();
|
|
peers_tl->content.clear();
|
|
addrs_tl->content.clear();
|
|
peerinfo_tl->content.clear();
|
|
peermap_tl->content.clear();
|
|
peers_tl->content << TileList::Row("this | 0 | 0 | " + PIString::fromNumber(daemon_.allPeers().size_s()) +
|
|
" [em = " + PIString::fromBool(daemon_.lockedEth()) + ", "
|
|
"mm = " + PIString::fromBool(daemon_.lockedMBcasts()) + ", "
|
|
"sm = " + PIString::fromBool(daemon_.lockedSends()) + ", "
|
|
"ms = " + PIString::fromBool(daemon_.lockedMCSends()) + ", "
|
|
"pm = " + PIString::fromBool(pm) + "]", CellFormat());
|
|
piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers())
|
|
peers_tl->content << TileList::Row(p.name + " | d = " + PIString::fromNumber(p.dist) +
|
|
" | p = " + PIString::fromNumber(p.ping()) +
|
|
" | n = " + PIString::fromBool(p.isNeighbour())
|
|
, CellFormat());
|
|
PIPeer::PeerInfo pi = daemon_.selfInfo();
|
|
if (cur_peer >= 0 && cur_peer < daemon_.allPeers().size_s()) pi = daemon_.allPeers()[cur_peer];
|
|
peerinfo_tl->content << TileSimple::Row("Name: " + pi.name, CellFormat());
|
|
peerinfo_tl->content << TileSimple::Row("Addreses: " + PIString::fromNumber(pi.addresses.size()), CellFormat());
|
|
peerinfo_tl->content << TileSimple::Row("Neighbours: " + pi.neighbours.join(", "), CellFormat());
|
|
piForeachC(PIPeer::PeerInfo::Address &a , pi.addresses)
|
|
addrs_tl->content << TileList::Row(a.address +
|
|
" | p = " + PIString::fromNumber(a.ping) +
|
|
" | a = " + PIString::fromBool(a.isAvailable()), CellFormat());
|
|
typedef PIPair<PIString, PIVector <PIPeer::PeerInfo* > > PeerPair;
|
|
PIStringList peermap;
|
|
piForeachC(PeerPair &p , daemon_._peerMap()) {
|
|
PIString s = p.first + " | ";
|
|
piForeachCR(PIPeer::PeerInfo * pp, p.second) s += " -> " + pp->name;
|
|
peermap << s;
|
|
}
|
|
piForeachC(PIString &s , peermap)
|
|
peermap_tl->content << TileList::Row(s, CellFormat());
|
|
updatePeerDiag(peerdiagdata_tl, daemon_.diagnosticData());
|
|
updatePeerDiag(peerdiagservice_tl, daemon_.diagnosticService());
|
|
daemon_.unlock();
|
|
screen->unlock();
|
|
}
|
|
void tick(void* data_, int delimiter) {
|
|
updatePeerInfo();
|
|
}
|
|
EVENT_HANDLER(void, menuRequest) {
|
|
piForeach (PIScreenTile * t, mtiles)
|
|
t->hide();
|
|
daemon_.disconnect();
|
|
tmenu->show();
|
|
tmenu->setFocus();
|
|
}
|
|
EVENT_HANDLER2(void, tileEvent, PIScreenTile *, t, PIScreenTypes::TileEvent, e) {
|
|
if (t == tmenu) {
|
|
if (e.type == TileList::RowPressed) {
|
|
piForeach (PIScreenTile * t, mtiles)
|
|
t->hide();
|
|
switch (e.data.toInt()) {
|
|
case 0: tinfo->show(); break;
|
|
case 1: daemon_.fm.setLocal(); daemon_.showLocalFilemanager(); tdaemon->show(); break;
|
|
case 2: daemon_.fm.setRemote(); daemon_.showMainList(); tdaemon->show(); break;
|
|
case 3: tpeer->show(); peers_tl->setFocus(); break;
|
|
case 4: daemon_.reinit(); tmenu->show(); break;
|
|
case 5: tpeerdiag->show(); break;
|
|
case 6:
|
|
{
|
|
PIString nn = askUserInput("Peer name:");
|
|
if (!nn.isEmpty()) {
|
|
daemon_.changeName(pisd_prefix + nn);
|
|
peerinfo_header->content.clear();
|
|
peerinfo_header->content << TileSimple::Row("Peer: " + daemon_.name() + " | " + daemon_.selfInfo().name, CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
|
|
updateTitle(title);
|
|
}
|
|
menuRequest();
|
|
}
|
|
break;
|
|
case 7: PIKbdListener::exiting = true; break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if (t == peers_tl) {
|
|
if (e.type == TileList::RowPressed) {
|
|
cur_peer = e.data.toInt() - 1;
|
|
updatePeerInfo();
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, e) {
|
|
if (e.key == PIKbdListener::Esc && e.modifiers[PIKbdListener::Shift]) {
|
|
PIKbdListener::exiting = true;
|
|
return;
|
|
}
|
|
if (screen->dialogTile()) return;
|
|
if (tpeer->visible || tinfo->visible || tpeerdiag->visible)
|
|
if (e.key == PIKbdListener::Esc) menuRequest();
|
|
//piCout << "key" << e.key;
|
|
}
|
|
EVENT_HANDLER1(void, messageFromApp, const PIByteArray & , m) {
|
|
if (m[0] == 'k') PIKbdListener::exiting = true;
|
|
|
|
}
|
|
Daemon & daemon_;
|
|
PIScreenTile * tmenu, * tinfo, * tfm, * tdaemon, * tpeer, * tpeerdiag;
|
|
TileList * peers_tl, * addrs_tl, * peermap_tl;
|
|
TileSimple * title;
|
|
TileSimple * peerinfo_tl, * peerinfo_header;
|
|
TileSimple * peerdiagdata_tl, * peerdiagservice_tl;
|
|
PIVector<PIScreenTile * > mtiles;
|
|
int cur_peer;
|
|
};
|
|
|
|
|
|
|
|
void usage() {
|
|
piCout << PICoutManipulators::Bold << "PIP System Daemon";
|
|
piCout << PICoutManipulators::Cyan << "Version" << PICoutManipulators::Bold << PIPVersion() << PICoutManipulators::NewLine;
|
|
piCout << PICoutManipulators::Green << PICoutManipulators::Bold << "Usage:" << PICoutManipulators::Default
|
|
<< "\"pisd [-hdfk] [-n <name>] [-a <ip>]\"" << PICoutManipulators::NewLine;
|
|
piCout << PICoutManipulators::Green << PICoutManipulators::Bold << "Details:";
|
|
piCout << "-h --help " << PICoutManipulators::Green << "- display this message and exit";
|
|
piCout << "-d --daemon " << PICoutManipulators::Green << "- start as daemon";
|
|
piCout << "-k --kill " << PICoutManipulators::Green << "- kill daemon";
|
|
piCout << "-f --force " << PICoutManipulators::Green << "- don`t check for another running instance";
|
|
piCout << "-n --name <name> " << PICoutManipulators::Green << "- set daemon name";
|
|
piCout << "-a --address <ip>" << PICoutManipulators::Green << "- connect to remote daemon via tcp";
|
|
}
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
//piDebug = false;
|
|
PICLI cli(argc, argv);
|
|
cli.addArgument("help");
|
|
cli.addArgument("daemon");
|
|
cli.addArgument("force");
|
|
cli.addArgument("kill");
|
|
cli.addArgument("1");
|
|
cli.addArgument("name", true);
|
|
cli.addArgument("address", true);
|
|
if (cli.hasArgument("help")) {
|
|
usage();
|
|
return 0;
|
|
}
|
|
PIString name = cli.argumentValue("name");
|
|
PIString sip = cli.argumentValue("address");
|
|
PISingleApplication * sapp = 0;
|
|
if ((cli.hasArgument("1") && !cli.hasArgument("force")) || cli.hasArgument("kill")) {
|
|
sapp = new PISingleApplication("pisd");
|
|
if (cli.hasArgument("1")) {
|
|
if (!sapp->isFirst()) {
|
|
piCout << "Another pisd is running, exit";
|
|
return 0;
|
|
}
|
|
}
|
|
if (cli.hasArgument("kill")) {
|
|
sapp->sendMessage(PIByteArray("k", 1));
|
|
return 0;
|
|
}
|
|
}
|
|
if (cli.hasArgument("daemon")) {
|
|
PIStringList args;
|
|
args << "-1";
|
|
if (cli.hasArgument("force"))
|
|
args << "-f";
|
|
if (cli.hasArgument("address"))
|
|
args << "-a" << sip;
|
|
if (!name.isEmpty())
|
|
args << "-n" << name;
|
|
PIProcess::execIndependent(PISystemInfo::instance()->execCommand, args);
|
|
return 0;
|
|
}
|
|
PIINTROSPECTION_START
|
|
//cli.addArgument("");
|
|
screen = new PIScreen(false);
|
|
Daemon * daemon = new Daemon();
|
|
if (!sip.isEmpty()) daemon->setTcpServerIP(sip);
|
|
sys_mon.startOnSelf();
|
|
screen->enableExitCapture(PIKbdListener::F10);
|
|
if (!name.isEmpty())
|
|
daemon->changeName(pisd_prefix + name);
|
|
MainMenu * menu = new MainMenu(*daemon);
|
|
if (sapp) CONNECTU(sapp, messageReceived, menu, messageFromApp)
|
|
screen->start();
|
|
screen->waitForFinish();
|
|
delete menu;
|
|
delete daemon;
|
|
delete screen;
|
|
if (sapp) delete sapp;
|
|
return 0;
|
|
}
|