/* PIP - Platform Independent Primitives Single application Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include "pisingleapplication.h" #include "pisharedmemory.h" #define SHM_SIZE 1024*32 PISingleApplication::PISingleApplication(const PIString & app_name): PIThread() { first = true; started = false; sacnt = 0; shm = new PISharedMemory("sa_" + app_name, SHM_SIZE); start(100); } PISingleApplication::~PISingleApplication() { stop(); if (!waitForFinish(5000)) terminate(); delete shm; } bool PISingleApplication::isFirst() const { waitFirst(); return first; } void PISingleApplication::sendMessage(const PIByteArray & m) { waitFirst(); PIByteArray ba; int lm[2] = {0, 0}; for (;;) { shm->read(lm, 8); if (lm[1] == 0) break; piMSleep(10); } ba << sacnt << sacnt << int(1) << m; shm->write(ba); } void PISingleApplication::begin() { int cnt[2] = {0, 0}; int tcnt = 0; shm->read(cnt, 8); for (int i = 0; i < 5; ++i) { tcnt = cnt[0]; shm->read(cnt, 8); if (cnt[0] == cnt[1] && cnt[0] != tcnt) { first = false; break; } piMSleep(100); } //piCoutObj << "started" << first << shm->size(); readed.reserve(shm->size()); started = true; } void PISingleApplication::run() { if (!first) return; ++sacnt; int st_[2] = {sacnt, sacnt}; shm->write(st_, 8); //piCoutObj << "write" << sacnt; readed = shm->readAll(); int t1(0), t2(0), nm(0); readed >> t1 >> t2 >> nm; if (nm != 0 && t1 == t2) { PIByteArray msg; readed >> msg; if (!msg.isEmpty()) { messageReceived(msg); //piCoutObj << "message" << msg; } int wi[3] = {sacnt, sacnt, 0}; shm->write(wi, 12); } } void PISingleApplication::waitFirst() const { while (!started) piMSleep(50); }