PIPeer important fix!

git-svn-id: svn://db.shs.com.ru/pip@110 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2015-04-19 19:01:46 +00:00
parent 929338a4d7
commit 476958706f
12 changed files with 147 additions and 18 deletions

View File

@@ -507,6 +507,9 @@ void PIScreen::run() {
tile_dialog->width = sw; tile_dialog->width = sw;
tile_dialog->height = sh; tile_dialog->height = sh;
tile_dialog->layout(); 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_); tile_dialog->drawEventInternal(&drawer_);
} }
console.print(); console.print();

View File

@@ -46,6 +46,58 @@
using namespace PIScreenTypes; using namespace PIScreenTypes;
PIScreenDrawer::PIScreenDrawer(PIVector<PIVector<Cell> > & 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() { void PIScreenDrawer::clear() {
for (int i = 0; i < cells.size_s(); ++i) for (int i = 0; i < cells.size_s(); ++i)
cells[i].fill(Cell()); 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<Cell> & 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) { 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); if (x0 == x1 && y0 == y1) drawPixel(x0, y0, c, col_char, col_back, flags_char);
Cell cc; Cell cc;

View File

@@ -29,19 +29,25 @@
class PIP_EXPORT PIScreenDrawer class PIP_EXPORT PIScreenDrawer
{ {
friend class PIScreen; friend class PIScreen;
PIScreenDrawer(PIVector<PIVector<PIScreenTypes::Cell> > & c): cells(c) {} PIScreenDrawer(PIVector<PIVector<PIScreenTypes::Cell> > & c);
public: public:
enum ArtChar {LineVertical = 1, LineHorizontal, Cross, CornerTopLeft, CornerTopRight, CornerBottomLeft, CornerBottomRight};
void clear(); void clear();
void clearRect(int x0, int y0, int x1, int y1) {fillRect(x0, y0, x1, y1, ' ');} 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 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 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 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 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); 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: private:
PIVector<PIVector<PIScreenTypes::Cell> > & cells; PIVector<PIVector<PIScreenTypes::Cell> > & cells;
int width, height; int width, height;
PIMap<ArtChar, PIChar> arts_;
}; };

View File

@@ -88,7 +88,6 @@ TileList::TileList(const PIString & n): PIScreenTile(n) {
focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab; focus_flags = CanHasFocus | NextByArrowsHorizontal | NextByTab;
lhei = offset = cur = 0; lhei = offset = cur = 0;
selection_mode = NoSelection; selection_mode = NoSelection;
vert_line = PIChar::fromUTF8("");
} }
@@ -101,6 +100,7 @@ void TileList::sizeHint(int & w, int & h) const {
void TileList::drawEvent(PIScreenDrawer * d) { void TileList::drawEvent(PIScreenDrawer * d) {
vert_line = d->artChar(PIScreenDrawer::LineVertical);
lhei = height - 2; lhei = height - 2;
//int osp = piMini(3, lhei / 4); //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()); int is = piClampi(offset, 0, piMaxi(0, content.size_s() - 1)), ie = piClampi(offset + lhei, 0, content.size_s());

View File

@@ -98,7 +98,10 @@ void PIBaseTransfer::received(PIByteArray data) {
} }
if (is_receiving && h.id == 0) { if (is_receiving && h.id == 0) {
// if (checkSession() == 0 && pt == pt_ReplySuccess) finish_receive(true); // 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; break;
case pt_Break: case pt_Break:

View File

@@ -50,7 +50,7 @@ PIPeer::PeerData::~PeerData() {
void PIPeer::PeerData::dtThread() { void PIPeer::PeerData::dtThread() {
//piCoutObj << "send DT ..."; // << "send DT ...";
dt_out.send(data); dt_out.send(data);
//piCoutObj << "send DT done"; //piCoutObj << "send DT done";
} }
@@ -71,8 +71,9 @@ void PIPeer::PeerData::receivedPacket(uchar type, const PIByteArray & d) {
dt = &dt_in; dt = &dt_in;
if (type == 2) if (type == 2)
dt = &dt_out; dt = &dt_out;
//piCoutObj << "DT received" << int(type) << d.size_s(); //piCoutObj << "DT received" << int(type) << d.size_s() << "...";
if (dt) dt->received(d); 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->setDebug(false);
ce->setName("__S__PIPeer_traffic_eth_rec_" + a); ce->setName("__S__PIPeer_traffic_eth_rec_" + a);
ce->setParameters(0); ce->setParameters(0);
ce->setThreadSafe(true);
bool ok = false; bool ok = false;
for (int p = _PIPEER_TRAFFIC_PORT_S; p < _PIPEER_TRAFFIC_PORT_E; ++p) { for (int p = _PIPEER_TRAFFIC_PORT_S; p < _PIPEER_TRAFFIC_PORT_E; ++p) {
ce->setReadAddress(a, p); ce->setReadAddress(a, p);
@@ -370,7 +370,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
if (pi) { if (pi) {
if (pi->isNeighbour()) { if (pi->isNeighbour()) {
sba << int(6) << to << from << addr << time; 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(); send_mutex.lock();
piForeachC (PeerInfo::Address & a, pi->addresses) { piForeachC (PeerInfo::Address & a, pi->addresses) {
if (eth_send.send(a.address, sba)) if (eth_send.send(a.address, sba))
@@ -400,7 +400,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
a.wait_ping = false; a.wait_ping = false;
if (a.ping < 0) a.ping = ptime.toMilliseconds(); if (a.ping < 0) a.ping = ptime.toMilliseconds();
else a.ping = 0.6 * a.ping + 0.4 * 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; return true;
} }
} }
@@ -445,7 +445,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
cnt++; cnt++;
if (cnt > _PIPEER_MSG_TTL || from == dp->name) return true; if (cnt > _PIPEER_MSG_TTL || from == dp->name) return true;
sba << type << from << to << cnt << pba; sba << type << from << to << cnt << pba;
//piCout << "translate packet" << from << "->" << to << ", ttl =" << cnt; piCout << "translate packet" << from << "->" << to << ", ttl =" << cnt;
sendToNeighbour(dp, sba); sendToNeighbour(dp, sba);
} }
return true; return true;
@@ -526,6 +526,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
//piCoutObj << "rec sync " << rpeers.size_s() << " peers"; //piCoutObj << "rec sync " << rpeers.size_s() << " peers";
peers_mutex.lock(); peers_mutex.lock();
if (!self_info.neighbours.contains(pi.name)) { if (!self_info.neighbours.contains(pi.name)) {
//piCoutObj << "add new nei to me" << pi.name;
self_info.addNeighbour(pi.name); self_info.addNeighbour(pi.name);
PeerInfo * np = peers_map.value(pi.name); PeerInfo * np = peers_map.value(pi.name);
if (np) { if (np) {
@@ -569,6 +570,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
} }
if (exist || isRemoved(rpeer)) continue; if (exist || isRemoved(rpeer)) continue;
rpeer.dist++; rpeer.dist++;
if (rpeer.name == pi.name) rpeer.dist = 0;
rpeer.resetPing(); rpeer.resetPing();
addPeer(rpeer); addPeer(rpeer);
ch = true; ch = true;
@@ -679,10 +681,10 @@ void PIPeer::sendPeerRemove(const PIString & peer) {
void PIPeer::pingNeighbours() { void PIPeer::pingNeighbours() {
PIByteArray ba, sba; PIByteArray ba, sba;
ba << int(5) << self_info.name; ba << int(5) << self_info.name;
//piCout << "pingNeighbours" << peers.size(); //piCout << "*** pingNeighbours" << peers.size() << "...";
piForeach (PeerInfo & p, peers) { piForeach (PeerInfo & p, peers) {
//piCout << " ping neighbour" << p.name << p.ping();
if (!p.isNeighbour()) continue; if (!p.isNeighbour()) continue;
//piCout << " ping neighbour" << p.name << p.ping();
send_mutex.lock(); send_mutex.lock();
piForeach (PeerInfo::Address & a, p.addresses) { piForeach (PeerInfo::Address & a, p.addresses) {
//piCout << " address" << a.address << a.wait_ping; //piCout << " address" << a.address << a.wait_ping;
@@ -700,6 +702,7 @@ void PIPeer::pingNeighbours() {
} }
send_mutex.unlock(); send_mutex.unlock();
} }
//piCout << "*** pingNeighbours" << peers.size() << "done";
} }

View File

@@ -179,6 +179,8 @@ private:
// 1 - new peer, 2 - remove peer, 3 - sync peers, 4 - data, 5 - ping request, 6 - ping reply // 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 // Data packet: 4, from, to, ticks, data_size, data
protected:
typedef PIPair<PIString, PIVector<PeerInfo * > > napair; typedef PIPair<PIString, PIVector<PeerInfo * > > napair;
PIVector<PIEthernet * > eths_traffic, eths_mcast, eths_bcast; PIVector<PIEthernet * > eths_traffic, eths_mcast, eths_bcast;

View File

@@ -47,6 +47,7 @@ PIMutex::PIMutex() {
pthread_mutex_init(&mutex, &attr); pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr); pthread_mutexattr_destroy(&attr);
#endif #endif
locked = false;
} }
@@ -56,6 +57,7 @@ PIMutex::~PIMutex() {
#else #else
pthread_mutex_destroy(&mutex); pthread_mutex_destroy(&mutex);
#endif #endif
locked = false;
} }
@@ -65,6 +67,7 @@ void PIMutex::lock() {
#else #else
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
#endif #endif
locked = true;
} }
@@ -74,13 +77,22 @@ void PIMutex::unlock() {
#else #else
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
#endif #endif
locked = false;
} }
bool PIMutex::tryLock() { bool PIMutex::tryLock() {
bool ret =
#ifdef WINDOWS #ifdef WINDOWS
return (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0); (WaitForSingleObject(mutex, 0) == WAIT_OBJECT_0);
#else #else
return (pthread_mutex_trylock(&mutex) == 0); (pthread_mutex_trylock(&mutex) == 0);
#endif #endif
locked = true;
return ret;
}
bool PIMutex::isLocked() const {
return locked;
} }

View File

@@ -49,6 +49,9 @@ public:
//! If mutex is already locked function returns immediate an returns "false" //! If mutex is already locked function returns immediate an returns "false"
bool tryLock(); bool tryLock();
//! Returns if mutex is locked
bool isLocked() const;
private: private:
#ifdef WINDOWS #ifdef WINDOWS
void * void *
@@ -56,6 +59,7 @@ private:
pthread_mutex_t pthread_mutex_t
#endif #endif
mutex; mutex;
bool locked;
}; };

View File

@@ -136,6 +136,8 @@ void Daemon::TileFileProgress::show(PIFileTransfer * f) {
if (ft) { if (ft) {
conn_name = ft->name(); conn_name = ft->name();
::screen.setDialogTile(this); ::screen.setDialogTile(this);
label_file->content[0].first = "Preparing ...";
prog_file->value = prog_all->value = 0;
buttons->cur = 0; buttons->cur = 0;
buttons->setFocus(); buttons->setFocus();
tm.reset(); tm.reset();
@@ -522,7 +524,7 @@ void Daemon::dataReceived(const PIString & from, const PIByteArray & data) {
r->dir_my.cd(dir); r->dir_my.cd(dir);
r->ft.setDirectory(r->dir_my); r->ft.setDirectory(r->dir_my);
//piCout << "store to" << r->dir_my.absolutePath(); //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); sendDirToRemote(r);
break; break;
case ReplyHostInfo: case ReplyHostInfo:

View File

@@ -52,6 +52,9 @@ public:
PIScreenTile * tile() const; PIScreenTile * tile() const;
bool lockedEth() const {return eth_mutex.isLocked();}
bool lockedPeers() const {return peers_mutex.isLocked();}
private: private:
enum PacketType { enum PacketType {
RequestHostInfo = 10, RequestHostInfo = 10,

View File

@@ -82,7 +82,7 @@ public:
CONNECTU(&screen, keyPressed, this, keyEvent) CONNECTU(&screen, keyPressed, this, keyEvent)
CONNECTU(&file_manager, menuRequest, this, menuRequest) CONNECTU(&file_manager, menuRequest, this, menuRequest)
CONNECTU(&daemon_, menuRequest, this, menuRequest) CONNECTU(&daemon_, menuRequest, this, menuRequest)
start(100); start(10);
} }
PIScreenTile * menuTile() { PIScreenTile * menuTile() {
TileList * ret = new TileList(); TileList * ret = new TileList();
@@ -127,6 +127,7 @@ public:
return ret; return ret;
} }
void updatePeerInfo() { void updatePeerInfo() {
bool pm = daemon_.lockedPeers();
screen.lock(); screen.lock();
daemon_.lock(); daemon_.lock();
peers_tl->content.clear(); peers_tl->content.clear();
@@ -134,8 +135,8 @@ public:
peerinfo_tl->content.clear(); peerinfo_tl->content.clear();
peermap_tl->content.clear(); peermap_tl->content.clear();
peers_tl->content << TileList::Row("this | 0 | 0 | " + PIString::fromNumber(daemon_.allPeers().size_s()) + peers_tl->content << TileList::Row("this | 0 | 0 | " + PIString::fromNumber(daemon_.allPeers().size_s()) +
", " + PIString::fromNumber(cur_peer) " [em = " + PIString::fromBool(daemon_.lockedEth()) + ", "
, CellFormat()); "pm = " + PIString::fromBool(pm) + "]", CellFormat());
piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers()) piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers())
peers_tl->content << TileList::Row(p.name + " | d = " + PIString::fromNumber(p.dist) + peers_tl->content << TileList::Row(p.name + " | d = " + PIString::fromNumber(p.dist) +
" | p = " + PIString::fromNumber(p.ping()) + " | p = " + PIString::fromNumber(p.ping()) +