abstractspwbridge.cpp
176 lines
| 5.5 KiB
| text/x-c
|
CppLexer
/ spwplugin / abstractspwbridge.cpp
Jeandet Alexis
|
r8 | /*------------------------------------------------------------------------------ | ||
-- This file is a part of the SocExplorer Software | ||||
Jeandet Alexis
|
r11 | -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS | ||
Jeandet Alexis
|
r8 | -- | ||
-- 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, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
-------------------------------------------------------------------------------*/ | ||||
/*-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@member.fsf.org | ||||
----------------------------------------------------------------------------*/ | ||||
r0 | #include "abstractspwbridge.h" | |||
Alexis Jeandet
|
r78 | #include <QTime> | ||
#include <socexplorerengine.h> | ||||
r0 | ||||
abstractSpwBridge::abstractSpwBridge(socexplorerplugin *parent) | ||||
:QObject((QObject*)parent) | ||||
{ | ||||
this->plugin = parent; | ||||
Jeandet Alexis
|
r8 | this->p_GUI=NULL; | ||
r0 | } | |||
Jeandet Alexis
|
r24 | abstractSpwBridge::~abstractSpwBridge() | ||
{ | ||||
paul
|
r50 | // delete this->p_GUI; | ||
Jeandet Alexis
|
r24 | } | ||
r0 | QWidget *abstractSpwBridge::getGUI() | |||
{ | ||||
return this->p_GUI; | ||||
} | ||||
bool abstractSpwBridge::connectBridge() | ||||
{ | ||||
return false; | ||||
} | ||||
bool abstractSpwBridge::disconnectBridge() | ||||
{ | ||||
return false; | ||||
} | ||||
Alexis Jeandet
|
r78 | |||
abstractSpwManager::abstractSpwManager(socexplorerplugin *plugin, QObject *parent) | ||||
:QThread((QObject*)parent) | ||||
{ | ||||
this->RMAPtimeout = 2000; | ||||
this->handleMutex = new QMutex(QMutex::NonRecursive); | ||||
this->RMAP_AnswersSem = new QSemaphore(0); | ||||
this->RMAP_AnswersMtx=new QMutex(QMutex::Recursive); | ||||
this->RMAP_pending_transaction_IDsMtx=new QMutex(QMutex::Recursive); | ||||
this->plugin = plugin; | ||||
connected = false; | ||||
} | ||||
abstractSpwManager::~abstractSpwManager() | ||||
{ | ||||
Alexis Jeandet
|
r82 | this->requestInterruption(); | ||
while(!this->isFinished()); | ||||
Alexis Jeandet
|
r78 | } | ||
int abstractSpwManager::getRMAPtransactionID() | ||||
{ | ||||
this->RMAP_pending_transaction_IDsMtx->lock(); | ||||
int ID=0; | ||||
bool found=true; | ||||
while(ID<511) | ||||
{ | ||||
for(int i=0;i<RMAP_pending_transaction_IDs.count();i++) | ||||
{ | ||||
if(RMAP_pending_transaction_IDs[i]==ID)found=false; | ||||
} | ||||
if(found==true)break; | ||||
ID++; | ||||
found = true; | ||||
} | ||||
if(found) | ||||
{ | ||||
RMAP_pending_transaction_IDs.append(ID); | ||||
} | ||||
this->RMAP_pending_transaction_IDsMtx->unlock(); | ||||
return ID; | ||||
} | ||||
int abstractSpwManager::getRMAPanswer(int transactionID, char **buffer) | ||||
{ | ||||
QTime timeout; | ||||
*buffer=NULL; | ||||
int count=0; | ||||
SocExplorerEngine::message(this->plugin,"Looking for RMAP answer",2); | ||||
qApp->processEvents(); | ||||
timeout.start(); | ||||
while (*buffer==NULL) | ||||
{ | ||||
this->RMAP_AnswersMtx->lock(); | ||||
SocExplorerEngine::message(this->plugin,"Got exclusive access on RMAP_Answers stack",2); | ||||
SocExplorerEngine::message(this->plugin,QString("%1 packet(s) available in RMAP_Answers stack").arg(RMAP_Answers.count()),2); | ||||
for(int i=0;i<RMAP_Answers.count();i++) | ||||
{ | ||||
SocExplorerEngine::message(this->plugin,QString("Packet %1 ID=%2").arg(i).arg(RMAP_Answers[i]->transactionID),2); | ||||
if(RMAP_Answers[i]->transactionID==transactionID) | ||||
{ | ||||
this->RMAP_pending_transaction_IDsMtx->lock(); | ||||
SocExplorerEngine::message(this->plugin,"Got exclusive access on RMAP_pending_transaction_ID stack",2); | ||||
for(int j=0;j<RMAP_pending_transaction_IDs.count();j++) | ||||
{ | ||||
if(RMAP_pending_transaction_IDs[j]==transactionID) | ||||
{ | ||||
RMAP_pending_transaction_IDs.removeAt(j); | ||||
} | ||||
} | ||||
this->RMAP_pending_transaction_IDsMtx->unlock(); | ||||
*buffer = RMAP_Answers[i]->data; | ||||
count = RMAP_Answers[i]->len; | ||||
RMAP_Answer* tmp=RMAP_Answers[i]; | ||||
RMAP_Answers.removeAt(i); | ||||
delete tmp; | ||||
} | ||||
} | ||||
this->RMAP_AnswersMtx->unlock(); | ||||
//if no answer found in the stack wait until a new packet is pushed | ||||
SocExplorerEngine::message(this->plugin,"waiting until a new packet is pushed",2); | ||||
if(*buffer==NULL) | ||||
{ | ||||
while (0==this->RMAP_AnswersSem->available()) | ||||
{ | ||||
SocExplorerEngine::message(this->plugin,QString("this->RMAP_AnswersSem->available() = %1").arg(this->RMAP_AnswersSem->available()),2); | ||||
if(timeout.elapsed()>=RMAPtimeout) | ||||
{ | ||||
SocExplorerEngine::message(this->plugin,"Timeout reached giving up!",2); | ||||
return -1; | ||||
} | ||||
usleep(1000); | ||||
qApp->processEvents(); | ||||
} | ||||
this->RMAP_AnswersSem->acquire(); | ||||
} | ||||
} | ||||
return count; | ||||
} | ||||
int abstractSpwManager::getLinkNumber() | ||||
{ | ||||
return this->linkNumber; | ||||
} | ||||
void abstractSpwManager::pushRmapPacket(char *packet, int len) | ||||
{ | ||||
char* packetbuffer = (char*)malloc(len); | ||||
memcpy(packetbuffer,packet,len); | ||||
RMAP_Answer* RMPAPpacket=new RMAP_Answer(RMAP_get_transactionID(packetbuffer+1),packetbuffer,len); | ||||
RMAP_AnswersMtx->lock(); | ||||
RMAP_Answers.append(RMPAPpacket); | ||||
RMAP_AnswersMtx->unlock(); | ||||
} | ||||