PIFile: applyFileInfo
PIPeer: fixed pisd work progress ... git-svn-id: svn://db.shs.com.ru/pip@20 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
18
main.cpp
18
main.cpp
@@ -126,13 +126,17 @@ public:
|
||||
using namespace PIScreenTypes;
|
||||
|
||||
int main (int argc, char * argv[]) {
|
||||
/*PILibrary lib(argv[1]);
|
||||
piCout << lib.isLoaded();
|
||||
piCout << lib.resolve(argv[2]);
|
||||
lib.unload();
|
||||
piCout << lib.load(argv[1]);
|
||||
piCout << lib.resolve(argv[2]);
|
||||
return 0;*/
|
||||
PIFile::FileInfo fi = PIFile::fileInfo(argv[1]);
|
||||
piCout << fi;
|
||||
fi.perm_user = fi.perm_group = fi.perm_other = 7;
|
||||
fi.id_user = 1000;
|
||||
fi.time_access.day--;
|
||||
fi.time_modification.hours++;
|
||||
fi.flags |= PIFile::FileInfo::Hidden;
|
||||
piCout << PIFile::applyFileInfo(fi);
|
||||
fi = PIFile::fileInfo(argv[1]);
|
||||
piCout << fi;
|
||||
return 0;
|
||||
/*if (!(argc == 3 || argc == 4)) {
|
||||
piCout << "UDPFileTransfer";
|
||||
piCout << "USE: piptest [src_ip_port] [dst_ip_port] {filename}";
|
||||
|
||||
@@ -227,6 +227,7 @@ void PIScreen::SystemConsole::print() {
|
||||
}
|
||||
for (int i = 0; i < height; ++i)
|
||||
pcells[i] = cells[i];
|
||||
printf("\e[0m");
|
||||
fflush(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -293,19 +293,35 @@ PIString PIDateTime::toString(const PIString & format) const {
|
||||
|
||||
|
||||
#ifdef WINDOWS
|
||||
PIDateTime::PIDateTime(FILETIME t) {
|
||||
FILETIME lt;
|
||||
FileTimeToLocalFileTime(&t, <);
|
||||
SYSTEMTIME st;
|
||||
FileTimeToSystemTime(<, &st);
|
||||
year = st.wYear;
|
||||
month = st.wMonth;
|
||||
day = st.wDay;
|
||||
hours = st.wHour;
|
||||
minutes = st.wMinute;
|
||||
seconds = st.wSecond;
|
||||
milliseconds = st.wMilliseconds;
|
||||
}
|
||||
PIDateTime::PIDateTime(FILETIME t) {
|
||||
FILETIME lt;
|
||||
SYSTEMTIME st;
|
||||
FileTimeToLocalFileTime(&t, <);
|
||||
FileTimeToSystemTime(<, &st);
|
||||
year = st.wYear;
|
||||
month = st.wMonth;
|
||||
day = st.wDay;
|
||||
hours = st.wHour;
|
||||
minutes = st.wMinute;
|
||||
seconds = st.wSecond;
|
||||
milliseconds = st.wMilliseconds;
|
||||
}
|
||||
|
||||
|
||||
FILETIME PIDateTime::toFILETIME() const {
|
||||
FILETIME lt, ret;
|
||||
SYSTEMTIME st;
|
||||
st.wYear = year;
|
||||
st.wMonth = month;
|
||||
st.wDay = day;
|
||||
st.wHour = hours;
|
||||
st.wMinute = minutes;
|
||||
st.wSecond = seconds;
|
||||
st.wMilliseconds = milliseconds;
|
||||
SystemTimeToFileTime(&st, <);
|
||||
LocalFileTimeToFileTime(<, &ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -224,6 +224,7 @@ struct PIP_EXPORT PIDateTime {
|
||||
PIDateTime(const PIDate & date, const PITime & time) {year = date.year; month = date.month; day = date.day; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
|
||||
#ifdef WINDOWS
|
||||
PIDateTime(FILETIME t);
|
||||
FILETIME toFILETIME() const;
|
||||
#endif
|
||||
int year;
|
||||
int month;
|
||||
|
||||
@@ -218,7 +218,12 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
|
||||
# else
|
||||
const_cast<char*>(p.data()), 0
|
||||
# endif
|
||||
, 0, versionsort);
|
||||
, 0,
|
||||
# ifdef MAC_OS
|
||||
alphasort);
|
||||
# else
|
||||
versionsort);
|
||||
# endif
|
||||
for (int i = 0; i < cnt; ++i) {
|
||||
l << PIFile::fileInfo(dp + PIString(list[i]->d_name));
|
||||
delete list[i];
|
||||
|
||||
@@ -34,7 +34,9 @@
|
||||
# define S_IFSOCK 0x20
|
||||
#else
|
||||
# include <sys/stat.h>
|
||||
# include <sys/time.h>
|
||||
# include <fcntl.h>
|
||||
# include <utime.h>
|
||||
#endif
|
||||
#define S_IFHDN 0x40
|
||||
#ifdef QNX
|
||||
@@ -289,6 +291,20 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
|
||||
ret.size = filesize.QuadPart;
|
||||
ret.time_access = PIDateTime(fi.ftLastAccessTime);
|
||||
ret.time_modification = PIDateTime(fi.ftLastWriteTime);
|
||||
/*PIByteArray sec;
|
||||
DWORD sec_n(0);
|
||||
//SECURITY_DESCRIPTOR sec;
|
||||
GetFileSecurity(path.data(), DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, (SECURITY_DESCRIPTOR*)sec.data(), 0, &sec_n);
|
||||
sec.resize(sec_n);
|
||||
GetFileSecurity(path.data(), DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, (SECURITY_DESCRIPTOR*)sec.data(), sec.size(), &sec_n);
|
||||
errorClear();
|
||||
SID sid; BOOL def;
|
||||
GetSecurityDescriptorGroup((PSECURITY_DESCRIPTOR)sec.data(), &sid, &def);
|
||||
char * s(0);
|
||||
ConvertSidToStringSid((PSID)&sid, s);
|
||||
piCout << s;
|
||||
LocalFree(s);
|
||||
//ret.id_user = ;*/
|
||||
}
|
||||
CloseHandle(hFile);
|
||||
#else
|
||||
@@ -325,3 +341,65 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
|
||||
if (n == "..") ret.flags = FileInfo::Dir | FileInfo::DotDot;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info) {
|
||||
if (path.isEmpty()) return false;
|
||||
PIString fp(path);
|
||||
if (fp.endsWith(PIDir::separator)) fp.pop_back();
|
||||
#ifdef WINDOWS
|
||||
DWORD attr = GetFileAttributes((LPCTSTR)(path.data()));
|
||||
if (attr == 0xFFFFFFFF) return false;
|
||||
attr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY);
|
||||
if (info.isHidden()) attr |= FILE_ATTRIBUTE_HIDDEN;
|
||||
if (!info.perm_user.write) attr |= FILE_ATTRIBUTE_READONLY;
|
||||
if (SetFileAttributes((LPCTSTR)(fp.data()), attr) == 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"SetFileAttributes\" error:" << errorString();
|
||||
return false;
|
||||
}
|
||||
HANDLE hFile = 0;
|
||||
if ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
|
||||
hFile = CreateFile(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
||||
} else {
|
||||
hFile = CreateFile(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
|
||||
}
|
||||
if (!hFile) return false;
|
||||
FILETIME atime = info.time_access.toFILETIME(), mtime = info.time_modification.toFILETIME();
|
||||
if (SetFileTime(hFile, 0, &atime, &mtime) == 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"SetFileTime\" error:" << errorString();
|
||||
return false;
|
||||
}
|
||||
CloseHandle(hFile);
|
||||
#else
|
||||
int mode(0);
|
||||
if (info.perm_user.read) mode |= S_IRUSR;
|
||||
if (info.perm_user.write) mode |= S_IWUSR;
|
||||
if (info.perm_user.exec) mode |= S_IXUSR;
|
||||
if (info.perm_group.read) mode |= S_IRGRP;
|
||||
if (info.perm_group.write) mode |= S_IWGRP;
|
||||
if (info.perm_group.exec) mode |= S_IXGRP;
|
||||
if (info.perm_other.read) mode |= S_IROTH;
|
||||
if (info.perm_other.write) mode |= S_IWOTH;
|
||||
if (info.perm_other.exec) mode |= S_IXOTH;
|
||||
if (chmod(fp.data(), mode) != 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"chmod\" error:" << errorString();
|
||||
return false;
|
||||
}
|
||||
if (chown(fp.data(), info.id_user, info.id_group) != 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"chown\" error:" << errorString();
|
||||
return false;
|
||||
}
|
||||
struct timeval tm[2];
|
||||
PISystemTime st = info.time_access.toSystemTime();
|
||||
tm[0].tv_sec = st.seconds;
|
||||
tm[0].tv_usec = st.nanoseconds / 1000;
|
||||
st = info.time_modification.toSystemTime();
|
||||
tm[1].tv_sec = st.seconds;
|
||||
tm[1].tv_usec = st.nanoseconds / 1000;
|
||||
if (utimes(fp.data(), tm) != 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"utimes\" error:" << errorString();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ public:
|
||||
Permissions(uchar r = 0): raw(r) {}
|
||||
Permissions(bool r, bool w, bool e): raw(0) {read = w; write = w; exec = e;}
|
||||
PIString toString() const {return PIString(read ? "r" : "-") + PIString(write ? "w" : "-") + PIString(exec ? "x" : "-");}
|
||||
operator int() const {return raw;}
|
||||
Permissions & operator =(int v) {raw = v; return *this;}
|
||||
union {
|
||||
uchar raw;
|
||||
struct {
|
||||
@@ -259,6 +261,12 @@ public:
|
||||
//! Returns FileInfo of file or dir with path "path"
|
||||
static FileInfo fileInfo(const PIString & path);
|
||||
|
||||
//! Apply "info" parameters to file or dir with path "path"
|
||||
static bool applyFileInfo(const PIString & path, const FileInfo & info);
|
||||
|
||||
//! Apply "info" parameters to file or dir with path "info".path
|
||||
static bool applyFileInfo(const FileInfo & info) {return applyFileInfo(info.path, info);}
|
||||
|
||||
//! \handlers
|
||||
//! \{
|
||||
|
||||
|
||||
@@ -46,6 +46,18 @@ int PIPeer::PeerInfo::ping() const {
|
||||
}
|
||||
|
||||
|
||||
PIString PIPeer::PeerInfo::fastestAddress() const {
|
||||
double mp = -1.;
|
||||
PIString ret;
|
||||
piForeachC (Address & a, addresses)
|
||||
if (a.ping > 0.) {
|
||||
mp = piMaxd(mp, a.ping);
|
||||
ret = a.address;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIPeer::PIPeer(const PIString & name_): PIObject() {
|
||||
setName(name_);
|
||||
self_info.name = name_;
|
||||
@@ -202,12 +214,13 @@ PIPeer::PeerInfo * PIPeer::quickestPeer(const PIString & to) {
|
||||
PeerInfo * dp = 0;
|
||||
int mping = 0x7FFFFFFF;
|
||||
for (int i = 0; i < tp.size_s(); ++i) {
|
||||
if (mping > tp[i]->ping()) {
|
||||
mping = tp[i]->ping();
|
||||
int p = tp[i]->ping();
|
||||
if (mping > p && p > 0) {
|
||||
mping = p;
|
||||
dp = tp[i];
|
||||
}
|
||||
}
|
||||
//piCout << "*** search quickest peer: found" << dp->name;
|
||||
//piCout << "*** search quickest peer: found" << (dp ? dp->name : "0");
|
||||
return dp;
|
||||
}
|
||||
|
||||
@@ -222,7 +235,7 @@ bool PIPeer::send(const PIString & to, const void * data, int size) {
|
||||
ba << int(4) << self_info.name << to << int(0) << size;
|
||||
PIByteArray fmsg(data, size), cmsg;
|
||||
int msg_count = (size - 1) / _PIPEER_MSG_SIZE + 1;
|
||||
//piCout << "[PIPeer] send" << size << "bytes in" << msg_count << "packets ...";
|
||||
piCout << "[PIPeer] send" << size << "bytes in" << msg_count << "packets ...";
|
||||
for (int i = 0; i < msg_count; ++i) {
|
||||
int csize = (i == msg_count - 1) ? ((size - 1) % _PIPEER_MSG_SIZE + 1) : _PIPEER_MSG_SIZE;
|
||||
cmsg = ba;
|
||||
@@ -230,7 +243,7 @@ bool PIPeer::send(const PIString & to, const void * data, int size) {
|
||||
cmsg.append(fmsg.data(i * _PIPEER_MSG_SIZE), csize);
|
||||
if (!sendToNeighbour(dp, cmsg)) return false;
|
||||
}
|
||||
//piCout << "[PIPeer] send" << size << "bytes ok";
|
||||
piCout << "[PIPeer] send" << size << "bytes ok";
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -242,6 +255,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
|
||||
PIString from, to;
|
||||
ba >> type;
|
||||
PIMutexLocker locker(eth_mutex);
|
||||
PIMutexLocker plocker(peers_mutex);
|
||||
//piCout << "[PIPeer \"" + name_ + "\"] Received packet" << type;
|
||||
if (type == 5) { // ping
|
||||
PIString addr;
|
||||
@@ -259,7 +273,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
|
||||
a.wait_ping = false;
|
||||
if (a.ping < 0) a.ping = ptime.toMilliseconds();
|
||||
else a.ping = 0.6 * a.ping + 0.4 * ptime.toMilliseconds();
|
||||
piCout << "*** ping echo" << p.name << a.address << a.ping;
|
||||
//piCout << "*** ping echo" << p.name << a.address << a.ping;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -348,7 +362,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
|
||||
case 1: // new peer
|
||||
//piCout << "new peer packet ...";
|
||||
peers_mutex.lock();
|
||||
if (hasPeer(pi.name)) {
|
||||
if (!hasPeer(pi.name)) {
|
||||
ba >> pi;
|
||||
pi.sync = 0;
|
||||
if (pi.dist == 0) {
|
||||
@@ -397,27 +411,30 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
|
||||
if (rpeer.name == self_info.name) continue;
|
||||
bool exist = false;
|
||||
piForeach (PeerInfo & peer, peers) {
|
||||
if (peer.name == rpeer.name) exist = true;
|
||||
if (exist && isPeerRecent(peer, rpeer)) {
|
||||
//piCout << "synced " << peer.name;
|
||||
for (int z = 0; z < rpeer.addresses.size_s(); ++z) {
|
||||
PeerInfo::Address & ra(rpeer.addresses[z]);
|
||||
for (int k = 0; k < peer.addresses.size_s(); ++k) {
|
||||
PeerInfo::Address & a(peer.addresses[k]);
|
||||
if (ra.address == a.address) {
|
||||
ra.ping = a.ping;
|
||||
ra.wait_ping = a.wait_ping;
|
||||
ra.last_ping = a.last_ping;
|
||||
piBreak;
|
||||
if (peer.name == rpeer.name) {
|
||||
exist = true;
|
||||
if (isPeerRecent(peer, rpeer)) {
|
||||
//piCout << "synced " << peer.name;
|
||||
for (int z = 0; z < rpeer.addresses.size_s(); ++z) {
|
||||
PeerInfo::Address & ra(rpeer.addresses[z]);
|
||||
for (int k = 0; k < peer.addresses.size_s(); ++k) {
|
||||
PeerInfo::Address & a(peer.addresses[k]);
|
||||
if (ra.address == a.address) {
|
||||
ra.ping = a.ping;
|
||||
ra.wait_ping = a.wait_ping;
|
||||
ra.last_ping = a.last_ping;
|
||||
piBreak;
|
||||
}
|
||||
}
|
||||
}
|
||||
peer.was_update = true;
|
||||
peer.addresses = rpeer.addresses;
|
||||
peer.cnt = rpeer.cnt;
|
||||
peer.time = rpeer.time;
|
||||
peer.addNeighbours(rpeer.neighbours);
|
||||
rpeer.neighbours = peer.neighbours;
|
||||
if (peer.name == pi.name) peer.sync = 0;
|
||||
}
|
||||
peer.addresses = rpeer.addresses;
|
||||
peer.cnt = rpeer.cnt;
|
||||
peer.time = rpeer.time;
|
||||
peer.addNeighbours(rpeer.neighbours);
|
||||
rpeer.neighbours = peer.neighbours;
|
||||
if (peer.name == pi.name) peer.sync = 0;
|
||||
piBreak;
|
||||
}
|
||||
}
|
||||
@@ -430,8 +447,6 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
|
||||
}
|
||||
//piCout << "***";
|
||||
//piCout << self_info.name << self_info.neighbours;
|
||||
if (ch)
|
||||
findNearestAddresses();
|
||||
piForeach (PeerInfo & i, peers) {
|
||||
if (i.dist == 0) {
|
||||
self_info.addNeighbour(i.name);
|
||||
@@ -439,6 +454,8 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
|
||||
}
|
||||
//piCout << i.name << i.neighbours;
|
||||
}
|
||||
if (ch)
|
||||
findNearestAddresses();
|
||||
peers_mutex.unlock();
|
||||
//piCoutObj << "after sync " << peers.size_s() << " peers";
|
||||
break;
|
||||
@@ -449,9 +466,10 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
|
||||
|
||||
bool PIPeer::sendToNeighbour(PIPeer::PeerInfo * peer, const PIByteArray & ba) {
|
||||
//if (peer->_neth == 0) return false;
|
||||
piCout << "[PIPeer] sendToNeighbour" << peer->name << peer->_naddress << ba.size_s() << "bytes ...";
|
||||
PIString addr = peer->fastestAddress();
|
||||
piCout << "[PIPeer] sendToNeighbour" << peer->name << addr << ba.size_s() << "bytes ...";
|
||||
//bool ok = peer->_neth->send(peer->_naddress, ba.data(), ba.size_s());
|
||||
bool ok = eth_send.send(peer->_naddress, ba);
|
||||
bool ok = eth_send.send(addr, ba);
|
||||
//piCout << "[PIPeer] sendToNeighbour" << (ok ? "ok" : "fail");
|
||||
if (ok) diag_d.sended(ba.size_s());
|
||||
return ok;
|
||||
@@ -504,7 +522,7 @@ void PIPeer::pingNeighbours() {
|
||||
ba << int(5) << self_info.name;
|
||||
//piCout << "pingNeighbours" << peers.size();
|
||||
piForeach (PeerInfo & p, peers) {
|
||||
//piCout << " ping neighbour" << p.name;
|
||||
piCout << " ping neighbour" << p.name << p.ping();
|
||||
if (!p.isNeighbour()) continue;
|
||||
piForeach (PeerInfo::Address & a, p.addresses) {
|
||||
//piCout << " address" << a.address << a.wait_ping;
|
||||
@@ -528,7 +546,7 @@ void PIPeer::syncPeers() {
|
||||
peers_mutex.lock();
|
||||
for (uint i = 0; i < peers.size(); ++i) {
|
||||
PeerInfo & cp(peers[i]);
|
||||
if (cp.sync > 3 && cp.dist == 0) {
|
||||
if (cp.sync > 3) {
|
||||
pn = cp.name;
|
||||
//piCoutObj << "sync: remove " << pn;
|
||||
addToRemoved(cp);
|
||||
@@ -543,7 +561,11 @@ void PIPeer::syncPeers() {
|
||||
change = true;
|
||||
continue;
|
||||
}
|
||||
cp.sync++;
|
||||
if (cp.was_update)
|
||||
cp.sync = 0;
|
||||
else
|
||||
cp.sync++;
|
||||
cp.was_update = false;
|
||||
}
|
||||
pingNeighbours();
|
||||
if (change) findNearestAddresses();
|
||||
@@ -600,7 +622,7 @@ void PIPeer::findNearestAddresses() {
|
||||
piForeach (PIEthernet * e, eths_traffic)
|
||||
if (e->readAddress() == ma) {
|
||||
i._neth = e;
|
||||
break;
|
||||
piBreak;
|
||||
}
|
||||
//piCout << i.name << i._naddress;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
friend PIByteArray & operator <<(PIByteArray & s, const PIPeer::PeerInfo & v);
|
||||
friend PIByteArray & operator >>(PIByteArray & s, PIPeer::PeerInfo & v);
|
||||
public:
|
||||
PeerInfo() {dist = sync = cnt = 0; _neth = 0; _first = 0;}
|
||||
PeerInfo() {dist = sync = cnt = 0; _neth = 0; _first = 0; was_update = false;}
|
||||
|
||||
struct Address {
|
||||
Address(const PIString & a = PIString(), const PIString & m = "255.255.255.0");
|
||||
@@ -70,6 +70,7 @@ public:
|
||||
|
||||
bool isNeighbour() const {return dist == 0;}
|
||||
int ping() const;
|
||||
PIString fastestAddress() const;
|
||||
|
||||
protected:
|
||||
void addNeighbour(const PIString & n) {if (!neighbours.contains(n)) neighbours << n;}
|
||||
@@ -79,6 +80,7 @@ public:
|
||||
PIString nearest_address;
|
||||
PIStringList neighbours;
|
||||
int sync, cnt;
|
||||
bool was_update;
|
||||
PISystemTime time;
|
||||
PIString _naddress;
|
||||
PIEthernet * _neth;
|
||||
|
||||
@@ -2,89 +2,121 @@
|
||||
#include "shared.h"
|
||||
#include "pisysteminfo.h"
|
||||
|
||||
extern PIScreen screen;
|
||||
|
||||
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(&screen, keyPressed, this, keyEvent)
|
||||
CONNECTU(&timer, tickEvent, this, timerEvent)
|
||||
timer.addDelimiter(5);
|
||||
timer.start(200);
|
||||
|
||||
//CONNECTU(&console, keyPressed, this, keyEvent)
|
||||
//dir.setDir("/home/peri4/Documents");
|
||||
//TileSimple * tl;
|
||||
|
||||
tile_root = new PIScreenTile();
|
||||
tile_root->direction = Vertical;
|
||||
|
||||
tile_header = new TileSimple("daemon header");
|
||||
tile_header->size_policy = Fixed;
|
||||
|
||||
list_daemons = new TileList("daemons list");
|
||||
list_daemons->hide();
|
||||
|
||||
list_actions = new TileList("actions list");
|
||||
list_actions->hide();
|
||||
list_actions->content << TileList::Row("Information", CellFormat());
|
||||
list_actions->content << TileList::Row("File manager", CellFormat());
|
||||
list_actions->content << TileList::Row("Shell", CellFormat());
|
||||
|
||||
tile_info = new TileSimple("daemon info");
|
||||
tile_info->hide();
|
||||
|
||||
tile_root->addTile(tile_header);
|
||||
tile_root->addTile(list_daemons);
|
||||
tile_root->addTile(list_actions);
|
||||
tile_root->addTile(tile_info);
|
||||
|
||||
CONNECTU(&screen, tileEvent, this, tileEvent)
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIScreenTile * Daemon::tile() const {
|
||||
return new PIScreenTile();
|
||||
return tile_root;
|
||||
}
|
||||
|
||||
|
||||
void Daemon::hideAll() {
|
||||
list_actions->hide();
|
||||
tile_info->hide();
|
||||
list_daemons->hide();
|
||||
}
|
||||
|
||||
|
||||
void Daemon::showTile(PIScreenTile * t, const PIString & header) {
|
||||
hideAll();
|
||||
t->show();
|
||||
t->setFocus();
|
||||
tile_header->content.resize(1);
|
||||
tile_header->content[0].first = header;
|
||||
tile_header->content[0].second.flags = Bold;
|
||||
if (!conn_name.isEmpty())
|
||||
tile_header->content[0].first.insert(0, "Daemon \"" + connectedDaemon() + "\": ");
|
||||
}
|
||||
|
||||
|
||||
void Daemon::fillInfoTile(const Daemon::HostInfo & hi) {
|
||||
screen.lock();
|
||||
tile_info->content.clear();
|
||||
tile_info->content << TileSimple::Row("Exec command: " + hi.execCommand, CellFormat());
|
||||
tile_info->content << TileSimple::Row(" Executed on " + hi.execDateTime.toString(), CellFormat());
|
||||
tile_info->content << TileSimple::Row(" Hostname: " + hi.hostname, CellFormat());
|
||||
tile_info->content << TileSimple::Row(" Username: " + hi.user, CellFormat());
|
||||
tile_info->content << TileSimple::Row(" OS name: " + hi.OS_name, CellFormat());
|
||||
tile_info->content << TileSimple::Row(" OS version: " + hi.OS_version, CellFormat());
|
||||
tile_info->content << TileSimple::Row("Architecture: " + hi.architecture, CellFormat());
|
||||
tile_info->content << TileSimple::Row(" CPU count: " + PIString::fromNumber(hi.processorsCount), CellFormat());
|
||||
screen.unlock();
|
||||
}
|
||||
|
||||
|
||||
void Daemon::tileEvent(PIScreenTile * t, TileEvent e) {
|
||||
if (t == list_daemons) {
|
||||
if (e.type == TileList::RowPressed) {
|
||||
connectToDaemon(list_daemons->content[e.data.toInt()].first);
|
||||
showActionList();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (t == list_actions) {
|
||||
if (e.type == TileList::RowPressed) {
|
||||
switch (e.data.toInt()) {
|
||||
case 0: mode = 2; showTile(tile_info, "Information"); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
if (!tile_root->visible) return;
|
||||
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();
|
||||
screen.lock();
|
||||
tile_info->content.clear();
|
||||
screen.unlock();
|
||||
showActionList();
|
||||
} else
|
||||
disconnect();
|
||||
}
|
||||
@@ -95,16 +127,17 @@ void Daemon::keyEvent(PIKbdListener::KeyEvent key) {
|
||||
|
||||
|
||||
void Daemon::timerEvent(void * _d, int delim) {
|
||||
if (!enabled) return;
|
||||
if (delim == 1) {
|
||||
if (mode == 0)
|
||||
updateConsole();
|
||||
|
||||
}
|
||||
if (delim == 5) {
|
||||
screen.lock();
|
||||
list_daemons->content.clear();
|
||||
availableDaemons();
|
||||
piForeachC (PIString & i, available_daemons)
|
||||
list_daemons->content << TileList::Row(i, CellFormat());
|
||||
screen.unlock();
|
||||
if (delim == 5 && mode == 2) {
|
||||
if (conn_name.isEmpty()) return;
|
||||
PIByteArray ba; ba << int(RequestHostInfo);
|
||||
send(conn_name, ba);
|
||||
//std::cout << "send " << std::hex << ba;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,14 +156,13 @@ 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();
|
||||
showMainList();
|
||||
}
|
||||
|
||||
|
||||
@@ -139,10 +171,16 @@ PIString Daemon::connectedDaemon() const {
|
||||
}
|
||||
|
||||
|
||||
void Daemon::peerConnected(const PIString & name) {
|
||||
/*piCout << "connected" << name;
|
||||
mode = 2;
|
||||
conn_name = name;*/
|
||||
}
|
||||
|
||||
|
||||
void Daemon::peerDisconnected(const PIString & name) {
|
||||
if (name == conn_name) {
|
||||
conn_name.clear();
|
||||
mode = 0;
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +190,7 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) {
|
||||
if (data.size() < 4) return;
|
||||
PIByteArray ba(data), rba;
|
||||
int type; ba >> type;
|
||||
//piCout << "rec from" << from << type;
|
||||
//std::cout << "rec from " << from << type;
|
||||
switch (type) {
|
||||
case RequestHostInfo:
|
||||
makeMyHostInfo();
|
||||
@@ -161,6 +199,7 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) {
|
||||
case ReplyHostInfo:
|
||||
ba >> info_other;
|
||||
makeOtherHostInfo();
|
||||
fillInfoTile(info_other);
|
||||
break;
|
||||
};
|
||||
if (!rba.isEmpty()) send(from, rba);
|
||||
@@ -200,7 +239,6 @@ void Daemon::makeOtherHostInfo() {
|
||||
|
||||
|
||||
void Daemon::updateConsole() {
|
||||
if (!enabled) return;
|
||||
switch (mode) {
|
||||
case 0: tabConnect(); break;
|
||||
case 1: tabFeature(); break;
|
||||
|
||||
@@ -13,6 +13,11 @@ public:
|
||||
Daemon();
|
||||
|
||||
struct HostInfo {
|
||||
HostInfo() {
|
||||
processorsCount = ID = threads = priority = -1;
|
||||
physical_memsize = share_memsize = 0;
|
||||
cpu_load_system = cpu_load_user = 0.;
|
||||
}
|
||||
PIString execCommand;
|
||||
PIString hostname;
|
||||
PIString user;
|
||||
@@ -31,14 +36,15 @@ public:
|
||||
float cpu_load_user;
|
||||
};
|
||||
|
||||
void enable() {enabled = true;}
|
||||
void disable() {enabled = false;}
|
||||
void showMainList() {showTile(list_daemons, "Select daemon");}
|
||||
void showActionList() {showTile(list_actions, "Select action");}
|
||||
|
||||
PIStringList availableDaemons() const;
|
||||
void connectToDaemon(const PIString & dn);
|
||||
void disconnect();
|
||||
|
||||
PIString connectedDaemon() const;
|
||||
PIString thisDaemonName() const {return selfInfo().name.mid(6);}
|
||||
|
||||
PIScreenTile * tile() const;
|
||||
|
||||
@@ -50,9 +56,14 @@ private:
|
||||
|
||||
};
|
||||
|
||||
EVENT_HANDLER2(void, tileEvent, PIScreenTile *, t, PIScreenTypes::TileEvent, e);
|
||||
EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, key);
|
||||
EVENT_HANDLER2(void, timerEvent, void * , _d, int, delim);
|
||||
EVENT(menuRequest);
|
||||
void hideAll();
|
||||
void showTile(PIScreenTile * t, const PIString & header = PIString());
|
||||
void fillInfoTile(const HostInfo & hi);
|
||||
void peerConnected(const PIString & name);
|
||||
void peerDisconnected(const PIString & name);
|
||||
void dataReceived(const PIString & from, const PIByteArray & data);
|
||||
void makeMyHostInfo();
|
||||
@@ -69,7 +80,9 @@ private:
|
||||
PIMap<int, PIString> dnames;
|
||||
PISystemMonitor sys_mon_other;
|
||||
HostInfo info_my, info_other;
|
||||
bool enabled;
|
||||
PIScreenTile * tile_root;
|
||||
TileSimple * tile_info, * tile_header;
|
||||
TileList * list_daemons, * list_actions;
|
||||
int mode, offset, cur, height;
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "file_manager.h"
|
||||
#include "shared.h"
|
||||
|
||||
|
||||
extern PIScreen screen;
|
||||
|
||||
FileManager::TileDir::TileDir(): TileList() {
|
||||
label_path = 0;
|
||||
@@ -132,8 +132,8 @@ void FileManager::TileDir::resizeEvent(int w, int h) {
|
||||
|
||||
FileManager::FileManager() {
|
||||
setName("FileManager");
|
||||
enabled = del_commit = false;
|
||||
//CONNECTU(&console, keyPressed, this, keyEvent)
|
||||
del_commit = false;
|
||||
CONNECTU(&screen, keyPressed, this, keyEvent)
|
||||
//dir.setDir("/home/peri4/Documents");
|
||||
TileSimple * tl;
|
||||
|
||||
@@ -183,6 +183,13 @@ PIScreenTile * FileManager::tile() const {
|
||||
|
||||
|
||||
void FileManager::keyEvent(PIKbdListener::KeyEvent key) {
|
||||
if (!tile_root->visible) return;
|
||||
switch (key.key) {
|
||||
case PIKbdListener::Esc:
|
||||
menuRequest();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
/*if (!enabled) return;
|
||||
if (key.key == 'D') {
|
||||
if (cur >= files.size_s() || cur < 0) return;
|
||||
|
||||
@@ -33,7 +33,7 @@ private:
|
||||
EVENT(menuRequest);
|
||||
void updateConsole();
|
||||
|
||||
bool enabled, del_commit;
|
||||
bool del_commit;
|
||||
TileDir * panels[2];
|
||||
PIScreenTile * tile_root;
|
||||
PIStringList selected;
|
||||
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
cur_peer = -1;
|
||||
TileSimple * tile = new TileSimple("title");
|
||||
tile->content << TileSimple::Row("pisd (PI System Daemon, PIP version " + PIPVersion() + ")", CellFormat(Black, Transparent));
|
||||
tile->content << TileSimple::Row("This daemon: \"" + daemon_.thisDaemonName() + "\"", CellFormat(Black, Transparent));
|
||||
tile->back_format.color_back = Yellow;
|
||||
tile->size_policy = Fixed;
|
||||
screen.rootTile()->addTile(tile);
|
||||
@@ -131,27 +132,28 @@ public:
|
||||
, CellFormat());
|
||||
piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers())
|
||||
peers_tl->content << TileList::Row(p.name + " | " + PIString::fromNumber(p.dist) +
|
||||
" | " + PIString::fromNumber(p.ping()) +
|
||||
" | " + PIString::fromNumber(p.addresses.size_s()) +
|
||||
" | " + PIString::fromBool(p.isNeighbour())
|
||||
, CellFormat());
|
||||
" | " + PIString::fromNumber(p.ping()) +
|
||||
" | " + PIString::fromNumber(p.addresses.size_s()) +
|
||||
" | " + 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());
|
||||
piForeachC(PIPeer::PeerInfo::Address &a , pi.addresses)
|
||||
addrs_tl->content << TileList::Row(a.address + " | " + a.netmask +
|
||||
" | " + PIString::fromNumber(a.ping) +
|
||||
" | " + PIString::fromNumber(a.last_ping.toSeconds()) +
|
||||
" | " + PIString::fromBool(a.isAvailable()), CellFormat());
|
||||
typedef PIPair<PIString, PIVector <PIPeer::PeerInfo* > > PeerPair;
|
||||
PIStringList peermap;
|
||||
" | " + PIString::fromNumber(a.ping) +
|
||||
" | " + PIString::fromNumber(a.last_ping.toSeconds()) +
|
||||
" | " + PIString::fromBool(a.isAvailable()), CellFormat());
|
||||
typedef PIPair<PIString, PIVector <PIPeer::PeerInfo* > > PeerPair;
|
||||
PIStringList peermap;
|
||||
piForeachC(PeerPair &p , daemon_._peerMap()) {
|
||||
PIString s = p.first + " | ";
|
||||
piForeachC(PIPeer::PeerInfo * pp, p.second) s += "->" + pp->name;
|
||||
peermap << s;
|
||||
}
|
||||
piForeachC(PIString &s , peermap)
|
||||
peers_tl->content << TileList::Row(s, CellFormat());
|
||||
peermap_tl->content << TileList::Row(s, CellFormat());
|
||||
screen.unlock();
|
||||
}
|
||||
void tick(void* data_, int delimiter) {
|
||||
@@ -171,8 +173,8 @@ public:
|
||||
switch (e.data.toInt()) {
|
||||
case 0: tinfo->show(); break;
|
||||
case 1: tfm->show(); break;
|
||||
case 2: tdaemon->show(); break;
|
||||
case 3: tpeer->show(); break;
|
||||
case 2: daemon_.showMainList(); tdaemon->show(); break;
|
||||
case 3: tpeer->show(); peers_tl->setFocus(); break;
|
||||
case 4: PIKbdListener::exiting = true; break;
|
||||
}
|
||||
}
|
||||
@@ -187,7 +189,8 @@ public:
|
||||
}
|
||||
}
|
||||
EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, e) {
|
||||
if (e.key == PIKbdListener::Esc) menuRequest();
|
||||
if (tpeer->visible || tinfo->visible)
|
||||
if (e.key == PIKbdListener::Esc) menuRequest();
|
||||
//piCout << "key" << e.key;
|
||||
}
|
||||
PIScreenTile * tmenu, * tinfo, * tfm, * tdaemon, * tpeer;
|
||||
|
||||
Reference in New Issue
Block a user