server locking flag (not real lock), last client vs this client, change port implemented, most funcs of the multidetector implemented again

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@64 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
bergamaschi
2011-11-25 15:33:25 +00:00
parent f1654363e0
commit 4cbd9b2265
14 changed files with 1958 additions and 1388 deletions

View File

@ -10,11 +10,13 @@
//int socketDescriptor, file_des;
int socketDescriptor, file_des;
const int send_rec_max_size=SEND_REC_MAX_SIZE;
extern int errno;
//struct sockaddr_in address;
//#define VERBOSE
@ -23,7 +25,8 @@ int bindSocket(unsigned short int port_number) {
int i;
struct sockaddr_in addressS;
int socketDescriptor;
int file_des;
file_des= -1;
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
@ -68,7 +71,7 @@ int bindSocket(unsigned short int port_number) {
printf(" %s %s\n",lastClientIP, thisClientIP);
return socketDescriptor;
@ -76,54 +79,20 @@ int bindSocket(unsigned short int port_number) {
}
/*
only client funcs
*/
/*
#ifndef C_ONLY
MySocketTCP::MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number):
last_keep_connection_open_action_was_a_send(0), file_des(-1), send_rec_max_size(SEND_REC_MAX_SIZE), is_a_server(0), portno(DEFAULT_PORTNO), socketDescriptor(-1)
{ // sender (client): where to? ip
//is_a_server = 0;
// SetupParameters();
strcpy(hostname,host_ip_or_name);
portno=port_number;
struct hostent *hostInfo = gethostbyname(host_ip_or_name);
if (hostInfo == NULL){
cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << "\n";
} else {
// Set some fields in the serverAddress structure.
serverAddress.sin_family = hostInfo->h_addrtype;
memcpy((char *) &serverAddress.sin_addr.s_addr,
hostInfo->h_addr_list[0], hostInfo->h_length);
serverAddress.sin_port = htons(port_number);
socketDescriptor=0; //You can use send and recv, //would it work?????
}
}
int MySocketTCP::getHostname(char *name) {
if (is_a_server==0) {
strcpy(name,hostname);
}
return is_a_server;
};
#endif
*/
int getServerError()
int getServerError(int socketDescriptor)
{
if (socketDescriptor<0) return 1;
else return 0;
};
int acceptConnection() {
int acceptConnection(int socketDescriptor) {
struct sockaddr_in addressC;
int file_des;
//socklen_t address_length;
size_t address_length=sizeof(struct sockaddr_in);
@ -193,9 +162,24 @@ int acceptConnection() {
socketDescriptor=-1;
}
inet_ntop(AF_INET, &(addressC.sin_addr), thisClientIP, INET_ADDRSTRLEN);
#ifdef VERBOSE
printf("client connected %d\n", file_des);
printf("client connected %d\n", file_des);
printf("addressC %s\n", thisClientIP);
printf("addressC %s\n", lastClientIP);
#endif
// struct sockaddr_in
//{
//short int sin_family; /* Famyly of the address*/
//unsigned short int sin_port; /* Port */
//struct in_addr sin_addr; /* Network address */
//unsigned char sin_zero[8]; /* Same size of struct sockaddr */
//};
}
@ -208,7 +192,7 @@ int acceptConnection() {
void closeConnection() {
void closeConnection(int file_des) {
//fflush(stdout);
//printf("Closing file_des %d\n", file_des);
//sleep(1);
@ -216,10 +200,13 @@ void closeConnection() {
#endif
if(file_des>=0)
close(file_des);
if (lockStatus==0) {
strcpy(lastClientIP,thisClientIP);
}
file_des=-1;
}
void exitServer() {
void exitServer(int socketDescriptor) {
if (socketDescriptor>=0)
close(socketDescriptor);
#ifdef VERY_VERBOSE
@ -252,7 +239,7 @@ void MySocketTCP::Disconnect(){
*/
int sendDataOnly(void* buf,int length) {
int sendDataOnly(int file_des, void* buf,int length) {
/*
int total_sent=0;
int nsending;
@ -279,7 +266,7 @@ int sendDataOnly(void* buf,int length) {
}
int receiveDataOnly(void* buf,int length) {
int receiveDataOnly(int file_des, void* buf,int length) {
int total_received=0;
int nreceiving;
@ -327,44 +314,44 @@ int sendDataOnly(void* buf,int length) {
int sendChannel(sls_detector_channel *myChan) {
return sendDataOnly(myChan, sizeof(sls_detector_channel));
int sendChannel(int file_des, sls_detector_channel *myChan) {
return sendDataOnly(file_des,myChan, sizeof(sls_detector_channel));
}
int sendChip(sls_detector_chip *myChip) {
int sendChip(int file_des, sls_detector_chip *myChip) {
int ts=0;
int nChans=myChip->nchan;
ts+=sendDataOnly(myChip,sizeof(sls_detector_chip));
ts+=sendDataOnly(myChip->chanregs,nChans*sizeof(int));
ts+=sendDataOnly(file_des,myChip,sizeof(sls_detector_chip));
ts+=sendDataOnly(file_des,myChip->chanregs,nChans*sizeof(int));
return ts;
}
int sendModule(sls_detector_module *myMod) {
int sendModule(int file_des, sls_detector_module *myMod) {
int ts=0;
int idac;
int nChips=myMod->nchip;
int nChans=myMod->nchan;
int nAdcs=myMod->nadc;
int nDacs=myMod->ndac;
ts+= sendDataOnly(myMod,sizeof(sls_detector_module));
ts+= sendDataOnly(file_des,myMod,sizeof(sls_detector_module));
#ifdef VERBOSE
printf("module %d of size %d sent\n",myMod->module, ts);
#endif
ts+= sendDataOnly(myMod->dacs,sizeof(float)*nDacs);
ts+= sendDataOnly(file_des,myMod->dacs,sizeof(float)*nDacs);
#ifdef VERBOSE
printf("dacs %d of size %d sent\n",myMod->module, ts);
for (idac=0; idac< nDacs; idac++)
printf("dac %d is %d\n",idac,myMod->dacs[idac]);
#endif
ts+= sendDataOnly(myMod->adcs,sizeof(float)*nAdcs);
ts+= sendDataOnly(file_des,myMod->adcs,sizeof(float)*nAdcs);
#ifdef VERBOSE
printf("adcs %d of size %d sent\n",myMod->module, ts);
#endif
ts+=sendDataOnly(myMod->chipregs,sizeof(int)*nChips);
ts+=sendDataOnly(file_des,myMod->chipregs,sizeof(int)*nChips);
#ifdef VERBOSE
printf("chips %d of size %d sent\n",myMod->module, ts);
#endif
ts+=sendDataOnly(myMod->chanregs,sizeof(int)*nChans);
ts+=sendDataOnly(file_des,myMod->chanregs,sizeof(int)*nChans);
#ifdef VERBOSE
printf("chans %d of size %d sent - %d\n",myMod->module, ts, myMod->nchan);
#endif
@ -374,17 +361,17 @@ int sendModule(sls_detector_module *myMod) {
return ts;
}
int receiveChannel(sls_detector_channel *myChan) {
return receiveDataOnly(myChan,sizeof(sls_detector_channel));
int receiveChannel(int file_des, sls_detector_channel *myChan) {
return receiveDataOnly(file_des,myChan,sizeof(sls_detector_channel));
}
int receiveChip(sls_detector_chip* myChip) {
int receiveChip(int file_des, sls_detector_chip* myChip) {
int *ptr=myChip->chanregs;
int ts=0;
int nChans, nchanold=myChip->nchan, chdiff;
ts+= receiveDataOnly(myChip,sizeof(sls_detector_chip));
ts+= receiveDataOnly(file_des,myChip,sizeof(sls_detector_chip));
myChip->chanregs=ptr;
@ -401,12 +388,12 @@ int receiveChip(sls_detector_chip* myChip) {
#endif
if (chdiff<=0)
ts+=receiveDataOnly(myChip->chanregs, sizeof(int)*nChans);
ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nChans);
else {
ptr=malloc(chdiff*sizeof(int));
myChip->nchan=nchanold;
ts+=receiveDataOnly(myChip->chanregs, sizeof(int)*nchanold);
ts+=receiveDataOnly(ptr, sizeof(int)*chdiff);
ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nchanold);
ts+=receiveDataOnly(file_des,ptr, sizeof(int)*chdiff);
free(ptr);
return FAIL;
}
@ -417,7 +404,7 @@ int receiveChip(sls_detector_chip* myChip) {
return ts;
}
int receiveModule(sls_detector_module* myMod) {
int receiveModule(int file_des, sls_detector_module* myMod) {
float *dacptr=myMod->dacs;
@ -430,7 +417,7 @@ int receiveModule(sls_detector_module* myMod) {
int nAdcs, naold=myMod->nadc, nadcdiff;
ts+= receiveDataOnly(myMod,sizeof(sls_detector_module));
ts+= receiveDataOnly(file_des,myMod,sizeof(sls_detector_module));
myMod->dacs=dacptr;
myMod->adcs=adcptr;
@ -478,57 +465,57 @@ int receiveModule(sls_detector_module* myMod) {
printf("received %d adcs\n",nAdcs);
#endif
if (ndacdiff<=0) {
ts+=receiveDataOnly(myMod->dacs, sizeof(float)*nDacs);
ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(float)*nDacs);
#ifdef VERBOSE
printf("dacs received\n");
#endif
} else {
dacptr=malloc(ndacdiff*sizeof(float));
myMod->ndac=ndold;
ts+=receiveDataOnly(myMod->dacs, sizeof(float)*ndold);
ts+=receiveDataOnly(dacptr, sizeof(float)*ndacdiff);
ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(float)*ndold);
ts+=receiveDataOnly(file_des,dacptr, sizeof(float)*ndacdiff);
free(dacptr);
return FAIL;
}
if (nadcdiff<=0) {
ts+=receiveDataOnly(myMod->adcs, sizeof(float)*nAdcs);
ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(float)*nAdcs);
#ifdef VERBOSE
printf("adcs received\n");
#endif
} else {
adcptr=malloc(nadcdiff*sizeof(float));
myMod->nadc=naold;
ts+=receiveDataOnly(myMod->adcs, sizeof(float)*naold);
ts+=receiveDataOnly(adcptr, sizeof(float)*nadcdiff);
ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(float)*naold);
ts+=receiveDataOnly(file_des,adcptr, sizeof(float)*nadcdiff);
free(adcptr);
return FAIL;
}
if (nchipdiff<=0) {
ts+=receiveDataOnly(myMod->chipregs, sizeof(int)*nChips);
ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nChips);
#ifdef VERBOSE
printf("chips received\n");
#endif
} else {
chipptr=malloc(nchipdiff*sizeof(int));
myMod->nchip=nchipold;
ts+=receiveDataOnly(myMod->chipregs, sizeof(int)*nchipold);
ts+=receiveDataOnly(chipptr, sizeof(int)*nchipdiff);
ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nchipold);
ts+=receiveDataOnly(file_des,chipptr, sizeof(int)*nchipdiff);
free(chipptr);
return FAIL;
}
if (nchandiff<=0) {
ts+=receiveDataOnly(myMod->chanregs, sizeof(int)*nChans);
ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nChans);
#ifdef VERBOSE
printf("chans received\n");
#endif
} else {
chanptr=malloc(nchandiff*sizeof(int));
myMod->nchan=nchanold;
ts+=receiveDataOnly(myMod->chanregs, sizeof(int)*nchanold);
ts+=receiveDataOnly(chanptr, sizeof(int)*nchandiff);
ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nchanold);
ts+=receiveDataOnly(file_des,chanptr, sizeof(int)*nchandiff);
free(chanptr);
return FAIL;
}

View File

@ -14,19 +14,23 @@
#include "sls_detector_defs.h"
int bindSocket(unsigned short int port_number);
int acceptConnection();
void closeConnection();
void exitServer();
int sendDataOnly(void* buf,int length);
int receiveDataOnly(void* buf,int length);
char lastClientIP[INET_ADDRSTRLEN];
char thisClientIP[INET_ADDRSTRLEN];
int lockStatus;
int getServerError();
int sendChannel(sls_detector_channel *myChan);
int sendChip(sls_detector_chip *myChip);
int sendModule(sls_detector_module *myMod);
int receiveChannel(sls_detector_channel *myChan);
int receiveChip(sls_detector_chip* myChip);
int receiveModule(sls_detector_module* myMod);
int bindSocket(unsigned short int port_number);
int acceptConnection(int socketDescriptor);
void closeConnection(int file_Des);
void exitServer(int socketDescriptor);
int sendDataOnly(int file_des, void* buf,int length);
int receiveDataOnly(int file_des, void* buf,int length);
int getServerError(int socketDescriptor);
int sendChannel(int file_des, sls_detector_channel *myChan);
int sendChip(int file_des, sls_detector_chip *myChip);
int sendModule(int file_des, sls_detector_module *myMod);
int receiveChannel(int file_des, sls_detector_channel *myChan);
int receiveChip(int file_des, sls_detector_chip* myChip);
int receiveModule(int file_des, sls_detector_module* myMod);
#endif

View File

@ -383,6 +383,12 @@ enum correctionFlags {
I0_NORMALIZATION
};
enum portType {
CONTROL_PORT, /**< control port */
STOP_PORT, /**<stop port */
DATA_PORT /**< data port */
};
/**
function indexes to call on the server
@ -466,9 +472,11 @@ enum {
F_EXIT_SERVER, /**< turn off detector server */
F_GET_TEMPERATURE,
F_SET_GOTTHARD,
F_GET_GOTTHARD
F_LOCK_SERVER, /**< Locks/Unlocks server communication to the given client */
F_GET_LAST_CLIENT_IP, /**< returns the IP of the client last connected to the detector */
F_SET_PORT /**< Changes communication port of the server */
/* Always append functions hereafter!!! */