git-svn-id: svn://db.shs.com.ru/pip@400 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
104
src_main/system/pisingleapplication.cpp
Normal file
104
src_main/system/pisingleapplication.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Single application
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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 << int(1) << m;
|
||||
shm->write(ba);
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::begin() {
|
||||
int cnt[2] = {0, 0};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
cnt[1] = cnt[0];
|
||||
shm->read(cnt, 4);
|
||||
if (cnt[0] != cnt[1]) {
|
||||
first = false;
|
||||
break;
|
||||
}
|
||||
piMSleep(100);
|
||||
}
|
||||
//piCoutObj << "started" << first << shm->size();
|
||||
readed.reserve(shm->size());
|
||||
started = true;
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::run() {
|
||||
if (!first) return;
|
||||
++sacnt;
|
||||
shm->write(&sacnt, 4);
|
||||
//piCoutObj << "write" << sacnt;
|
||||
readed = shm->readAll();
|
||||
int t(0), nm(0);
|
||||
readed >> t >> nm;
|
||||
if (nm != 0) {
|
||||
PIByteArray msg;
|
||||
readed >> msg;
|
||||
if (!msg.isEmpty()) {
|
||||
messageReceived(msg);
|
||||
//piCoutObj << "message" << msg;
|
||||
}
|
||||
int wi[2] = {sacnt, 0};
|
||||
shm->write(wi, 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PISingleApplication::waitFirst() const {
|
||||
while (!started)
|
||||
piMSleep(50);
|
||||
}
|
||||
Reference in New Issue
Block a user