|
|
/*
|
|
|
* ABS_Client.c
|
|
|
*
|
|
|
* Fecha de creacion : Set, 2012
|
|
|
* Ultima modificacion : Oct 2012
|
|
|
* Autor : Christiane Dietrich.
|
|
|
*/
|
|
|
//#include <python2.6/Python.h>
|
|
|
#include <python2.5/Python.h>
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
#include <unistd.h>
|
|
|
#include <errno.h>
|
|
|
#include <sys/types.h>
|
|
|
#include <sys/socket.h>
|
|
|
#include <netinet/in.h>
|
|
|
#include <arpa/inet.h>
|
|
|
#include <netdb.h>
|
|
|
|
|
|
#include "./Librerias/Mensajes.h"
|
|
|
|
|
|
#define PUERTO_SERVIDOR 7000
|
|
|
//#define DIR_SERVIDOR "192.168.1.30"
|
|
|
|
|
|
/* Servidor */
|
|
|
#define TAM_BUFFER 10000
|
|
|
/* Servidor */
|
|
|
|
|
|
|
|
|
int ABS_Client(char *header, char *addrClient, char *addrServer ,char *opcion, char *valor_help){
|
|
|
|
|
|
int conexion_clienteFD;
|
|
|
struct sockaddr_in inf_servidor;
|
|
|
|
|
|
int broadcast= 1;
|
|
|
int resultado;
|
|
|
int numbytes_enviados;
|
|
|
|
|
|
long lSize=0;
|
|
|
size_t size_of_filecontent=0;
|
|
|
|
|
|
char *head= NULL;
|
|
|
char *tx= NULL;
|
|
|
char *rx= NULL;
|
|
|
char *comando= NULL;
|
|
|
char *data= NULL;
|
|
|
char *helper= ":";
|
|
|
char *tx_buff= NULL;
|
|
|
unsigned int indicator = 1;
|
|
|
|
|
|
head = (char*) malloc (sizeof(char)*(strlen(header)+1));
|
|
|
if (head == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
strcpy (head, header);
|
|
|
|
|
|
tx = (char*) malloc (sizeof(char)*(strlen(addrClient)+1));
|
|
|
if (tx == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
strcpy (tx, addrClient);
|
|
|
|
|
|
rx = (char*) malloc (sizeof(char)*(strlen(addrServer)+1));
|
|
|
if (rx == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
strcpy (rx, addrServer);
|
|
|
|
|
|
/* determine comand */
|
|
|
if(strcmp(opcion,"-f") == 0){
|
|
|
comando= "SNDF";
|
|
|
FILE * file_to_send= NULL;
|
|
|
file_to_send = fopen(valor_help, "r");
|
|
|
if (!file_to_send){
|
|
|
printf("The file provided for the Control Modules couldn't be opened!!!\n");
|
|
|
return -1;
|
|
|
}
|
|
|
else{
|
|
|
// obtain file size:
|
|
|
fseek (file_to_send, 0 , SEEK_END);
|
|
|
lSize = ftell (file_to_send);
|
|
|
rewind (file_to_send);
|
|
|
|
|
|
// allocate memory to contain the whole file:
|
|
|
data = (char*) malloc (sizeof(char)*(lSize+1));
|
|
|
if (data == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// copy the file into the data:
|
|
|
size_of_filecontent = fread (data,1,lSize,file_to_send);
|
|
|
|
|
|
if (size_of_filecontent != lSize) {
|
|
|
printf ("Reading error");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* the whole file is now loaded in the memory data. */
|
|
|
// terminate
|
|
|
fclose (file_to_send);
|
|
|
}
|
|
|
}else if (strcmp(opcion,"-c") == 0){ // Se reconoce el comando que se esta enviando
|
|
|
comando= "CHGB";
|
|
|
data = (char*) malloc (sizeof(char)*(strlen(valor_help)+1));
|
|
|
//data = (char *) malloc(strlen(valor)+1);
|
|
|
if (data == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
strcpy (data, valor_help);
|
|
|
}else if(strcmp(opcion,"-a") == 0){
|
|
|
comando= "ANST";
|
|
|
data = (char*) malloc (sizeof(char)*(strlen(valor_help)+1));
|
|
|
//data = (char *) malloc(strlen(valor)+1);
|
|
|
if (data == NULL) {
|
|
|
printf("Memory error");
|
|
|
return -1;
|
|
|
}
|
|
|
strcpy (data, valor_help);
|
|
|
}else {
|
|
|
ERROR("OPCION INCORRECTA: {-c|-a|-f}");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
//complete tx frame
|
|
|
tx_buff= (char *) malloc(strlen(head)+1+strlen(tx)+1+strlen(rx)+1+strlen(comando)+1+strlen(data)+1);
|
|
|
// Se arma el buffer a ser enviado.
|
|
|
strcpy(tx_buff,head);
|
|
|
strcat(tx_buff,helper);
|
|
|
strcat(tx_buff,tx);
|
|
|
strcat(tx_buff,helper);
|
|
|
strcat(tx_buff,rx);
|
|
|
strcat(tx_buff,helper);
|
|
|
strcat(tx_buff,comando);
|
|
|
strcat(tx_buff,helper);
|
|
|
strcat(tx_buff,data);
|
|
|
strcat(tx_buff,helper);
|
|
|
|
|
|
|
|
|
/* Se establece el socket UDP */
|
|
|
conexion_clienteFD= socket(AF_INET,SOCK_DGRAM,0);
|
|
|
if (conexion_clienteFD == -1){
|
|
|
ERROR("No se pudo establecer el socket: socket()");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* Se establece el Broadcast con la funcion setsockpt() */
|
|
|
resultado= setsockopt(conexion_clienteFD, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
|
|
if (resultado == -1){
|
|
|
ERROR("No se pudo establecer la opcion de Broadcast: setsockopt()");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
/* Se configura la estructura que contiene la informacion sobre el servidor: inf_servidor */
|
|
|
inf_servidor.sin_family= AF_INET;
|
|
|
inf_servidor.sin_port= htons(PUERTO_SERVIDOR);
|
|
|
inf_servidor.sin_addr.s_addr= inet_addr(addrServer);
|
|
|
memset(inf_servidor.sin_zero, '\0', sizeof(inf_servidor.sin_zero));
|
|
|
|
|
|
/* Se procede a enviar el buffer */
|
|
|
numbytes_enviados= sendto(conexion_clienteFD,tx_buff,strlen(tx_buff),0,(struct sockaddr *)&inf_servidor,sizeof(inf_servidor));
|
|
|
|
|
|
printf ("Frame was sent to Central Control Module with %s IP number\n",addrServer);
|
|
|
|
|
|
if(numbytes_enviados == -1){
|
|
|
ERROR("Error de envio de datos: sendto()");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
|
|
|
free (head);
|
|
|
free (tx);
|
|
|
free (rx);
|
|
|
free (data);
|
|
|
free(tx_buff);
|
|
|
close(conexion_clienteFD);
|
|
|
///*
|
|
|
// servidor
|
|
|
int conexion_servidorFd;
|
|
|
struct sockaddr_in inf_servidor2;
|
|
|
struct sockaddr_storage inf_cliente;
|
|
|
int resultado2;
|
|
|
int numbytes_recibidos;
|
|
|
|
|
|
char *headR;
|
|
|
char *txR;
|
|
|
char *rxR;
|
|
|
char *comandoR;
|
|
|
char *valorR;
|
|
|
FILE *file_to_wr;
|
|
|
char *rx_buff = (char *) malloc(TAM_BUFFER);
|
|
|
|
|
|
// size_t addr_len;
|
|
|
socklen_t addr_len;
|
|
|
|
|
|
memset(&inf_servidor2, 0, sizeof(inf_servidor2));
|
|
|
inf_servidor2.sin_family= AF_INET;
|
|
|
inf_servidor2.sin_port= htons(PUERTO_SERVIDOR);
|
|
|
inf_servidor2.sin_addr.s_addr= INADDR_ANY;
|
|
|
|
|
|
// Se establece el socket
|
|
|
conexion_servidorFd = socket(AF_INET,SOCK_DGRAM,0);
|
|
|
if (conexion_servidorFd == -1){
|
|
|
ERROR_FATAL("No se establecio correctamente el socket: socket() point1");
|
|
|
}
|
|
|
|
|
|
// Se asocia el socket a un puerto y una IP
|
|
|
resultado2 = bind(conexion_servidorFd,(struct sockaddr *)&inf_servidor2,sizeof(inf_servidor2));
|
|
|
if (resultado2== -1){
|
|
|
ERROR_FATAL("No se establecio correctamente el socket: bind() point2");
|
|
|
}
|
|
|
|
|
|
while(indicator == 1){
|
|
|
LOG_SERVIDOR("Waiting for the client...\n");
|
|
|
|
|
|
// Se espera hasta que un cliente se conecte
|
|
|
addr_len = sizeof(inf_cliente);
|
|
|
numbytes_recibidos = recvfrom(conexion_servidorFd, rx_buff, TAM_BUFFER-1, 0, (struct sockaddr *)&inf_cliente, &addr_len);
|
|
|
|
|
|
if (numbytes_recibidos == -1){
|
|
|
ERROR_FATAL("Error en la recepcion de datos: recvfrom()");
|
|
|
}
|
|
|
|
|
|
// Se procede a procesar los datos recibidos
|
|
|
rx_buff[numbytes_recibidos]= '\0';
|
|
|
|
|
|
// create a response-file
|
|
|
headR = strtok(rx_buff, ":");
|
|
|
txR = strtok(NULL, ":");
|
|
|
rxR = strtok(NULL, ":");
|
|
|
comandoR = strtok(NULL, ":");
|
|
|
valorR = strtok(NULL, ":");
|
|
|
|
|
|
|
|
|
if ((headR == NULL) || (txR == NULL) || (rxR == NULL) || (comandoR == NULL) || (valorR == NULL)){
|
|
|
ERROR("procesarPeticionClient: formato de mensaje incorrecto");
|
|
|
}else{
|
|
|
|
|
|
if(strcmp(comandoR,"SNDF") == 0){
|
|
|
file_to_wr = fopen("report.abs", "w+");
|
|
|
if (!file_to_wr){
|
|
|
printf("The file, in which the received data should be written, couldn't be opened!!!\n");
|
|
|
}else{
|
|
|
fprintf(file_to_wr, "%s", valorR);
|
|
|
}
|
|
|
fclose(file_to_wr);
|
|
|
printf("File content was successfully written in\"report.abs\"\n");
|
|
|
}else if(strcmp(comandoR,"CHGB") == 0){
|
|
|
file_to_wr = fopen("report.ver", "w+");
|
|
|
if (!file_to_wr){
|
|
|
printf("The file, in which the received data should be written, couldn't be opened!!!\n");
|
|
|
}else{
|
|
|
fprintf(file_to_wr, "%s", valorR);
|
|
|
}
|
|
|
fclose(file_to_wr);
|
|
|
printf("File content was successfully written in \"report.ver\"\n");
|
|
|
}else if(strcmp(comandoR,"ANST") == 0){
|
|
|
file_to_wr = fopen("report.net", "w+");
|
|
|
if (!file_to_wr){
|
|
|
printf("The file, in which the received data should be written, couldn't be opened!!!\n");
|
|
|
}else{
|
|
|
fprintf(file_to_wr, "%s", valorR);
|
|
|
}
|
|
|
fclose(file_to_wr);
|
|
|
printf("File content was successfully written in \"report.net\"\n");
|
|
|
}else{
|
|
|
ERROR("procesa_peticion_client: respuesta no reconocido");
|
|
|
}
|
|
|
}
|
|
|
indicator = 0;
|
|
|
// servidor
|
|
|
}
|
|
|
free (rx_buff);
|
|
|
//*/
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|