works with gigabit

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@94 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
l_maliakal_d 2012-01-13 09:39:02 +00:00
parent 9f7fe775e8
commit f0ec4036be
6 changed files with 993 additions and 627 deletions

View File

@ -1,7 +1,7 @@
#include "communication_funcs.h"
#include <sys/socket.h>
//#include <sys/socket.h>
#include <netinet/tcp.h> /* for TCP_NODELAY */
#include <stdlib.h>
#include <string.h>
@ -10,11 +10,14 @@
//int socketDescriptor, file_des;
int socketDescriptor, file_des;
const int send_rec_max_size=SEND_REC_MAX_SIZE;
extern int errno;
char dummyClientIP[INET_ADDRSTRLEN];
//struct sockaddr_in address;
//#define VERBOSE
@ -23,9 +26,10 @@ int bindSocket(unsigned short int port_number) {
int i;
struct sockaddr_in addressS;
int socketDescriptor;
//int file_des;
file_des= -1;
//file_des= -1;
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
//socketDescriptor = socket(PF_INET, SOCK_STREAM, 0);
@ -33,7 +37,7 @@ int bindSocket(unsigned short int port_number) {
if (socketDescriptor < 0) {
printf("Cannot create socket..socketdescript less than 0\n");
printf("Can not create socket\n");
} else {
i = 1;
@ -51,7 +55,7 @@ int bindSocket(unsigned short int port_number) {
if(bind(socketDescriptor,(struct sockaddr *) &addressS,sizeof(addressS))<0){
printf("Cannot create socket..did not bind\n");
printf("Can not create socket\n");
socketDescriptor=-1;
} else {
@ -68,76 +72,39 @@ int bindSocket(unsigned short int port_number) {
return socketDescriptor;
}
/*
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=-1;
//socklen_t address_length;
size_t address_length=sizeof(struct sockaddr_in);
if(file_des>0) return file_des;
// if(file_des>0) return file_des;
//next 3 lines commented out by dhanya
//#ifndef C_ONLY
// if(is_a_server){ //server; the server will wait for the clients connection
//#endif
if (socketDescriptor>0) {
if ((file_des = accept(socketDescriptor,(struct sockaddr *) &addressC, &address_length)) < 0) {
//if ((file_des = accept(socketDescriptor,(struct sockaddr *) &address, &address_length)) < 0) {
if ((file_des = accept(socketDescriptor,(struct sockaddr *) &addressC, &address_length)) < 0) {
printf("Error: with server accept, connection refused %d\n", errno);
@ -193,26 +160,9 @@ int acceptConnection() {
socketDescriptor=-1;
}
#ifdef VERBOSE
inet_ntop(AF_INET, &(addressC.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
struct sockaddr_in6 sa;
char str[INET6_ADDRSTRLEN];
// store this IP address in sa:
inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr));
// now get it back and print it
inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);
//printf("%s\n\n", str); // prints "2001:db8:8714:3a90::12"
printf("client connected %d\n", file_des);
printf("addressC family %d port %d addr %s\n",addressC.sin_family,addressC.sin_port,inet_ntoa(addressC.sin_addr));//edited
#endif
//strcpy(lastClientIP,inet_ntoa(addressC.sin_addr)); //In case you want to lock the server...
// struct sockaddr_in
@ -223,7 +173,7 @@ inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);
//unsigned char sin_zero[8]; /* Same size of struct sockaddr */
//};
}
return file_des;
}
@ -234,7 +184,7 @@ inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);
void closeConnection() {
void closeConnection(int file_des) {
//fflush(stdout);
//printf("Closing file_des %d\n", file_des);
//sleep(1);
@ -245,7 +195,7 @@ void closeConnection() {
file_des=-1;
}
void exitServer() {
void exitServer(int socketDescriptor) {
if (socketDescriptor>=0)
close(socketDescriptor);
#ifdef VERY_VERBOSE
@ -257,55 +207,14 @@ void exitServer() {
/* client close conenction */
/*
#ifndef C_ONLY
void MySocketTCP::Disconnect(){
if(file_des>=0){ //then was open
if(is_a_server){
close(file_des);
}
else {
close(socketDescriptor);
socketDescriptor=-1;
}
file_des=-1;
}
int sendDataOnly(int file_des, void* buf,int length) {
}
#endif
*/
int sendDataOnly(void* buf,int length) {
/*
int total_sent=0;
int nsending;
int nsent;
#ifdef VERY_VERBOSE
printf("want to send %d Bytes\n", length);
#endif
if (file_des<0) return -1;
while(length>0){
nsending = (length>send_rec_max_size) ? send_rec_max_size:length;
nsent = write(file_des,(char*)buf+total_sent,nsending);
if(!nsent) break;
length-=nsent;
total_sent+=nsent;
// cout<<"nsent: "<<nsent<<endl;
}
*/
return write(file_des, buf, length);
}
int receiveDataOnly(void* buf,int length) {
int receiveDataOnly(int file_des, void* buf,int length) {
int total_received=0;
int nreceiving;
@ -335,7 +244,14 @@ int sendDataOnly(void* buf,int length) {
#ifdef VERY_VERBOSE
printf("received %d Bytes\n", total_received);
#endif
if (total_received>0)
strcpy(thisClientIP,dummyClientIP);
if (strcmp(lastClientIP,thisClientIP))
differentClients=1;
else
differentClients=0;
return total_received;
}
@ -353,44 +269,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
@ -400,17 +316,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;
@ -427,12 +343,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;
}
@ -443,7 +359,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;
@ -456,7 +372,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;
@ -504,57 +420,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,24 @@
#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 differentClients;
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

@ -944,9 +944,8 @@ int setSettings(int i)
/* Initialization*/
int initChannelbyNumber(sls_detector_channel myChan) {
int reg=myChan.reg;
int ft=reg & TRIM_DR;

View File

@ -5,7 +5,7 @@
#include <stdlib.h>
int sockfd;
extern int sockfd;
void error(char *msg)
@ -18,7 +18,9 @@ int main(int argc, char *argv[])
int portno, b;
char cmd[100];
int retval=OK;
int sd, fd;
if (argc==1) {
portno = DEFAULT_PORTNO;
sprintf(cmd,"%s %d &",argv[0],DEFAULT_PORTNO+1);
@ -37,8 +39,12 @@ int main(int argc, char *argv[])
init_detector(b);
bindSocket(portno);
if (getServerError()) {
sd=bindSocket(portno);
sockfd=sd;
if (getServerError(sd)) {
printf("server error!\n");
return -1;
}
@ -58,21 +64,21 @@ int main(int argc, char *argv[])
#ifdef VERY_VERBOSE
printf("Waiting for client call\n");
#endif
acceptConnection();
fd=acceptConnection(sockfd);
#ifdef VERY_VERBOSE
printf("Conenction accepted\n");
#endif
retval=decode_function();
#ifdef VERY_VERBOSE
retval=decode_function(fd);
//#ifdef VERY_VERBOSE
printf("function executed\n");
#endif
closeConnection();
//#endif
closeConnection(fd);
#ifdef VERY_VERBOSE
printf("connection closed\n");
#endif
}
exitServer();
exitServer(sockfd);
printf("Goodbye!\n");
return 0;

File diff suppressed because it is too large Load Diff

View File

@ -12,15 +12,21 @@
#define GOODBYE -200
int sockfd;
int function_table();
int decode_function();
int decode_function(int);
int init_detector(int);
int M_nofunc(int);
int exit_server(int);
// General purpose functions
int get_detector_type(int);
int set_number_of_modules(int);
@ -61,11 +67,12 @@ int get_roi(int);
int set_speed(int);
int set_readout_flags(int);
int execute_trimming(int);
// to take out/not by dhanya..
int getGotthard(int);
int setGotthard(int);
float get_temperature(int);
int lock_server(int);
int set_port(int);
int get_last_client_ip(int);
int update_client(int);
int send_update(int);
int configure_mac(int);
#endif