From 476958706ff341d3369f07dd12ca1920d5343f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Sun, 19 Apr 2015 19:01:46 +0000 Subject: [PATCH] PIPeer important fix! git-svn-id: svn://db.shs.com.ru/pip@110 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- src/console/piscreen.cpp | 3 ++ src/console/piscreendrawer.cpp | 90 ++++++++++++++++++++++++++++++++++ src/console/piscreendrawer.h | 8 ++- src/console/piscreentiles.cpp | 2 +- src/io/pibasetransfer.cpp | 5 +- src/io/pipeer.cpp | 19 ++++--- src/io/pipeer.h | 4 +- src/thread/pimutex.cpp | 16 +++++- src/thread/pimutex.h | 4 ++ utils/system_daemon/daemon.cpp | 4 +- utils/system_daemon/daemon.h | 3 ++ utils/system_daemon/main.cpp | 7 +-- 12 files changed, 147 insertions(+), 18 deletions(-) diff --git a/src/console/piscreen.cpp b/src/console/piscreen.cpp index adb99d55..dad0df7f 100644 --- a/src/console/piscreen.cpp +++ b/src/console/piscreen.cpp @@ -507,6 +507,9 @@ void PIScreen::run() { tile_dialog->width = sw; tile_dialog->height = sh; tile_dialog->layout(); + int dx = tile_dialog->x - 1, dy = tile_dialog->y - 1, dw = tile_dialog->width, dh = tile_dialog->height; + drawer_.drawFrame(dx, dy, dx + dw + 1, dy + dh + 1, (Color)tile_dialog->back_format.color_char, + (Color)tile_dialog->back_format.color_back, (CharFlags)tile_dialog->back_format.flags); tile_dialog->drawEventInternal(&drawer_); } console.print(); diff --git a/src/console/piscreendrawer.cpp b/src/console/piscreendrawer.cpp index f0ee8f0c..abe0d174 100644 --- a/src/console/piscreendrawer.cpp +++ b/src/console/piscreendrawer.cpp @@ -46,6 +46,58 @@ using namespace PIScreenTypes; +PIScreenDrawer::PIScreenDrawer(PIVector > & c): cells(c) { + arts_[LineVertical] = +#ifdef PIP_ICU + PIChar::fromUTF8("│"); +#else + PIChar('|'); +#endif + + arts_[LineHorizontal] = +#ifdef PIP_ICU + PIChar::fromUTF8("─"); +#else + PIChar('-'); +#endif + + arts_[Cross] = +#ifdef PIP_ICU + PIChar::fromUTF8("┼"); +#else + PIChar('+'); +#endif + + arts_[CornerTopLeft] = +#ifdef PIP_ICU + PIChar::fromUTF8("┌"); +#else + PIChar('+'); +#endif + + arts_[CornerTopRight] = +#ifdef PIP_ICU + PIChar::fromUTF8("┐"); +#else + PIChar('+'); +#endif + + arts_[CornerBottomLeft] = +#ifdef PIP_ICU + PIChar::fromUTF8("└"); +#else + PIChar('+'); +#endif + + arts_[CornerBottomRight] = +#ifdef PIP_ICU + PIChar::fromUTF8("┘"); +#else + PIChar('+'); +#endif +} + + void PIScreenDrawer::clear() { for (int i = 0; i < cells.size_s(); ++i) cells[i].fill(Cell()); @@ -129,6 +181,44 @@ void PIScreenDrawer::drawRect(int x0, int y0, int x1, int y1, const PIChar & c, } +void PIScreenDrawer::drawFrame(int x0, int y0, int x1, int y1, Color col_char, Color col_back, CharFlags flags_char) { + if (x0 == x1 && y0 == y1) return; + Cell cc; + cc.format.color_char = col_char; + cc.format.color_back = col_back; + cc.format.flags = flags_char; + int dx = x0 < x1 ? 1 : -1; + int dy = y0 < y1 ? 1 : -1; + int xs[2] = {x0, x1}; + int ys[2] = {y0, y1}; + for (int k = 0; k < 2; ++k) { + int j = ys[k]; + if (j >= 0 && j < height) { + PIVector & cv(cells[j]); + cc.symbol = artChar(LineHorizontal); + for (int i = x0 + 1; i != x1; i += dx) + if (i >= 0 && i < width) + cv[i] = cc; + } + j = xs[k]; + if (j >= 0 && j < width) { + cc.symbol = artChar(LineVertical); + for (int i = y0 + 1; i != y1; i += dy) + if (i >= 0 && i < height) + cells[i][j] = cc; + } + } + int i = x0, j = y0; cc.symbol = artChar(CornerTopLeft); + if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; + i = x1, j = y0; cc.symbol = artChar(CornerTopRight); + if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; + i = x0, j = y1; cc.symbol = artChar(CornerBottomLeft); + if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; + i = x1, j = y1; cc.symbol = artChar(CornerBottomRight); + if (i >= 0 && i < width && j >= 0 && j < height) cells[j][i] = cc; +} + + void PIScreenDrawer::fillRect(int x0, int y0, int x1, int y1, const PIChar & c, Color col_char, Color col_back, CharFlags flags_char) { if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char); Cell cc; diff --git a/src/console/piscreendrawer.h b/src/console/piscreendrawer.h index 14351e05..5cd5cdf3 100644 --- a/src/console/piscreendrawer.h +++ b/src/console/piscreendrawer.h @@ -29,19 +29,25 @@ class PIP_EXPORT PIScreenDrawer { friend class PIScreen; - PIScreenDrawer(PIVector > & c): cells(c) {} + PIScreenDrawer(PIVector > & c); public: + enum ArtChar {LineVertical = 1, LineHorizontal, Cross, CornerTopLeft, CornerTopRight, CornerBottomLeft, CornerBottomRight}; + void clear(); void clearRect(int x0, int y0, int x1, int y1) {fillRect(x0, y0, x1, y1, ' ');} void drawPixel(int x, int y, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0); void drawLine(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0); void drawRect(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0); + void drawFrame(int x0, int y0, int x1, int y1, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0); void drawText(int x, int y, const PIString & s, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Transparent, PIScreenTypes::CharFlags flags_char = 0); void fillRect(int x0, int y0, int x1, int y1, const PIChar & c, PIScreenTypes::Color col_char = PIScreenTypes::Default, PIScreenTypes::Color col_back = PIScreenTypes::Default, PIScreenTypes::CharFlags flags_char = 0); + PIChar artChar(const ArtChar type) const {return arts_.value(type, PIChar(' '));} + private: PIVector > & cells; int width, height; + PIMap arts_; }; diff --git a/src/console/piscreentiles.cpp b/src/console/piscreentiles.cpp index 9718bd02..b15bb016 100644 --- a/src/console/piscreentiles.cpp +++ b/src/console/piscreentiles.cpp @@ -88,7 +88,6 @@ TileList::TileList(const PIString & n): PIScreenTile(n) { focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab; lhei = offset = cur = 0; selection_mode = NoSelection; - vert_line = PIChar::fromUTF8("│"); } @@ -101,6 +100,7 @@ void TileList::sizeHint(int & w, int & h) const { void TileList::drawEvent(PIScreenDrawer * d) { + vert_line = d->artChar(PIScreenDrawer::LineVertical); lhei = height - 2; //int osp = piMini(3, lhei / 4); int is = piClampi(offset, 0, piMaxi(0, content.size_s() - 1)), ie = piClampi(offset + lhei, 0, content.size_s()); diff --git a/src/io/pibasetransfer.cpp b/src/io/pibasetransfer.cpp index 713c828c..106e1097 100644 --- a/src/io/pibasetransfer.cpp +++ b/src/io/pibasetransfer.cpp @@ -98,7 +98,10 @@ void PIBaseTransfer::received(PIByteArray data) { } if (is_receiving && h.id == 0) { // if (checkSession() == 0 && pt == pt_ReplySuccess) finish_receive(true); - if (checkSession() == 0 && pt == pt_ReplySuccess) { piCoutObj << "Success receive"; finish_receive(true);} + if (checkSession() == 0 && pt == pt_ReplySuccess) { + //piCoutObj << "Success receive"; + finish_receive(true); + } } break; case pt_Break: diff --git a/src/io/pipeer.cpp b/src/io/pipeer.cpp index 335a8657..2357328f 100755 --- a/src/io/pipeer.cpp +++ b/src/io/pipeer.cpp @@ -50,7 +50,7 @@ PIPeer::PeerData::~PeerData() { void PIPeer::PeerData::dtThread() { - //piCoutObj << "send DT ..."; + // << "send DT ..."; dt_out.send(data); //piCoutObj << "send DT done"; } @@ -71,8 +71,9 @@ void PIPeer::PeerData::receivedPacket(uchar type, const PIByteArray & d) { dt = &dt_in; if (type == 2) dt = &dt_out; - //piCoutObj << "DT received" << int(type) << d.size_s(); + //piCoutObj << "DT received" << int(type) << d.size_s() << "..."; if (dt) dt->received(d); + //piCoutObj << "DT received" << int(type) << d.size_s() << "done"; } @@ -184,7 +185,6 @@ void PIPeer::initEths(PIStringList al) { ce->setDebug(false); ce->setName("__S__PIPeer_traffic_eth_rec_" + a); ce->setParameters(0); - ce->setThreadSafe(true); bool ok = false; for (int p = _PIPEER_TRAFFIC_PORT_S; p < _PIPEER_TRAFFIC_PORT_E; ++p) { ce->setReadAddress(a, p); @@ -370,7 +370,7 @@ bool PIPeer::dataRead(uchar * readed, int size) { if (pi) { if (pi->isNeighbour()) { sba << int(6) << to << from << addr << time; - //piCout << "ping from" << from << addr << ", send back to" << pi->name; + //piCout << " ping from" << from << addr << ", send back to" << pi->name; send_mutex.lock(); piForeachC (PeerInfo::Address & a, pi->addresses) { if (eth_send.send(a.address, sba)) @@ -400,7 +400,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; } } @@ -445,7 +445,7 @@ bool PIPeer::dataRead(uchar * readed, int size) { cnt++; if (cnt > _PIPEER_MSG_TTL || from == dp->name) return true; sba << type << from << to << cnt << pba; - //piCout << "translate packet" << from << "->" << to << ", ttl =" << cnt; + piCout << "translate packet" << from << "->" << to << ", ttl =" << cnt; sendToNeighbour(dp, sba); } return true; @@ -526,6 +526,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) { //piCoutObj << "rec sync " << rpeers.size_s() << " peers"; peers_mutex.lock(); if (!self_info.neighbours.contains(pi.name)) { + //piCoutObj << "add new nei to me" << pi.name; self_info.addNeighbour(pi.name); PeerInfo * np = peers_map.value(pi.name); if (np) { @@ -569,6 +570,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) { } if (exist || isRemoved(rpeer)) continue; rpeer.dist++; + if (rpeer.name == pi.name) rpeer.dist = 0; rpeer.resetPing(); addPeer(rpeer); ch = true; @@ -679,10 +681,10 @@ void PIPeer::sendPeerRemove(const PIString & peer) { void PIPeer::pingNeighbours() { PIByteArray ba, sba; ba << int(5) << self_info.name; - //piCout << "pingNeighbours" << peers.size(); + //piCout << "*** pingNeighbours" << peers.size() << "..."; piForeach (PeerInfo & p, peers) { - //piCout << " ping neighbour" << p.name << p.ping(); if (!p.isNeighbour()) continue; + //piCout << " ping neighbour" << p.name << p.ping(); send_mutex.lock(); piForeach (PeerInfo::Address & a, p.addresses) { //piCout << " address" << a.address << a.wait_ping; @@ -700,6 +702,7 @@ void PIPeer::pingNeighbours() { } send_mutex.unlock(); } + //piCout << "*** pingNeighbours" << peers.size() << "done"; } diff --git a/src/io/pipeer.h b/src/io/pipeer.h index d3451540..379c5277 100755 --- a/src/io/pipeer.h +++ b/src/io/pipeer.h @@ -144,7 +144,7 @@ protected: EVENT_HANDLER2(bool, dataRead, uchar *, readed, int, size); EVENT_HANDLER2(bool, mbcastRead, uchar *, readed, int, size); - + private: EVENT_HANDLER2(void, timerEvent, void * , data, int, delim); EVENT_HANDLER2(bool, sendInternal, const PIString &, to, const PIByteArray &, data); @@ -179,6 +179,8 @@ private: // 1 - new peer, 2 - remove peer, 3 - sync peers, 4 - data, 5 - ping request, 6 - ping reply // Data packet: 4, from, to, ticks, data_size, data +protected: + typedef PIPair > napair; PIVector eths_traffic, eths_mcast, eths_bcast; diff --git a/src/thread/pimutex.cpp b/src/thread/pimutex.cpp index 7bd17cec..bbbf58ce 100755 --- a/src/thread/pimutex.cpp +++ b/src/thread/pimutex.cpp @@ -47,6 +47,7 @@ PIMutex::PIMutex() { pthread_mutex_init(&mutex, &attr); pthread_mutexattr_destroy(&attr); #endif + locked = false; } @@ -56,6 +57,7 @@ PIMutex::~PIMutex() { #else pthread_mutex_destroy(&mutex); #endif + locked = false; } @@ -65,6 +67,7 @@ void PIMutex::lock() { #else pthread_mutex_lock(&mutex); #endif + locked = true; } @@ -74,13 +77,22 @@ void PIMutex::unlock() { #else pthread_mutex_unlock(&mutex); #endif + locked = false; } bool PIMutex::tryLock() { + bool ret = #ifdef WINDOWS - return (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0); + (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0); #else - return (pthread_mutex_trylock(&mutex) == 0); + (pthread_mutex_trylock(&mutex) == 0); #endif + locked = true; + return ret; +} + + +bool PIMutex::isLocked() const { + return locked; } diff --git a/src/thread/pimutex.h b/src/thread/pimutex.h index 58f0aef8..bd2d09f6 100755 --- a/src/thread/pimutex.h +++ b/src/thread/pimutex.h @@ -49,6 +49,9 @@ public: //! If mutex is already locked function returns immediate an returns "false" bool tryLock(); + //! Returns if mutex is locked + bool isLocked() const; + private: #ifdef WINDOWS void * @@ -56,6 +59,7 @@ private: pthread_mutex_t #endif mutex; + bool locked; }; diff --git a/utils/system_daemon/daemon.cpp b/utils/system_daemon/daemon.cpp index f0888f18..e0975b9f 100644 --- a/utils/system_daemon/daemon.cpp +++ b/utils/system_daemon/daemon.cpp @@ -136,6 +136,8 @@ void Daemon::TileFileProgress::show(PIFileTransfer * f) { if (ft) { conn_name = ft->name(); ::screen.setDialogTile(this); + label_file->content[0].first = "Preparing ..."; + prog_file->value = prog_all->value = 0; buttons->cur = 0; buttons->setFocus(); tm.reset(); @@ -522,7 +524,7 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) { r->dir_my.cd(dir); r->ft.setDirectory(r->dir_my); //piCout << "store to" << r->dir_my.absolutePath(); - piCout << "cd to" << dir << ", abs =" << r->dir_my.absolutePath(); + //piCout << "cd to" << dir << ", abs =" << r->dir_my.absolutePath(); sendDirToRemote(r); break; case ReplyHostInfo: diff --git a/utils/system_daemon/daemon.h b/utils/system_daemon/daemon.h index 7f3fe2b0..6571664f 100644 --- a/utils/system_daemon/daemon.h +++ b/utils/system_daemon/daemon.h @@ -52,6 +52,9 @@ public: PIScreenTile * tile() const; + bool lockedEth() const {return eth_mutex.isLocked();} + bool lockedPeers() const {return peers_mutex.isLocked();} + private: enum PacketType { RequestHostInfo = 10, diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index 0effdea6..9f404a7a 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -82,7 +82,7 @@ public: CONNECTU(&screen, keyPressed, this, keyEvent) CONNECTU(&file_manager, menuRequest, this, menuRequest) CONNECTU(&daemon_, menuRequest, this, menuRequest) - start(100); + start(10); } PIScreenTile * menuTile() { TileList * ret = new TileList(); @@ -127,6 +127,7 @@ public: return ret; } void updatePeerInfo() { + bool pm = daemon_.lockedPeers(); screen.lock(); daemon_.lock(); peers_tl->content.clear(); @@ -134,8 +135,8 @@ public: peerinfo_tl->content.clear(); peermap_tl->content.clear(); peers_tl->content << TileList::Row("this | 0 | 0 | " + PIString::fromNumber(daemon_.allPeers().size_s()) + - ", " + PIString::fromNumber(cur_peer) - , CellFormat()); + " [em = " + PIString::fromBool(daemon_.lockedEth()) + ", " + "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()) +