monitor.cpp
Go to the documentation of this file.
/*
* Copyright (C) 2018 dimercur
*
* 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 "monitor.h"
#include "server.h"
using namespace std;
SOCKADDR_IN ssin;
SOCKET sock;
socklen_t recsize = sizeof (ssin);
string serverReceive(int size);
int sendMessage(const char *data, int dataLength);
int receive(char *data);
int send_message_to_monitor(const char* typeMessage, const void * data) {
if ((string) typeMessage == HEADER_STM_IMAGE) {
Jpg * imgC = (Jpg*) data;
sendMessage("IMG", 3);
sendMessage((const char*) imgC->data(), imgC->size());
sendMessage("TRAME", 5);
return 0;
} else if ((string) typeMessage == HEADER_STM_POS) {
char buffer[400];
Position * maPosition = (Position*) data;
sprintf(buffer, "POScenter: %3d;%3d | %.1fTRAME", maPosition->center.x, maPosition->center.y, maPosition->angle);
sendMessage(buffer, strlen(buffer));
return 0;
} else if ((string) typeMessage == HEADER_STM_MES) {
char buffer[50];
sprintf(buffer, "MSG%sTRAME", (const char*) data);
sendMessage(buffer, strlen(buffer));
return 0;
} else if ((string) typeMessage == HEADER_STM_ACK) {
char buffer[50];
sprintf(buffer, "ACK%sTRAME", (const char*) data);
sendMessage(buffer, strlen(buffer));
return 0;
} else if ((string) typeMessage == HEADER_STM_BAT) {
char buffer[50];
sprintf(buffer, "BAT%sTRAME", (const char*) data);
sendMessage(buffer, strlen(buffer));
return 0;
} else if ((string) typeMessage == HEADER_STM_NO_ACK) {
char buffer[50];
sprintf(buffer, "NAK%sTRAME", (const char*) data);
sendMessage(buffer, strlen(buffer));
return 0;
} else if ((string) typeMessage == HEADER_STM_LOST_DMB) {
char buffer[50];
sprintf(buffer, "LCD%sTRAME", (const char*) data);
sendMessage(buffer, strlen(buffer));
return 0;
} else {
return -1;
}
}
int receive_message_from_monitor(char *typeMessage, char *data) {
char buffer[20];
int tBuffer = receive(buffer);
sscanf(buffer, "%3s:%s", typeMessage, data);
return tBuffer;
}
int receive(char *data) {
int result;
result = receiveDataFromServer(data, 20);
//cout <<"Data : " << data << endl;
return result;
}
int sendMessage(const char *data, int dataLength) {
if (sendDataToServer((char*) data, dataLength) != dataLength) {
perror("Send failed : ");
return false;
}
return true;
}
Position::center
Point center
Definition: image.h:70
HEADER_STM_POS
#define HEADER_STM_POS
Definition: monitor.h:43
Position::angle
float angle
Definition: image.h:72
receive
int receive(char *data)
Definition: monitor.cpp:90
SOCKET
int SOCKET
Definition: monitor.h:70
HEADER_STM_NO_ACK
#define HEADER_STM_NO_ACK
Definition: monitor.h:44
HEADER_STM_ACK
#define HEADER_STM_ACK
Definition: monitor.h:45
std
receive_message_from_monitor
int receive_message_from_monitor(char *typeMessage, char *data)
Réception d&#39;un message. La fonction est bloquante et retourne par référence le type de message reçu (...
Definition: monitor.cpp:83
send_message_to_monitor
int send_message_to_monitor(const char *typeMessage, const void *data)
Envoi d&#39;un message vers l&#39;interface graphique.
Definition: monitor.cpp:40
HEADER_STM_LOST_DMB
#define HEADER_STM_LOST_DMB
Definition: monitor.h:47
HEADER_STM_BAT
#define HEADER_STM_BAT
Definition: monitor.h:42
recsize
socklen_t recsize
Definition: monitor.cpp:33
server.h
Position
Definition: image.h:69
ssin
SOCKADDR_IN ssin
Definition: monitor.cpp:31
sendMessage
int sendMessage(const char *data, int dataLength)
Definition: monitor.cpp:97
Jpg
vector< unsigned char > Jpg
Definition: image.h:67
receiveDataFromServer
int receiveDataFromServer(char *data, int size)
Definition: server.cpp:95
HEADER_STM_MES
#define HEADER_STM_MES
Definition: monitor.h:46
sendDataToServer
int sendDataToServer(char *data, int length)
Definition: server.cpp:85
sock
SOCKET sock
Definition: monitor.cpp:32
SOCKADDR_IN
struct sockaddr_in SOCKADDR_IN
Definition: monitor.h:71
monitor.h
Library for sending message to monitor or receiving message.
serverReceive
string serverReceive(int size)
HEADER_STM_IMAGE
#define HEADER_STM_IMAGE
Definition: monitor.h:41