3.10.2013 - PIPeer release, PIConsole now can work as server and remote client. Remote console test program in directory "remote_console"

This commit is contained in:
peri4
2013-10-03 16:04:02 +04:00
parent 9111640ca8
commit 4b90f2818e
56 changed files with 6422 additions and 673 deletions

View File

@@ -18,6 +18,7 @@
*/
#include "piconsole.h"
#include "pipeer.h"
extern PIMutex __PICout_mutex__;
@@ -28,6 +29,7 @@ PIConsole::PIConsole(bool startNow, KBFunc slot): PIThread() {
needLockRun(true);
ret_func = slot;
num_format = 0;
vid = 0;
cur_tab = width = height = pwidth = pheight = max_y = 0;
def_align = Nothing;
#ifdef WINDOWS
@@ -40,13 +42,20 @@ PIConsole::PIConsole(bool startNow, KBFunc slot): PIThread() {
GetConsoleMode(hOut, &smode);
GetConsoleCursorInfo(hOut, &curinfo);
#endif
tabs.reserve(16);
addTab("main");
listener = new PIKbdListener(key_event, this);
peer = 0;
server_mode = false;
state = Disconnected;
peer_timer.addDelimiter(20);
CONNECT2(void, void * , int, &peer_timer, timeout, this, peerTimer);
if (startNow) start();
}
PIConsole::~PIConsole() {
stopPeer();
if (isRunning())
stop();
clearTabs(false);
@@ -227,6 +236,7 @@ PIString PIConsole::fstr(PIFlags<PIConsole::Format> f) {
attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
if (f[PIConsole::Bold]) attr |= FOREGROUND_INTENSITY;
if (f[PIConsole::Underline]) attr |= COMMON_LVB_UNDERSCORE;
SetConsoleTextAttribute(hOut, attr);
return PIString();
@@ -317,6 +327,7 @@ void PIConsole::run() {
width = ws.ws_col;
height = ws.ws_row;
#endif
//fflush(0); return;
__PICout_mutex__.lock();
if (pwidth != width || pheight != height) {
clearScreen();
@@ -336,27 +347,37 @@ void PIConsole::run() {
if (j > height - 3) continue;
j++;
moveRight(cx);
if (tv.type == 0 && tv.s == 0) {
if (tv.type == 15) {
newLine();
continue;
}
moveRight(tv.offset);
PIString rstr;
const void * ptr = 0;
if (tv.remote) {
if (tv.type == 0) {
rstr << PIByteArray(tv.rdata);
ptr = &rstr;
} else
ptr = tv.rdata.data();
} else
ptr = tv.ptr;
switch (tv.type) {
case 0: clen = printValue(tv.s != 0 ? *tv.s : PIString(), tv.format); break;
case 1: clen = printValue(tv.b != 0 ? *tv.b : false, tv.format); break;
case 2: clen = printValue(tv.i != 0 ? *tv.i : 0, tv.format); break;
case 3: clen = printValue(tv.l != 0 ? *tv.l : 0l, tv.format); break;
case 4: clen = printValue(tv.c != 0 ? *tv.c : char(0), tv.format); break;
case 5: clen = printValue(tv.f != 0 ? *tv.f : 0.f, tv.format); break;
case 6: clen = printValue(tv.d != 0 ? *tv.d : 0., tv.format); break;
case 7: clen = printValue(tv.sh != 0 ? *tv.sh : short(0), tv.format); break;
case 8: clen = printValue(tv.ui != 0 ? *tv.ui : 0u, tv.format); break;
case 9: clen = printValue(tv.ul != 0 ? *tv.ul : 0ul, tv.format); break;
case 10: clen = printValue(tv.ush != 0 ? *tv.ush : ushort(0), tv.format); break;
case 11: clen = printValue(tv.uc != 0 ? *tv.uc : uchar(0), tv.format); break;
case 12: clen = printValue(tv.ll != 0 ? *tv.ll : 0l, tv.format); break;
case 13: clen = printValue(tv.ull != 0 ? *tv.ull : 0ull, tv.format); break;
case 14: clen = printValue(bitsValue(tv.ptr, tv.bitFrom, tv.bitCount), tv.format); break;
case 0: clen = printValue(ptr != 0 ? *(const PIString*)ptr : PIString(), tv.format); break;
case 1: clen = printValue(ptr != 0 ? *(const bool*)ptr : false, tv.format); break;
case 2: clen = printValue(ptr != 0 ? *(const int*)ptr : 0, tv.format); break;
case 3: clen = printValue(ptr != 0 ? *(const long*)ptr : 0l, tv.format); break;
case 4: clen = printValue(ptr != 0 ? *(const char*)ptr : char(0), tv.format); break;
case 5: clen = printValue(ptr != 0 ? *(const float*)ptr : 0.f, tv.format); break;
case 6: clen = printValue(ptr != 0 ? *(const double*)ptr : 0., tv.format); break;
case 7: clen = printValue(ptr != 0 ? *(const short*)ptr : short(0), tv.format); break;
case 8: clen = printValue(ptr != 0 ? *(const uint*)ptr : 0u, tv.format); break;
case 9: clen = printValue(ptr != 0 ? *(const ulong*)ptr : 0ul, tv.format); break;
case 10: clen = printValue(ptr != 0 ? *(const ushort*)ptr : ushort(0), tv.format); break;
case 11: clen = printValue(ptr != 0 ? *(const uchar*)ptr : uchar(0), tv.format); break;
case 12: clen = printValue(ptr != 0 ? *(const llong*)ptr : 0l, tv.format); break;
case 13: clen = printValue(ptr != 0 ? *(const ullong*)ptr: 0ull, tv.format); break;
case 14: clen = printValue(bitsValue(ptr, tv.bitFrom, tv.bitCount), tv.format); break;
}
if (clen + tv.offset < (uint)col_wid) {
#if defined(QNX) || defined(FREE_BSD)
@@ -401,7 +422,7 @@ void PIConsole::fillLabels() {
if (ccol.alignment != Nothing) {
mx = 0;
piForeachC (Variable & j, cvars)
if (j.s != 0)
if (!j.isEmpty())
if (mx < j.name.size())
mx = j.name.size();
mx += 2;
@@ -416,7 +437,7 @@ void PIConsole::fillLabels() {
Variable & tv(cvars[j]);
cvars[j].nx = cx;
cvars[j].ny = cy;
if (tv.name.size() == 0) {
if (tv.name.isEmpty()) {
cvars[j].offset = 0;
clearLine();
newLine();
@@ -424,7 +445,8 @@ void PIConsole::fillLabels() {
continue;
}
clearLine();
if (tv.type == 0 && tv.s == 0) {
//piCout << tv.name << tv.type << tv.ptr;
if (tv.type == 15) {
cvars[j].offset = cvars[j].name.length();
cvars[j].nx += cvars[j].offset;
printLine(tv.name, cx, tv.format);
@@ -432,7 +454,7 @@ void PIConsole::fillLabels() {
cy++;
continue;
}
if (tv.s != 0) {
if (!tv.isEmpty()) {
switch (ccol.alignment) {
case Nothing:
cvars[j].offset = (tv.name + ": ").length();
@@ -489,7 +511,7 @@ void PIConsole::status() {
}
int PIConsole::bitsValue(const void * src, int offset, int count) {
int PIConsole::bitsValue(const void * src, int offset, int count) const {
int ret = 0, stbyte = offset / 8, cbit = offset - stbyte * 8;
char cbyte = reinterpret_cast<const char * >(src)[stbyte];
for (int i = 0; i < count; i++) {
@@ -522,38 +544,38 @@ const char * PIConsole::toBin(const void * d, int s) {
}
#define ADD_VAR_BODY tv.name = name; tv.bitFrom = tv.bitCount = 0; tv.format = format; checkColumn(col);
#define ADD_VAR_BODY vid++; tv.id = vid; tv.name = name; tv.bitFrom = tv.bitCount = 0; tv.format = format; tv.remote = false; checkColumn(col);
void PIConsole::addString(const PIString & name, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 0; tv.s = 0; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const PIString* ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 0; tv.s = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 15; tv.size = 0; tv.ptr = 0; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const PIString * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 0; tv.size = 0; tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const bool * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 1; tv.b = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 1; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const int * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 2; tv.i = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 2; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const long * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 3; tv.l = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 3; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const char * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 4; tv.c = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 4; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const float * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 5; tv.f = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 5; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const double * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 6; tv.d = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 6; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const short * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 7; tv.sh = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 7; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const uint * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 8; tv.ui = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 8; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const ulong * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 9; tv.ul = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 9; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const ushort * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 10; tv.ush = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 10; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const uchar * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 11; tv.uc = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 11; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const llong * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 12; tv.ll = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 12; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const ullong * ptr, int col, PIFlags<PIConsole::Format> format) {
ADD_VAR_BODY tv.type = 13; tv.ull = ptr; column(col).push_back(tv);}
ADD_VAR_BODY tv.type = 13; tv.size = sizeof(*ptr); tv.ptr = ptr; column(col).push_back(tv);}
void PIConsole::addVariable(const PIString & name, const PIProtocol * ptr, int col, PIFlags<PIConsole::Format> format) {
addString("protocol " + name, col, format | PIConsole::Bold);
addVariable("Rec - " + ptr->receiverDeviceName(), ptr->receiverDeviceState_ptr(), col, format);
@@ -592,10 +614,10 @@ void PIConsole::addVariable(const PIString & name, const PISystemMonitor * ptr,
addVariable("cpu load", &(ptr->statistic().cpu_load_system), col, format);
}
void PIConsole::addBitVariable(const PIString & name, const void * ptr, int fromBit, int bitCount, int col, PIFlags<PIConsole::Format> format) {
tv.name = name; tv.bitFrom = fromBit; tv.bitCount = bitCount; tv.type = 14; tv.ptr = ptr; tv.format = format;
vid++; tv.id = vid; tv.size = sizeof(ullong); tv.name = name; tv.bitFrom = fromBit; tv.bitCount = bitCount; tv.type = 14; tv.ptr = ptr; tv.format = format;
checkColumn(col); column(col).push_back(tv);}
void PIConsole::addEmptyLine(int col, uint count) {
tv.name = ""; tv.type = 0; tv.d = 0; tv.format = Normal;
tv.id = 0; tv.size = 0; tv.name = ""; tv.type = 0; tv.ptr = 0; tv.format = Normal;
for (uint i = 0; i < count; ++i) {
checkColumn(col);
column(col).push_back(tv);
@@ -670,3 +692,226 @@ inline int PIConsole::printValue(const ushort value, PIFlags<PIConsole::Format>
inline int PIConsole::printValue(const uint value, PIFlags<PIConsole::Format> format) {PRINT_VAR_BODY}
inline int PIConsole::printValue(const ulong value, PIFlags<PIConsole::Format> format) {PRINT_VAR_BODY}
inline int PIConsole::printValue(const ullong value, PIFlags<PIConsole::Format> format) {PRINT_VAR_BODY}
void PIConsole::startServer(const PIString & name) {
stopPeer();
server_mode = true;
peer = new PIPeer("_rcs_:" + name);
CONNECT2(void, const PIString & , const PIByteArray &, peer, dataReceivedEvent, this, peerReceived);
peer_timer.start(50.);
serverSendInfo();
}
void PIConsole::stopPeer() {
remote_clients.clear();
peer_timer.stop();
if (peer != 0) delete peer;
peer = 0;
state = Disconnected;
}
PIStringList PIConsole::clients() const {
PIStringList sl;
if (peer == 0) return sl;
piForeachC (PIPeer::PeerInfo & i, peer->allPeers()) {
if (i.name.left(6) != "_rcc_:") continue;
sl << i.name.right(i.name.length() - 6);
}
return sl;
}
void PIConsole::listenServers() {
stopPeer();
server_mode = false;
server_name.clear();
srand(currentSystemTime().nanoseconds);
peer = new PIPeer("_rcc_:" + currentDateTime().toString("hhmmssddMMyy_") + PIString::fromNumber(rand()));
CONNECT2(void, const PIString & , const PIByteArray &, peer, dataReceivedEvent, this, peerReceived);
peer_timer.start(100.);
}
PIStringList PIConsole::availableServers() const {
PIStringList sl;
if (peer == 0) return sl;
piForeachC (PIPeer::PeerInfo & i, peer->allPeers()) {
if (i.name.left(6) != "_rcs_:") continue;
sl << i.name.right(i.name.length() - 6);
}
return sl;
}
void PIConsole::connectToServer(const PIString & name) {
if (peer == 0) listenServers();
server_name = name;
}
void PIConsole::disconnect() {
stopPeer();
}
void PIConsole::serverSendInfo() {
if (peer == 0) return;
PIByteArray ba;
ba << int(0xAA);
peer->sendToAll(ba);
}
void PIConsole::serverSendData() {
if (peer == 0) return;
PIByteArray ba;
PIVector<VariableContent> content;
piForeach (Tab & t, tabs)
piForeach (Column & c, t.columns)
piForeach (Variable & v, c.variables)
if (!v.isEmpty() && v.id > 0) {
VariableContent vc;
vc.id = v.id;
v.writeData(vc.rdata);
content << vc;
}
piForeach (RemoteClient & rc, remote_clients) {
ba.clear();
switch (rc.state) {
case FetchingData:
ba << int(0xCC) << tabs;
//piCout << "server send const data" << rc.name << ba.size_s();
break;
case Committing:
ba << int(0xDD);
break;
case Connected:
ba << int(0xEE) << content;
break;
default: break;
}
if (!ba.isEmpty())
peer->send(rc.name, ba);
}
}
PIConsole::RemoteClient & PIConsole::remoteClient(const PIString & fname) {
piForeach (RemoteClient & i, remote_clients)
if (i.name == fname)
return i;
remote_clients << RemoteClient(fname);
return remote_clients.back();
}
void PIConsole::peerReceived(const PIString & from, const PIByteArray & data) {
int type;
PIByteArray ba(data);
ba >> type;
//piCout << "rec packet from" << from << "type" << PICoutManipulators::Hex << type;
if (server_mode) {
if (from.left(5) != "_rcc_") return;
//PIString rcn = from.right(from.length() - 6);
RemoteClient & rc(remoteClient(from));
switch (type) {
case 0xBB: // fetch const data request
//piCout << "fetch data request from" << from << rc.state;
if (rc.state != Connected)
rc.state = FetchingData;
break;
case 0xCC: // const data commit
//piCout << "commit from" << from;
if (rc.state != Connected)
rc.state = Connected;
break;
default: break;
}
} else {
PIVector<VariableContent> content;
PIMap<int, Variable * > vids;
if (from.left(5) != "_rcs_") return;
PIString rcn = from.right(from.length() - 6);
switch (type) {
case 0xAA: // new server
//piCout << "new server" << rcn;
break;
case 0xCC: // const data
//piCout << "received const data";
state = Committing;
ba >> tabs;
cur_tab = tabs.isEmpty() ? -1 : 0;
piForeach (Tab & t, tabs)
piForeach (Column & c, t.columns)
piForeach (Variable & v, c.variables)
v.remote = true;
break;
case 0xDD: // const data commit
//piCout << "received commit";
state = Connected;
break;
case 0xEE: // dynamic data
//piCout << "received data" << ba.size_s();
piForeach (Tab & t, tabs)
piForeach (Column & c, t.columns)
piForeach (Variable & v, c.variables)
if (!v.isEmpty() && v.id > 0)
vids[v.id] = &v;
ba >> content;
piForeach (VariableContent & vc, content) {
if (vc.id <= 0) continue;
Variable * v = vids.at(vc.id);
if (v == 0) continue;
//piCout << "read" << v->name << vc.rdata.size_s();
v->rdata = vc.rdata;
}
break;
default: break;
}
}
}
void PIConsole::peerTimer(void * data, int delim) {
if (peer == 0) return;
if (server_mode) {
if (delim == 20)
serverSendInfo();
else
serverSendData();
} else {
if (delim != 1 || server_name.isEmpty()) return;
const PIPeer::PeerInfo * p = peer->getPeerByName("_rcs_:" + server_name);
if (p == 0) return;
PIByteArray ba;
switch (state) {
case Disconnected:
peer_timer.reset();
ba << int(0xBB);
//piCout << "send to" << server_name << "fetch request disc";
peer->send(p, ba);
state = FetchingData;
break;
case FetchingData:
if (peer_timer.elapsed_s() < 3.)
return;
peer_timer.reset();
ba << int(0xBB);
//piCout << "send to" << server_name << "fetch request fd";
peer->send(p, ba);
break;
case Committing:
peer_timer.reset();
ba << int(0xCC);
//piCout << "send to" << server_name << "committing";
state = Connected;
peer->send(p, ba);
break;
default: break;
};
}
}