connections.h File Reference

#include <netinet/in.h>
#include <signal.h>
Include dependency graph for connections.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define DBPORT   9000
#define MAXBUFLEN   2000

Functions

void setup ()
void DB_udp ()
void * DB_udphandler (void *udp_BUF)
void DB_process_UDP (unsigned char *udp_BUF)

Variables

int DB_sockfd
struct sockaddr_in DB_server_addr their_addr
int DB_addr_len
int numbytes
unsigned char buffer [MAXBUFLEN]

Define Documentation

#define DBPORT   9000

Definition at line 37 of file connections.h.

Referenced by setup().

#define MAXBUFLEN   2000

Definition at line 38 of file connections.h.

Referenced by DB_udp().


Function Documentation

void DB_process_UDP ( unsigned char *  udp_BUF  ) 

Definition at line 245 of file connections.c.

References cfgparser(), and dataparser().

Referenced by DB_udphandler().

00245                                            {
00246 
00247         int stat_status,i;
00248         unsigned char c = udp_BUF[1];
00249         c <<= 1;
00250         c >>= 5;
00251         if(c == 0x00){                                  /* If data frame */
00252 
00253                 printf("\nData frame\n");
00254                 stat_status = dataparser(udp_BUF);
00255                 printf("Return from DParser\n");
00256 
00257         } else if(c == 0x03) {                  /* If configuration frame */
00258 
00259                 for(i=0;i<400;i++)printf("%x\t",udp_BUF[i]);
00260                 printf("\nConfiguration frame\n");
00261                 cfgparser(udp_BUF);
00262                 printf("Return from CFG\n");
00263                 
00264 
00265         } else {        
00266                 
00267                 printf("Erroneous frame\n");
00268 
00269         }       
00270         fflush(stdout);
00271 } 

Here is the call graph for this function:

Here is the caller graph for this function:

void DB_udp (  ) 

Definition at line 160 of file connections.c.

References buffer, copy_cbyc(), DB_addr_len, DB_sockfd, DB_udphandler(), MAXBUFLEN, mutex_on_buffer, numbytes, and their_addr.

Referenced by setup().

00160              {
00161 
00162         // UDP threads are created for each received UDP message in 'detached' mode. Thus allowing any number of threads to be created. 
00163         int err;
00164         pthread_attr_t attr;
00165         pthread_attr_init(&attr);
00166 
00167         if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In  the detached state, the thread resources are
00168                                                                                 // immediately freed when it terminates, but 
00169                 perror(strerror(err));                                          // pthread_join(3) cannot be used to synchronize
00170                 exit(1);                                                        //  on the thread termination.         
00171                                                                                 
00172         }                                                               
00173                                                                                                                                                                                                                                                                                           
00174         if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out)
00175 
00176                 perror(strerror(err));
00177                 exit(1);
00178         }  
00179 
00180 
00181         /* UDP data Received */
00182         while(1) {
00183 
00184                if ((numbytes = recvfrom(DB_sockfd,buffer, MAXBUFLEN-1, 0,(struct sockaddr *)&their_addr, (socklen_t *)&DB_addr_len)) == -1) { 
00185                                                                                                         // Main if
00186                          perror("recvfrom");
00187                          exit(1);
00188 
00189                 } else { /* New datagram has been received */
00190         
00191                         pthread_t t;
00192                         int tt;
00193                         unsigned char *udp_BUF;
00194                         udp_BUF = malloc(MAXBUFLEN*sizeof(unsigned char));
00195         
00196                         if(!udp_BUF) {
00197         
00198                                 printf("No space for udp_BUF\n");
00199                                 exit(1);
00200                         }
00201                         printf("\nUDP Server got packet from %s\n",inet_ntoa(their_addr.sin_addr));
00202                         printf("packet is %d bytes long\n",numbytes);
00203                         buffer[numbytes] = '\0';
00204         
00205                         /* Copy the udp_buf data to a local variable */
00206                         pthread_mutex_lock(&mutex_on_buffer);
00207                         copy_cbyc(udp_BUF,buffer,MAXBUFLEN);
00208                         memset(buffer, '\0', MAXBUFLEN);
00209                         pthread_mutex_unlock(&mutex_on_buffer);
00210         
00211                         /* Creates a new thread for each received UDP message */
00212                         if((tt = pthread_create(&t,&attr,DB_udphandler,(void *)udp_BUF))) {
00213                 
00214                                 perror(strerror(tt));   
00215                                 exit(1);
00216                         }               
00217         
00218                 } // Main if ends
00219 
00220          } // while ends
00221         
00222          pthread_attr_destroy(&attr);           
00223 } 

