[397] | 1 | /**
|
---|
| 2 | * \file ipc_receive.h
|
---|
| 3 | * \brief definition de classe
|
---|
| 4 | * \author {
|
---|
| 5 | * Sébastien Ambroziak
|
---|
| 6 | * Paul Sauvage
|
---|
| 7 | * }
|
---|
| 8 | * \version 0.1
|
---|
| 9 | * \date 5 novembre 2020
|
---|
| 10 | *
|
---|
| 11 | * Definitions de la classe IpcReceiver ainsi que ses méthodes.
|
---|
| 12 | * Cette classe permet de recevoir des structures ou des liste de structure par ipc
|
---|
| 13 | *
|
---|
| 14 | * La classe IpcReceiver utilise une template à 2 paramètres, T et N
|
---|
| 15 | * le paramètre T correspond au type de donnée que le receiver doit recevoir
|
---|
| 16 | * le paramètre N correspond au nombre d'élément du tableau d'élément de type T à recevoir
|
---|
| 17 | * si N = 0 la fonction receive() retourne directement un objet de type T
|
---|
| 18 | * si N > 0 la fonction receive() retourne un pointeur de type T et donc un tableau d'élément T de N éléments
|
---|
| 19 | *
|
---|
| 20 | * Le constructeur utilise 2 paramètres:
|
---|
| 21 | * Une chaîne de caractère contenant le nom du fichier ipc vers lequel lire les données
|
---|
| 22 | * Un entier correspond au numéro de canal Ipc
|
---|
| 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | #ifndef IPC_RECEIVE
|
---|
| 29 | #define IPC_RECEIVE
|
---|
| 30 |
|
---|
| 31 | #include "ipc_base.h"
|
---|
| 32 |
|
---|
| 33 | #include <functional>
|
---|
| 34 | #include <memory>
|
---|
| 35 | #include <iostream>
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 | #include <type_traits>
|
---|
| 38 |
|
---|
| 39 | #include <sys/ipc.h>
|
---|
| 40 | #include <sys/msg.h>
|
---|
| 41 | #include <sys/time.h>
|
---|
| 42 | #include <string>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 |
|
---|
| 45 | using namespace std;
|
---|
| 46 | using namespace ipc::type;
|
---|
| 47 |
|
---|
| 48 | class IpcReceiverBase {
|
---|
| 49 | public:
|
---|
| 50 | IpcReceiverBase(const char* ipc_name, int proj_id=65){
|
---|
| 51 | cout << "IpcReceiver on " << ipc_name << endl;
|
---|
| 52 | system("mkdir -p /tmp/ipc");
|
---|
| 53 | system("chmod 777 /tmp/ipc");
|
---|
| 54 |
|
---|
| 55 | string pathname = string("/tmp/ipc/") + string(ipc_name);
|
---|
| 56 | string command = string("> ") + pathname;
|
---|
| 57 | system(command.c_str());
|
---|
| 58 |
|
---|
| 59 | command = string("chmod 666 ") + pathname;
|
---|
| 60 | system(command.c_str());
|
---|
| 61 |
|
---|
| 62 | key_t ipc_key = ftok(pathname.c_str(), proj_id); //crete unique key
|
---|
| 63 | cout << "ipc key " << ipc_key << endl << endl;
|
---|
| 64 | _queue_id = msgget(ipc_key, 0666 | IPC_CREAT); //create message queue and return id
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | ~IpcReceiverBase(){msgctl(_queue_id, IPC_RMID, NULL);} //destroy the message queue.
|
---|
| 68 |
|
---|
| 69 | protected:
|
---|
| 70 | int _queue_id;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 | template <typename T>
|
---|
| 74 | class IpcReceiver : IpcReceiverBase {
|
---|
| 75 | public:
|
---|
| 76 | IpcReceiver(const char* ipc_name, int proj_id=65, bool blocking = true): IpcReceiverBase(ipc_name, proj_id), _blocking(blocking) {}
|
---|
| 77 | ~IpcReceiver() = default;
|
---|
| 78 |
|
---|
| 79 | int receive(){
|
---|
| 80 | if(_blocking){
|
---|
| 81 | msgrcv(_queue_id, &_message, sizeof(_message.data), 0, 0);
|
---|
| 82 | }
|
---|
| 83 | else if(msgrcv(_queue_id, &_message, sizeof(_message.data), 0, IPC_NOWAIT) == -1){
|
---|
| 84 | return -1;
|
---|
| 85 | }
|
---|
| 86 | return 0;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | timestamp getTimestamp() const {
|
---|
| 90 | return _message.data.stamp;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | T getValue() const {return _message.data.value;}
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | protected:
|
---|
| 97 | ipc_message<T> _message;
|
---|
| 98 | bool _blocking;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | template <typename T>
|
---|
| 102 | class IpcReceiverTab : IpcReceiverBase {
|
---|
| 103 | public:
|
---|
| 104 | IpcReceiverTab(size_t N, const char* ipc_name, int proj_id=65, bool blocking = true): IpcReceiverBase(ipc_name, proj_id), _N(N), _blocking(blocking) {
|
---|
| 105 | _message = (ipc_tab<T>*) malloc(sizeof(ipc_tab<T>) + sizeof(T)*N);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | ~IpcReceiverTab() = default;
|
---|
| 109 |
|
---|
| 110 | int receive(){
|
---|
| 111 | if(_blocking){
|
---|
| 112 | msgrcv(_queue_id, _message, sizeof(T)*_N + sizeof(_message->data), 0, 0);
|
---|
| 113 | }
|
---|
| 114 | else if(msgrcv(_queue_id, _message, sizeof(T)*_N + sizeof(_message->data), 0, IPC_NOWAIT) == -1){
|
---|
| 115 | return -1;
|
---|
| 116 | }
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | T getValue(size_t index) const {
|
---|
| 121 | return _message->data.values[index];
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | const T* getValues() const {
|
---|
| 125 | return _message->data.values;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | timestamp getTimestamp() const {
|
---|
| 129 | return _message->data.stamp;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | size_t getN() const {return _N;}
|
---|
| 133 |
|
---|
| 134 | void setAll(T value) {
|
---|
| 135 | for (size_t i = 0; i < _N; i++)
|
---|
| 136 | {
|
---|
| 137 | _message->data.values[i] = value;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | protected:
|
---|
| 142 | size_t _N;
|
---|
| 143 | bool _blocking;
|
---|
| 144 | ipc_tab<T> *_message;
|
---|
| 145 |
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | #endif |
---|