/*
PIP - Platform Independent Primitives
Remote console viewer
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
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 .
*/
#include "picli.h"
#include "pisystemmonitor.h"
#include "pisysteminfo.h"
#include "file_manager.h"
#include "daemon.h"
#include "shared.h"
void key_event(char key, void * );
class _Init {
public:
_Init() {srand(PISystemTime::current().toMicroseconds());}
};
_Init _pisd_init;
PIConsole console(false, key_event);
PISystemMonitor sys_mon;
FileManager file_manager;
Daemon daemon_;
int mode = 0, oldmode = -1;
class MenuOperator: public PIObject {
PIOBJECT(MenuOperator)
public:
MenuOperator() {
CONNECTU(&file_manager, menuRequest, this, menuRequest)
CONNECTU(&daemon_, menuRequest, this, menuRequest)
}
EVENT_HANDLER(void, menuRequest) {mode = 0;}
};
MenuOperator menu_operator;
void key_event(char key, void * ) {
if (key == PIKbdListener::Esc) {
;//mode = 0;
} else {
if (mode != 3) {
if (key < '0' || key > '9') return;
mode = key - '0';
}
}
}
void tabMenu() {
startTab();
console.addString("Select with numeric key:");
console.addString(" 0 - this screen");
console.addString(" 1 - show host info");
console.addString(" 2 - local file manager");
console.addString(" 3 - connect to another daemon");
finishTab();
}
void tabInfo() {
startTab();
console.addString("Exec command: " + PISystemInfo::instance()->execCommand);
console.addString(" Executed on " + PISystemInfo::instance()->execDateTime.toString());
console.addString(" Hostname: " + PISystemInfo::instance()->hostname);
console.addString(" Username: " + PISystemInfo::instance()->user);
console.addString(" OS name: " + PISystemInfo::instance()->OS_name);
console.addString(" OS version: " + PISystemInfo::instance()->OS_version);
console.addString("Architecture: " + PISystemInfo::instance()->architecture);
console.addString(" CPU count: " + PIString::fromNumber(PISystemInfo::instance()->processorsCount));
console.addString(" ", 2, PIConsole::Yellow | PIConsole::Inverse);
console.addVariable("of this process", &sys_mon, 2);
finishTab();
}
void tabConnect() {
}
#include
int main(int argc, char * argv[]) {
/*PICRC<16> crc(0xFF12FF34);
PIByteArray ba(4096);
for (int i = 0; i < ba.size_s(); ++i) ba[i] = i % 256;
PITimeMeasurer tm;
uint_cl<16> ret;
for (int i = 0; i < 1000; ++i) {
ret = crc.calculate(ba);
}*/
/*if (argc < 2) return 0;
PIFile af;
if (!af.open(argv[1], PIIODevice::ReadOnly))
return 0;
piCout << "open" << af.path();
PIByteArray sign, jfif, rsign, rdata;
int phase = 0, pos = 0, asize = af.size();
ushort jct = 0, jcl = 0;
uint ct = 0, cl = 0, cc = 0, tend = 1229278788;
sign << uchar(0x89) << uchar(0x50) << uchar(0x4E) << uchar(0x47) << uchar(0x0D) << uchar(0x0A) << uchar(0x1A) << uchar(0x0A);
jfif << uchar(0x4A) << uchar(0x46) << uchar(0x49) << uchar(0x46);
rsign.resize(sign.size());
PIByteArray nf;
PITimeMeasurer tm;
int cnt = 0, j = 0;
while (!af.isEnd()) {
if (tm.elapsed_m() >= 500) {
tm.reset();
piCout << "readed" << PIString::readableSize(af.pos());
}
if (pos >= asize) break;
switch (phase) {
case 0:
af.seek(pos);
af.read(rsign.data(), rsign.size_s());
if (rsign == sign) {
phase = 1;
nf = rsign;
} else if (rsign.resized(4) == jfif) {
phase = 2;
af.seek(pos - 6);
nf.resize(2);
af.read(nf.data(), nf.size_s());
} else
pos++;
break;
case 1:
af.read(&cl, 4);
af.read(&ct, 4);
rdata.resize(piLetobe(cl));
af.read(rdata.data(), rdata.size_s());
af.read(&cc, 4);
nf.append(&cl, 4);
nf.append(&ct, 4);
nf.append(rdata);
nf.append(&cc, 4);
if (piLetobe(ct) == tend) {
piCout << "found PNG size" << PIString::readableSize(nf.size_s());
PIFile wf("./PNG/" + PIString(cnt++) + ".png");
if (wf.open(PIIODevice::ReadWrite))
wf.write(nf.data(), nf.size());
nf.clear();
phase = 0;
pos = af.pos();
}
break;
case 2:
//jct = 0;
//while (jct != ushort(0xD9FF)) {
// af.read(&jct, 2);
// nf.append(&jct, 2);
//}
//if (++j >= 30) return 0;
af.read(&jct, 2);
nf.append(&jct, 2);
if (jct == ushort(0xD9FF)) {
piCout << "found JPG size" << PIString::readableSize(nf.size_s());
PIFile wf("./PNG/" + PIString(cnt++) + ".jpg");
if (wf.open(PIIODevice::ReadWrite))
wf.write(nf.data(), nf.size());
nf.clear();
phase = 0;
pos = af.pos();
break;
}
af.read(&jcl, 2);
nf.append(&jcl, 2);
if (jct == 0xDAFF) {
jct = 0;
int pp = af.pos();
rdata.resize(1024*1024*3);
af.read(rdata.data(), rdata.size_s());
nf.append(rdata);
//while (jct != ushort(0xFFD9)) {
// af.read(&jct, 2);
// nf.append(&jct, 2);
// //piCout << Hex << jct;
//}
piCout << "found JPG size" << PIString::readableSize(nf.size_s());
PIFile wf("./PNG/" + PIString(cnt++) + ".jpg");
if (wf.open(PIIODevice::ReadWrite))
wf.write(nf.data(), nf.size());
nf.clear();
phase = 0;
pos = pp + 1;
break;
}
//piCout << "length" << piLetobe(jcl);
rdata.resize(piLetobe(jcl) - 2);
af.read(rdata.data(), rdata.size_s());
nf.append(rdata);
break;
};
};
return 0;*/
//piCout << Hex << uint(ret) << tm.elapsed_m();
PICLI cli(argc, argv);
//cli.addArgument("");
sys_mon.startOnSelf();
console.enableExitCapture();
console.setDefaultAlignment(PIConsole::Right);
console.start();
//WAIT_FOR_EXIT
//return 0;
while (!PIKbdListener::exiting) {
if (oldmode != mode) {
file_manager.disable();
daemon_.disable();
oldmode = mode;
switch (mode) {
case 0: /// menu
tabMenu();
break;
case 1: /// host info
tabInfo();
break;
case 2: /// file manager
file_manager.enable();
break;
case 3: /// daemon
daemon_.enable();
break;
}
}
piMSleep(100);
}
return 0;
};