Here is the call graph for this function:

Here is the caller graph for this function:

void* DB_udphandler ( void *  udp_BUF  ) 

Definition at line 230 of file connections.c.

References DB_process_UDP().

Referenced by DB_udp().

00230                                     {
00231 
00232         DB_process_UDP((unsigned char*)udp_BUF);
00233 
00234         free((unsigned char*)udp_BUF);
00235 
00236         pthread_exit(NULL); /* Exit the thread once the task is done. */
00237                 
00238 }

Here is the call graph for this function:

Here is the caller graph for this function:

void setup (  ) 

Definition at line 68 of file connections.c.

References conn_cfg, conn_data, database, DB_addr_len, DB_sockfd, DB_udp(), DBPORT, password, server, and user.

Referenced by main().

00068             {
00069 
00070         printf("Entering setup()\n");
00071 
00072 /* MySQL data base parameters */
00073 
00074         server = "localhost";
00075         user = "root";
00076         password = "root";
00077         database = "openPDC";
00078 
00079 /* MySQL data base connection */
00080         
00081         conn_data = mysql_init(NULL);
00082         conn_cfg = mysql_init(NULL);
00083 
00084         if(conn_data == NULL) {
00085 
00086                 printf("No sufficient memory\n");
00087                 exit(1);
00088         }
00089 
00090         if(conn_cfg == NULL) {
00091 
00092                 printf("No sufficient memory\n");
00093                 exit(1);
00094         }
00095 
00096 /* Connect to database */
00097         if (!mysql_real_connect(conn_data, server,
00098                       user, password, database, 0, NULL, 0)) {
00099 
00100                 fprintf(stderr, "%s\n", mysql_error(conn_data));
00101                 exit(1);
00102         }
00103 
00104         if (!mysql_real_connect(conn_cfg, server,
00105                       user, password, database, 0, NULL, 0)) {
00106 
00107                 fprintf(stderr, "%s\n", mysql_error(conn_cfg));
00108                 exit(1);
00109         }
00110 
00111 /* Create UDP socket and bind to port */
00112         int yes;
00113         if ((DB_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00114 
00115                  perror("socket");
00116                  exit(1);
00117         
00118         } else {
00119 
00120                 printf("DB Socket:Sucessfully created\n");
00121 
00122         }       
00123 
00124         if (setsockopt(DB_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
00125                 perror("setsockopt");
00126                 exit(1);
00127         }
00128 
00129         DB_server_addr.sin_family = AF_INET;            // host byte order
00130         DB_server_addr.sin_port = htons(DBPORT);        // short, network byte order
00131         DB_server_addr.sin_addr.s_addr = INADDR_ANY;    // automatically fill with my IP
00132         memset(&(DB_server_addr.sin_zero),'\0', 8);     // zero the rest of the struct
00133 
00134         if (bind(DB_sockfd, (struct sockaddr *)&DB_server_addr,
00135                                      sizeof(DB_server_addr)) == -1) {
00136                  perror("bind");
00137                  exit(1);
00138         
00139         } else {
00140 
00141                 printf("DB Socket Bind :Sucessfull\n");
00142         } 
00143 
00144         printf("\nDB Server Listening on port %d\n",DBPORT);
00145 
00146         DB_addr_len = sizeof(struct sockaddr);
00147 
00148         DB_udp();
00149         mysql_close(conn_cfg);
00150         mysql_close(conn_data);
00151 
00152 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

unsigned char buffer[MAXBUFLEN]

Definition at line 43 of file connections.h.

Referenced by DB_udp().

Definition at line 42 of file connections.h.

Referenced by DB_udp(), and setup().

int DB_sockfd

Definition at line 40 of file connections.h.

Referenced by DB_udp(), and setup().

int numbytes

Definition at line 42 of file connections.h.

Referenced by DB_udp().

struct sockaddr_in DB_server_addr their_addr

Definition at line 41 of file connections.h.

Referenced by DB_udp().

Generated on Tue Jun 7 13:40:38 2011 for iPDC-DBServer by  doxygen 1.6.3