[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[certi-cvs] certi/libCERTI RingBuffer.cc RingBuffer.hh Sema... [br_CERTI
From: |
certi-cvs |
Subject: |
[certi-cvs] certi/libCERTI RingBuffer.cc RingBuffer.hh Sema... [br_CERTI_SHM_NEWGEN_dev] |
Date: |
Mon, 12 Oct 2009 12:02:43 +0000 |
CVSROOT: /sources/certi
Module name: certi
Branch: br_CERTI_SHM_NEWGEN_dev
Changes by: Adelantado <adele> 09/10/12 12:02:43
Added files:
libCERTI : RingBuffer.cc RingBuffer.hh SemaphoreWin32.cc
SemaphoreWin32.hh
Log message:
Introducing RingBuffer class to emulate Socket Unix Send and Receive
Operations through shared memory. Available under UNIX, Linux (Posix and SysV
implementation) and Win32 OS.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/RingBuffer.cc?cvsroot=certi&only_with_tag=br_CERTI_SHM_NEWGEN_dev&rev=1.1.2.1
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/RingBuffer.hh?cvsroot=certi&only_with_tag=br_CERTI_SHM_NEWGEN_dev&rev=1.1.2.1
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/SemaphoreWin32.cc?cvsroot=certi&only_with_tag=br_CERTI_SHM_NEWGEN_dev&rev=1.1.2.1
http://cvs.savannah.gnu.org/viewcvs/certi/libCERTI/SemaphoreWin32.hh?cvsroot=certi&only_with_tag=br_CERTI_SHM_NEWGEN_dev&rev=1.1.2.1
Patches:
Index: RingBuffer.cc
===================================================================
RCS file: RingBuffer.cc
diff -N RingBuffer.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ RingBuffer.cc 12 Oct 2009 12:02:43 -0000 1.1.2.1
@@ -0,0 +1,482 @@
+// ----------------------------------------------------------------------------
+// CERTI - HLA RunTime Infrastructure
+// Copyright (C) 2002-2005 ONERA
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA
+// ----------------------------------------------------------------------------
+
+// Systems includes
+#include <limits>
+#include <string.h>
+// Specifics includes
+#ifdef _WIN32
+ #include <windows.h>
+ #include <process.h>
+ #include "SocketSHMWin32.hh"
+ #include "SemaphoreWin32.hh"
+ #include "SHMWin32.hh"
+#else
+ #include "SemaphorePosix.hh"
+ #include "SHMPosix.hh"
+ #include "SemaphoreSysV.hh"
+ #include "SHMSysV.hh"
+#endif
+
+#include "RingBuffer.hh"
+
+RingBuffer::RingBuffer(const std::string& RingBuffer_Name,
+ const BUFFER_SIDE_t& RingBuffer_Side,
+ const int RingBuffer_Size,
+ const std::string& Shm_Sem_Type ) {
+
+#ifdef DEBUG
+std::cout << "-----------------------------------------------------------" <<
std::endl ;
+std::cout << " ----------------------- RingBuffer -----------------------"<<
std::endl ;
+std::cout << "-----------------------------------------------------------" <<
std::endl ;
+#endif
+
+_Name = RingBuffer_Name ;
+_Side = RingBuffer_Side ;
+_Size = RingBuffer_Size ;
+
+_Tab_SC = new int[3] ;
+_Tab_SC[0] = RingBuffer_Size ;
+_Tab_SC[1] = 0 ;
+_Tab_SC[2] = 0 ;
+
+_Tab_CS = new int[3] ;
+_Tab_CS[0] = RingBuffer_Size ;
+_Tab_CS[1] = 0 ;
+_Tab_CS[2] = 0 ;
+
+#ifndef _WIN32
+if (Shm_Sem_Type == "SysV"){
+ _Sem_CS = new SemaphoreSysV() ;
+ _Sem_SC = new SemaphoreSysV() ;
+ }
+else if (Shm_Sem_Type == "Posix"){
+ _Sem_CS = new SemaphorePosix() ;
+ _Sem_SC = new SemaphorePosix() ;
+ }
+else{
+ perror("Shm_Sem_Type Must be Posix or SysV") ;
+ exit(1) ;
+ }
+#else
+ _Sem_CS = new SemaphoreWin32() ;
+ _Sem_SC = new SemaphoreWin32() ;
+#endif
+
+
+if(_Side == BUFFER_SC){
+ _Sem_SC->Create_Init(1,
Semaphore::buildSemName(RingBuffer_Name+"_BUFFER_SC")) ;
+ }
+else{
+ _Sem_CS->Create_Init(1,
Semaphore::buildSemName(RingBuffer_Name+"_BUFFER_CS")) ;
+ }
+
+
+if(_Side == BUFFER_SC){
+#ifndef _WIN32
+ if (Shm_Sem_Type == "SysV"){
+ _Shm_SC = new
SHMSysV(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"), RingBuffer_Size,true) ;
+ _Shm_CS = new
SHMSysV(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"), RingBuffer_Size) ;
+ _Pw_Pr_SC = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_IND_SC"),
3 * sizeof(int) , true) ;
+ _Pw_Pr_CS = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_IND_CS"),
3 * sizeof(int)) ;
+ }
+ if (Shm_Sem_Type == "Posix"){
+ _Shm_SC = new
SHMPosix(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"), RingBuffer_Size,true)
;
+ _Shm_CS = new
SHMPosix(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"), RingBuffer_Size) ;
+ _Pw_Pr_SC = new
SHMPosix(SHM::buildShmName(RingBuffer_Name+"_IND_SC"), 3 * sizeof(int) , true) ;
+ _Pw_Pr_CS = new
SHMPosix(SHM::buildShmName(RingBuffer_Name+"_IND_CS"), 3 * sizeof(int)) ;
+ }
+#else
+ _Shm_SC = new
SHMWin32(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"), RingBuffer_Size,true)
;
+ _Shm_CS = new
SHMWin32(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"), RingBuffer_Size) ;
+ _Pw_Pr_SC = new
SHMWin32(SHM::buildShmName(RingBuffer_Name+"_IND_SC"), 3 * sizeof(int) , true) ;
+ _Pw_Pr_CS = new
SHMWin32(SHM::buildShmName(RingBuffer_Name+"_IND_CS"), 3 * sizeof(int)) ;
+#endif
+ }
+else {
+#ifndef _WIN32
+ if (Shm_Sem_Type == "SysV"){
+ _Shm_CS = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"),
RingBuffer_Size, true) ;
+ _Shm_SC = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"),
RingBuffer_Size) ;
+ _Pw_Pr_CS = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_IND_CS"), 3
* sizeof(int), true) ;
+ _Pw_Pr_SC = new SHMSysV(SHM::buildShmName(RingBuffer_Name+"_IND_SC"), 3
* sizeof(int)) ;
+ }
+ if (Shm_Sem_Type == "Posix"){
+ _Shm_CS = new SHMPosix(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"),
RingBuffer_Size, true) ;
+ _Shm_SC = new SHMPosix(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"),
RingBuffer_Size) ;
+ _Pw_Pr_CS = new SHMPosix(SHM::buildShmName(RingBuffer_Name+"_IND_CS"),
3 * sizeof(int), true) ;
+ _Pw_Pr_SC = new SHMPosix(SHM::buildShmName(RingBuffer_Name+"_IND_SC"),
3 * sizeof(int)) ;
+ }
+#else
+ if (Shm_Sem_Type == "Win32"){
+ _Shm_CS = new SHMWin32(SHM::buildShmName(RingBuffer_Name+"_BUFFER_CS"),
RingBuffer_Size, true) ;
+ _Shm_SC = new SHMWin32(SHM::buildShmName(RingBuffer_Name+"_BUFFER_SC"),
RingBuffer_Size) ;
+ _Pw_Pr_CS = new SHMWin32(SHM::buildShmName(RingBuffer_Name+"_IND_CS"),
3 * sizeof(int), true) ;
+ _Pw_Pr_SC = new SHMWin32(SHM::buildShmName(RingBuffer_Name+"_IND_SC"),
3 * sizeof(int)) ;
+ }
+#endif
+}
+
+if(_Side == BUFFER_SC){
+ _Shm_SC->Open() ;
+ _Shm_SC->Attach() ;
+ _Pw_Pr_SC->Open() ;
+ _Pw_Pr_SC->Attach() ;
+ #ifdef DEBUG
+ std::cout << " The SHMs for RingBuffer from SERVER to CUSTOMER Exchange
are Created and Attached " << std::endl ;
+ std::cout << " -----> Adresse : _Shm_SC->GetShm() = " << _Shm_SC->GetShm()
<< std::endl ;
+ std::cout << " -----> Adresse : _Pw_Pr_SC->GetShm() = " <<
_Pw_Pr_SC->GetShm() << std::endl ;
+ #endif
+ }
+else{
+ _Shm_CS->Open() ;
+ _Shm_CS->Attach() ;
+ _Pw_Pr_CS->Open() ;
+ _Pw_Pr_CS->Attach() ;
+ #ifdef DEBUG
+ std::cout << " The SHMs for RingBuffer from CUSTOMER to SERVER Exchanges
are Created and Attached" << std::endl ;
+ std::cout << " -----> Adresse : _Shm_CS->GetShm() = " <<
_Shm_CS->GetShm() << std::endl ;
+ std::cout << " -----> Adresse : _Pw_Pr_CS->GetShm() = " <<
_Pw_Pr_CS->GetShm() << std::endl ;
+ #endif
+ }
+} // End of RingBuffer Constructor
+// ************************************************
+// Method : RingBuffer::Attach()
+// ************************************************
+void RingBuffer::Attach() {
+
+if(_Side == BUFFER_CS){
+ _Sem_SC->Attach(Semaphore::buildSemName(_Name+"_BUFFER_SC")) ;
+ }
+else{
+ _Sem_CS->Attach(Semaphore::buildSemName(_Name+"_BUFFER_CS")) ;
+ }
+
+if(_Side == BUFFER_CS){
+ _Shm_SC->Open() ;
+ _Shm_SC->Attach() ;
+ _Pw_Pr_SC->Open() ;
+ _Pw_Pr_SC->Attach() ;
+ #ifdef DEBUG
+ std::cout << " The SHMs for RingBuffer from SERVER to CUSTOMER Exchanges
are Identified and Attached " << std::endl ;
+ std::cout << " Adresse : _Shm_SC->GetShm() = " << _Shm_SC->GetShm() <<
std::endl ;
+ std::cout << " Adresse : _Pw_Pr_SC->GetShm() = " << _Pw_Pr_SC->GetShm()
<< std::endl ;
+ #endif
+ }
+else{
+ _Shm_CS->Open() ;
+ _Shm_CS->Attach() ;
+ _Pw_Pr_CS->Open() ;
+ _Pw_Pr_CS->Attach() ;
+ #ifdef DEBUG
+ std::cout << " The SHMs for RingBuffer from CUSTOMER to SERVER Exchanges
are Identified and Attached " << std::endl ;
+ std::cout << " Adresse : _Shm_CS->GetShm() = " << _Shm_CS->GetShm() <<
std::endl ;
+ std::cout << " Adresse : _Pw_Pr_CS->GetShm() = " << _Pw_Pr_CS->GetShm()
<< std::endl ;
+ #endif
+ }
+
+if(_Side == BUFFER_CS){
+ memcpy(_Pw_Pr_CS->GetShm(), _Tab_CS, 3 * sizeof(int) ) ;
+ }
+else{
+ memcpy(_Pw_Pr_SC->GetShm(), _Tab_SC, 3 * sizeof(int) ) ;
+ }
+
+} // End of RingBuffer::Attach()
+
+// ************************************************
+// Destructor
+// ************************************************
+RingBuffer ::~RingBuffer (){
+if(_Side == BUFFER_SC){
+ _Sem_SC->Delete() ;
+ }
+else{
+ _Sem_CS->Delete() ;
+ }
+
+delete _Sem_SC ;
+delete _Sem_CS ;
+delete _Shm_SC ;
+delete _Shm_CS ;
+delete _Pw_Pr_SC ;
+delete _Pw_Pr_CS ;
+} // End of RingBuffer Destructor
+
+// ************************************************
+// Method : RingBuffer::Send(...)
+// ************************************************
+void RingBuffer::Send(void *Buffer, size_t Size) {
+
+#ifdef DEBUG
+std::cout << "RingBuffer --> Try to Send..." << std::endl ;
+#endif
+
+if (Size > _Size) {
+ perror("RingBuffer::Send(...) : Size too big !! ") ;
+ exit(1);
+ }
+
+if(_Side == BUFFER_SC){
+
+ _Sem_SC->P() ;
+ memcpy(_Tab_SC, _Pw_Pr_SC->GetShm(), 3 * sizeof(int) ) ;
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> BEGIN Algorithm : Count_SC = " <<
_Tab_SC[0] << " | Write_SC = " << _Tab_SC[1] << "| Read_SC = " << _Tab_SC[2] <<
std::endl ;
+ #endif
+
+ if (Size > _Tab_SC[0]) { // Test si il y a assez de place disponible dans
le buffer (Exeption ÃÂ envoyer)
+ perror("Probleme !! ") ;
+ exit(1);
+ }
+
+ if ( (_Tab_SC[2] > _Tab_SC[1]) || ((_Tab_SC[1] >= _Tab_SC[2]) && (Size +
_Tab_SC[1] <= _Size)) ) {
+ // Plecture >= Pecriture || Pecriture >= Plecture && Taille a ecrire +
Pecriture <=TailleTotaleSegment
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> Utilisation memcpy Simple " <<
std::endl ;
+ std::cout << "RingBuffer::Send(...) --> Adresse Utilisee
_Shm_SC->GetShm() = " << _Shm_SC->GetShm() << "| _Shm_SC->GetShm() + _Tab_SC[1]
= " << (void *)((char *)(_Shm_SC->GetShm()) + (sizeof(void *) * _Tab_SC[1])) <<
std::endl ;
+ #endif
+
+ memcpy((void *)((char *)(_Shm_SC->GetShm())+ (sizeof(void *) *
_Tab_SC[1])) , Buffer, Size) ;
+
+ _Tab_SC[0] -= Size ;
+ // _Tab_SC[1] += Size ;
+ _Tab_SC[1] = (_Tab_SC[1] + Size) % _Size ;
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> Utilisation memcpy Double " <<
std::endl ;
+ #endif
+
+ int Rest_byte_in_SHM = _Size - _Tab_SC[1] ;
+
+ memcpy((void *)((char *)(_Shm_SC->GetShm())+ (sizeof(void *) *
_Tab_SC[1])), Buffer, Rest_byte_in_SHM ) ;
+ memcpy(_Shm_SC->GetShm(), (void *)((char *)(Buffer) + (sizeof(void *)
* Rest_byte_in_SHM)), Size - Rest_byte_in_SHM ) ;
+
+ _Tab_SC[1] = (_Tab_SC[1] + Size) % _Size ;
+ _Tab_SC[0] -= Size ;
+ // _Tab_SC[0] = _Tab_SC[2] - _Tab_SC[1] ;
+
+ } // Fin du if ((_Tab_SC[2] >= _Tab_SC[1]) ...) /else
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> END of Algorithm : Count_SC = "
<< _Tab_SC[0] << " | Write_SC = " << _Tab_SC[1] << "| Read_SC = " << _Tab_SC[2]
<< std::endl ;
+ #endif
+
+ memcpy(_Pw_Pr_SC->GetShm(), _Tab_SC, 3 * sizeof(int) ) ;
+
+ _Sem_SC->V() ;
+ }
+else{
+
+ _Sem_CS->P() ;
+ memcpy(_Tab_CS, _Pw_Pr_CS->GetShm(), 3 * sizeof(int) ) ;
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> BEGIN Algorithm : Count_CS = " <<
_Tab_CS[0] << " | Write_CS = " << _Tab_CS[1] << "| Read_CS = " << _Tab_CS[2] <<
std::endl ;
+ #endif
+
+ if (Size > _Tab_CS[0]) { // Test si il y a assez de place disponible dans
le buffer
+ perror("Probleme !! ") ;
+ exit(1);
+ }
+
+ if ( (_Tab_CS[2] > _Tab_CS[1]) || ((_Tab_CS[1] >= _Tab_CS[2]) && (Size +
_Tab_CS[1] <= _Size)) ) {
+ // Plecture >= Pecriture || Pecriture >= Plecture && Taille a ecrire +
Pecriture <=TailleTotaleSegment
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> Utilisation memcpy Simple " <<
std::endl ;
+ std::cout << "RingBuffer::Send(...) --> Adresse Utilisee
_Shm_CS->GetShm() = " << _Shm_CS->GetShm() << "| _Shm_CS->GetShm() +
_Tab_CS[1] = " << (void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void *) *
_Tab_CS[1])) << std::endl ;
+ #endif
+
+ memcpy((void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void *) *
_Tab_CS[1])) , Buffer, Size) ;
+ _Tab_CS[0] -= Size ;
+ // _Tab_CS[1] += Size ;
+ _Tab_CS[1] = (_Tab_CS[1] + Size) % _Size ;
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> Utilisation memcpy Double " <<
std::endl ;
+ #endif
+
+ int Rest_byte_in_SHM = _Size - _Tab_CS[1] ;
+ memcpy((void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void *) *
_Tab_CS[1])), Buffer, Rest_byte_in_SHM ) ;
+ memcpy(_Shm_CS->GetShm(), (void *)((char *)(Buffer) + (sizeof(void *)
* Rest_byte_in_SHM)), Size - Rest_byte_in_SHM ) ;
+
+ _Tab_CS[1] = (_Tab_CS[1] + Size) % _Size ;
+ // _Tab_CS[0] = _Tab_CS[2] - _Tab_CS[1] ;
+ _Tab_CS[0] -= Size ;
+
+ }
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Send(...) --> END of Algorithm : Count_CS = "
<< _Tab_CS[0] << " | Write_CS = " << _Tab_CS[1] << "| Read_CS = " << _Tab_CS[2]
<< std::endl ;
+ #endif
+
+ memcpy(_Pw_Pr_CS->GetShm(), _Tab_CS, 3 * sizeof(int) ) ;
+
+ _Sem_CS->V() ;
+ }
+
+#ifdef DEBUG
+std::cout << "RingBuffer --> Send Complete !!" << std::endl ;
+#endif
+} // End of RingBuffer::Send(...)
+
+// ************************************************
+// Method : RingBuffer::Receive(...)
+// ************************************************
+void RingBuffer::Receive(void *Buffer, size_t Size) {
+
+#ifdef DEBUG
+std::cout << "RingBuffer --> Try to Receive..." << std::endl ;
+#endif
+
+if (Size > _Size) {
+ perror("RingBuffer::Receive(...) : Size too big !! ") ;
+ exit(1);
+ }
+
+if(_Side == BUFFER_SC){
+
+ _Sem_CS->P() ;
+
+ memcpy(_Tab_CS, _Pw_Pr_CS->GetShm(), 3 * sizeof(int) ) ;
+
+ if (_Tab_CS[0] == _Size ) { // Test si il y a assez de place disponible
dans le buffer (Exeption ÃÂ envoyer)
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Nothing to Read on _Shm_SC
!!"<< std::endl ;
+ #endif
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> BEGIN Algorithm : Count_CS
= " << _Tab_CS[0] << " | Write_CS = " << _Tab_CS[1] << "| Read_CS = " <<
_Tab_CS[2] << std::endl ;
+ #endif
+
+ if ( (_Tab_CS[1] > _Tab_CS[2]) || ((_Tab_CS[2] >= _Tab_CS[1]) && (Size
+ _Tab_CS[2] <= _Size)) ) {
+ // Plecture >= Pecriture || Pecriture >= Plecture && Taille a ecrire +
Pecriture <=TailleTotaleSegment
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Utilisation memcpy
Simple " << std::endl ;
+ std::cout << "RingBuffer::Receive(...) --> Adresse Utilisee
_Shm_CS->GetShm() = " << _Shm_CS->GetShm() << "| _Shm_CS->GetShm() +
_Tab_CS[2] = " << (void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void *) *
_Tab_CS[2])) << std::endl ;
+ #endif
+
+ memcpy(Buffer, (void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void
*) * _Tab_CS[2])) ,Size) ;
+
+ _Tab_CS[0] += Size ;
+ // _Tab_CS[0] = (_Tab_CS[0] + Size) % _Size ;
+ _Tab_CS[2] += Size ;
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Utilisation memcpy
Double " << std::endl ;
+ #endif
+
+ int Rest_byte_in_SHM = _Size - _Tab_CS[2] ;
+ memcpy( Buffer, (void *)((char *)(_Shm_CS->GetShm())+ (sizeof(void
*) * _Tab_CS[2])), Rest_byte_in_SHM ) ;
+ memcpy( (void *)((char *)(Buffer) + (sizeof(void *) *
Rest_byte_in_SHM)), _Shm_CS->GetShm(), Size - Rest_byte_in_SHM ) ;
+
+ _Tab_CS[2] = (_Tab_CS[2] + Size) % _Size ;
+ _Tab_CS[0] += Size ;
+ } // Fin du if ((_Tab_SC[2] >= _Tab_SC[1]) ...) /else
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> END Algorithm : Count_CS =
" << _Tab_CS[0] << " | Write_CS = " << _Tab_CS[1] << "| Read_CS = " <<
_Tab_CS[2] << std::endl ;
+ #endif
+ std::cout << "AVANT memcpy(_Pw_Pr_CS->GetShm(), _Tab_CS, 3 *
sizeof(int) ) ; !! " << std::endl ;
+ memcpy(_Pw_Pr_CS->GetShm(), _Tab_CS, 3 * sizeof(int) ) ;
+ std::cout << "APRES memcpy(_Pw_Pr_CS->GetShm(), _Tab_CS, 3 *
sizeof(int) ) ; !! " << std::endl ;
+ } // Fin du else for if (_Tab_SC[0] == _Size )
+
+ _Sem_CS->V() ;
+ }
+else{
+
+ _Sem_SC->P() ;
+ memcpy(_Tab_SC, _Pw_Pr_SC->GetShm(), 3 * sizeof(int) ) ;
+
+ if (_Tab_SC[0] == _Size ) { // Test si il y a assez de place disponible
dans le buffer (Exeption ÃÂ envoyer)
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Nothing to Read on _Shm_CS
!!"<< std::endl ;
+ #endif
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Adresse _Tab_SC = " <<
_Tab_SC << " Adresse _Pw_Pr_SC = " << _Pw_Pr_SC << std::endl ;
+ std::cout << "RingBuffer::Receive(...) --> Begin of Algorithm :
Count_SC = " << _Tab_SC[0] << " | Write_SC = " << _Tab_SC[1] << "| Read_SC = "
<< _Tab_SC[2] << std::endl ;
+ #endif
+
+ if ( (_Tab_SC[1] > _Tab_SC[2]) || ((_Tab_SC[2] >= _Tab_SC[1]) && (Size
+ _Tab_SC[2] <= _Size)) ) {
+ // Plecture >= Pecriture || Pecriture >= Plecture && Taille a ecrire
+ Pecriture <=TailleTotaleSegment
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Utilisation memcpy
Simple " << std::endl ;
+ std::cout << "RingBuffer::Receive(...) --> Adresse Utilisee
_Shm_SC->GetShm() = " << _Shm_SC->GetShm() << "| _Shm_SC->GetShm() +
_Tab_SC[2] = " << (void *)((char *)(_Shm_SC->GetShm())+ (sizeof(void *) *
_Tab_SC[2])) << std::endl ;
+ #endif
+
+ memcpy( Buffer, (void *)((char *)(_Shm_SC->GetShm())+ (sizeof(void
*) * _Tab_SC[2])) , Size) ;
+
+ _Tab_SC[0] += Size ;
+ // _Tab_SC[0] = (_Tab_SC[0] + Size) % _Size ;
+ _Tab_SC[2] += Size ;
+ }
+ else {
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> Utilisation memcpy
Double " << std::endl ;
+ #endif
+
+ int Rest_byte_in_SHM = _Size - _Tab_SC[2] ;
+ memcpy(Buffer, (void *)((char *)(_Shm_SC->GetShm())+ (sizeof(void
*) * _Tab_SC[2])), Rest_byte_in_SHM ) ;
+ memcpy((void *)((char *)(Buffer) + (sizeof(void *) *
Rest_byte_in_SHM)), _Shm_SC->GetShm(), Size - Rest_byte_in_SHM ) ;
+
+ _Tab_SC[2] = (_Tab_SC[2] + Size) % _Size ;
+ // _Tab_CS[0] = _Tab_CS[2] - _Tab_CS[1] ;
+ _Tab_SC[0] += Size ;
+ }
+
+ #ifdef DEBUG
+ std::cout << "RingBuffer::Receive(...) --> End of Algorithm :
Count_SC = " << _Tab_SC[0] << " | Write_SC = " << _Tab_SC[1] << "| Read_SC = "
<< _Tab_SC[2] << std::endl ;
+ #endif
+
+ memcpy(_Pw_Pr_SC->GetShm(), _Tab_SC, 3 * sizeof(int) ) ;
+ } // Fin du else for if (_Tab_CS[0] == _Size )
+
+ _Sem_SC->V() ;
+ }
+
+#ifdef DEBUG
+std::cout << "RingBuffer --> Receive complete!!!" << std::endl ;
+#endif
+} // End of RingBuffer::Receive(...)
+
+// ************************************************
+// Method : RingBuffer::Close()
+// ************************************************
+void RingBuffer::Close() {
+
+_Shm_SC->Close() ;
+_Shm_CS->Close() ;
+_Pw_Pr_SC->Close() ;
+_Pw_Pr_CS->Close() ;
+
+} // End of RingBuffer::Close()
+
Index: RingBuffer.hh
===================================================================
RCS file: RingBuffer.hh
diff -N RingBuffer.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ RingBuffer.hh 12 Oct 2009 12:02:43 -0000 1.1.2.1
@@ -0,0 +1,72 @@
+// ----------------------------------------------------------------------------
+// CERTI - HLA RunTime Infrastructure
+// Copyright (C) 2002-2005 ONERA
+//
+// 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 2 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, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// ----------------------------------------------------------------------------
+
+#ifndef RING_BUFFER_H
+#define RING_BUFFER_H
+
+#include <iostream>
+
+// Specifics includes
+#include "SHM.hh"
+#include "Semaphore.hh"
+
+class CERTI_EXPORT RingBuffer {
+public :
+ // Typedef Side
+ typedef enum{BUFFER_SC,BUFFER_CS} BUFFER_SIDE_t ;
+
+ // Constructor
+ RingBuffer(const std::string& RingBuffer_Name,
+ const BUFFER_SIDE_t& RingBuffer_Side,
+ const int RingBuffer_Size,
+ const std::string& SHM_Sem_Type ) ; // SHM_Sem_Type =
Posix,SysV ou Win32
+ // Destructor
+ ~RingBuffer ();
+
+ void Attach() ;
+
+ void Send(void *Buffer, size_t Size) ; // To send Data on a memory segment
+ void Receive(void *Buffer, size_t Size) ; // To receive Data on a memory
segment
+
+ void Close(); // To Close the two SHMs
+
+protected :
+ std::string _Name ;
+ BUFFER_SIDE_t _Side ;
+ size_t _Size ;
+
+ /***** Server -->>> Customer ******/
+ SHM *_Shm_SC ;
+ SHM *_Pw_Pr_SC ;
+ // _Count_SC, _Write_SC, _Read_SC
+ // int _Tab_SC[3] ;
+ int* _Tab_SC ;
+ Semaphore *_Sem_SC ;
+
+ /***** Customer -->>> Server ******/
+ SHM *_Shm_CS ;
+ SHM *_Pw_Pr_CS ;
+ // _Count_CS, _Write_CS, _Read_CS
+ // int _Tab_CS[3] ;
+ int* _Tab_CS ;
+ Semaphore *_Sem_CS ;
+
+}; // End of --> class SocketSHM
+
+#endif
Index: SemaphoreWin32.cc
===================================================================
RCS file: SemaphoreWin32.cc
diff -N SemaphoreWin32.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SemaphoreWin32.cc 12 Oct 2009 12:02:43 -0000 1.1.2.1
@@ -0,0 +1,148 @@
+#include "SemaphoreWin32.hh"
+
+// ************************************************
+// Constructor
+// ************************************************
+SemaphoreWin32::SemaphoreWin32(){
+_hSemaphore = NULL ;
+}
+
+// ************************************************
+// Destructor
+// ************************************************
+SemaphoreWin32::~SemaphoreWin32() {}
+
+// ************************************************
+// Method : SemaphoreWin32::Create_Init(...)
+// ************************************************
+void SemaphoreWin32::Create_Init(const int initval, const std::string&
New_Semname) {
+
+_hSemaphore = CreateSemaphore(
+ (LPSECURITY_ATTRIBUTES)NULL, // security
attributes
+ (LONG)(initval), // initial count
+ (LONG)(1), // maximum count
+ (LPCTSTR)(New_Semname.c_str())); // named
semaphore
+
+ if (_hSemaphore == NULL){
+ _tprintf(TEXT("CreateSemaphore() in SemaphoreWin32::Create_Init(...)
(%d).n"),
+ GetLastError());
+ exit(1) ;
+ }
+ #ifdef DEBUG
+ std::cout << "We create the semaphore identified by handle : " << _hSemaphore
<< " and name : " << New_Semname << std::endl ;
+ #endif
+
+} // End of method : Create_Init(...)
+
+// ************************************************
+// Method : SemaphoreWin32::Attach(...)
+// ************************************************
+void SemaphoreWin32::Attach(const std::string& New_Semname) {
+
+
+
+// Open the semaphore
+
+ _hSemaphore = OpenSemaphore(
+ SEMAPHORE_ALL_ACCESS, // security attributes
+ FALSE, // Inherit Handle
+ (LPCTSTR)(New_Semname.c_str()));
// named semaphore
+
+#ifdef DEBUG
+ std::cout << "We try to attach the semaphore identified by handle : " <<
_hSemaphore << " and name : " << New_Semname << std::endl ;
+#endif
+
+ if (_hSemaphore == NULL)
+ {
+ _tprintf(TEXT("Could not open semaphore object (%d).n"),
+ GetLastError());
+ exit (1);
+ }
+
+} // End of method : Attach(...)
+
+// ************************************************
+// Method : SemaphoreWin32::P
+// ************************************************
+
+void SemaphoreWin32::P() {
+
+#ifdef DEBUG
+std::cout << "Begin of Operation P for the semaphore identified by handle : "
<< _hSemaphore << std::endl ;
+#endif
+
+DWORD WINAPI dwRetCode;
+
+dwRetCode = WaitForSingleObject(
+ (HANDLE)_hSemaphore, // handle to semaphore
+ INFINITE); // if we want to have P blocked
+
+switch (dwRetCode)
+ {
+ // The semaphore object was signaled.
+ case WAIT_OBJECT_0:
+ // Semaphore is signaled
+ // go ahead and continue the work
+ break;
+
+ default:
+ // Handle errors
+ _tprintf(TEXT("Error with WaitForSingleObject() in
SemaphoreWin32::P() (%d).n"),
+ GetLastError());
+ exit(1);
+ }
+
+#ifdef DEBUG
+std::cout << "End of Operation P for the semaphore identified by handle : " <<
_hSemaphore << std::endl ;
+#endif
+
+} // End of P()
+
+
+// ************************************************
+// Method : SemaphoreWin32::V
+// ************************************************
+
+void SemaphoreWin32::V() {
+
+#ifdef DEBUG
+std::cout << "Begin of Operation V for the semaphore identified by handle : "
<< _hSemaphore << std::endl ;
+#endif
+
+BOOL WINAPI retcode ;
+
+ retcode = ReleaseSemaphore(
+ _hSemaphore, // handle to semaphore
+ 1, // increase count by one
+ NULL) ; // not interested in previous count
+
+ if (retcode == 0) {
+ _tprintf(TEXT("Error with ReleaseSemaphore() in SemaphoreWin32::V()
(%d).n"),
+ GetLastError());
+ exit(1);
+ }
+
+#ifdef DEBUG
+std::cout << "End of Operation V for the semaphore identified by handle : " <<
_hSemaphore << std::endl ;
+#endif
+
+} // End of V()
+
+// ************************************************
+// Method : SemaphoreWin32::Delete
+// ************************************************
+void SemaphoreWin32::Delete(){
+
+BOOL WINAPI retcode ;
+
+#ifdef DEBUG
+std::cout << "Destroy the semaphore identified by handle : " << _hSemaphore <<
std::endl ;
+#endif
+
+retcode = CloseHandle(_hSemaphore);
+
+if(retcode == 0)
+ _tprintf(TEXT("Error with CloseHandle() in SemaphoreWin32::Delete()
(%d).n"),
+ GetLastError());
+} // End of Delete()
+
Index: SemaphoreWin32.hh
===================================================================
RCS file: SemaphoreWin32.hh
diff -N SemaphoreWin32.hh
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SemaphoreWin32.hh 12 Oct 2009 12:02:43 -0000 1.1.2.1
@@ -0,0 +1,35 @@
+#ifndef SEMAPHOREWIN32_H
+#define SEMAPHOREWIN32_H
+
+// Semaphores usefull systems includes
+#include <windows.h>
+#include <conio.h>
+#include <tchar.h>
+
+// Others systems includes
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <iostream>
+
+// Specifics includes
+#include "certi.hh"
+#include "Semaphore.hh"
+
+
+class CERTI_EXPORT SemaphoreWin32 : public Semaphore {
+ private :
+ HANDLE WINAPI _hSemaphore;
+
+ public :
+ SemaphoreWin32() ;
+ virtual ~SemaphoreWin32() ;
+ void Create_Init(const int initval, const std::string& New_Semname) ;
+ void Attach(const std::string& New_Semname ) ;
+ void P() ;
+ void V() ;
+ void Delete() ;
+
+} ;
+
+#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [certi-cvs] certi/libCERTI RingBuffer.cc RingBuffer.hh Sema... [br_CERTI_SHM_NEWGEN_dev],
certi-cvs <=