Board.cpp
Go to the documentation of this file.
00001 /*
00002 * Board.cpp provides functionality of a virtual board not real.
00003 *
00004 * Copyright (c).2012. OWNER: Institute for Software Technology, TU Graz Austria.
00005 * Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00006 * All rights reserved.
00007 *    This program is free software: you can redistribute it and/or modify
00008 *    it under the terms of the GNU General Public License as published by
00009 *    the Free Software Foundation, either version 3 of the License, or
00010 *    (at your option) any later version.
00011 *
00012 *    This program is distributed in the hope that it will be useful,
00013 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 *    GNU General Public License for more details.
00016 *
00017 *    You should have received a copy of the GNU General Public License
00018 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */ 
00020 
00021 //#include <iostream>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <errno.h>
00030 #include <string.h>
00031 #include <boost/thread.hpp>
00032 #include <stdlib.h>
00033 
00034 int BUFF_SIZE;
00035 int sock, connected, bytes_recieved , True = 1, broadcastFrq;  
00036 char send_data[255] , recv_data[255];
00037 unsigned char nbuffer[255];
00038 unsigned char * p;
00039 int channels;
00040 int MAX_channels = 8;
00041 struct board
00042 {
00043          char on_off[8];
00044          float max_vol[8];
00045          float max_cur[8];
00046          float pr_vol[8];
00047          float pr_cur[8];
00048 };
00049 struct board board_info; 
00050 struct sockaddr_in server_addr,client_addr;    
00051 int sin_size;
00052 
00053 void set_channels(void)
00054 {
00055             for(int channel=0;channel<MAX_channels;channel++)
00056            {
00057              if((channel%2)==0)
00058                { 
00059                  board_info.on_off[channel] = 0;
00060                  board_info.max_vol[channel] = channel*10+0.1*channel;
00061                  board_info.max_cur[channel] = channel*20+0.1*channel;
00062                  board_info.pr_vol[channel] = channel-1+0.1*channel;
00063                  board_info.pr_cur[channel] = channel+1+0.1*channel;
00064               }
00065              else
00066                 {
00067                  board_info.on_off[channel] = 0;
00068                  board_info.max_vol[channel] = channel*5+0.1*channel;
00069                  board_info.max_cur[channel] = channel*8+0.1*channel;
00070                  board_info.pr_vol[channel] = channel+0.1*channel;
00071                  board_info.pr_cur[channel] = channel+2+0.1*channel;
00072                  }
00073            }        
00074 
00075 }
00076 
00077 void take_boardSpecifications()
00078 {          
00079           p = nbuffer;
00080                                         channels = MAX_channels;
00081           char delim = 2;
00082           char command = 0;
00083           ushort length = 8*channels+1;
00084           
00085           *p = delim;
00086           p++;
00087           *p = command;
00088           p++;
00089           *((ushort *)p) = length;
00090           p+=2;
00091 
00092 
00093           char n = (char) channels;
00094           
00095          *p=n;
00096           p++;
00097           int offset = sizeof(delim)+sizeof(command)+sizeof(length)+sizeof(n);
00098           
00099           for(int channel=0;channel<channels;channel++)
00100           {
00101           float max_curr = board_info.max_cur[channel];
00102           float max_vol = board_info.max_vol[channel];
00103           *((float *)p) = max_curr;
00104           p+=4;
00105           *((float *)p) = max_vol;
00106           p+=4;
00107           }
00108           BUFF_SIZE = 4+1+8*channels;
00109           //printf("\n SENT DATA from Server: delim = %i , command = %i , length = %i , Channels = %i " , nbuffer[0], nbuffer[1], nbuffer[2] , nbuffer[4]);
00110           offset = 5;
00111           for(int channel=0;channel<channels;channel++)
00112           {
00113            //printf("\n Channel# : %d, Max_Vol= %f, Max_Cur= %f", channel,*((float *)(nbuffer+offset)), *((float *)(nbuffer+offset+4)));
00114            offset +=8;
00115           }
00116 }                
00117 
00118 void take_boardMeasurments()
00119 {         
00120           p = nbuffer;
00121                                         channels = 8;
00122           char delim = 2;
00123           char command = 2;
00124           ushort length = 9*channels+1;
00125           
00126           *p = delim;
00127           p++;
00128           *p = command;
00129           p++;
00130           *((ushort *)p) = length;
00131           p+=2;
00132 
00133 
00134           char n = (char) channels;
00135           
00136          *p=n;
00137           p++;
00138 
00139           for(int channel=0;channel<channels;channel++)
00140           {
00141           char on_off = board_info.on_off[channel];
00142           float pr_curr = board_info.pr_cur[channel];
00143           float pr_vol = board_info.pr_vol[channel];
00144           *p=on_off;
00145           p++;
00146           *((float *)p) = pr_curr;
00147           p+=4;
00148           *((float *)p) = pr_vol;
00149           p+=4;
00150           }
00151 
00152           BUFF_SIZE = 4+1+9*channels;
00153           /*printf("\n SENT DATA from Server: delim = %i , command = %i , length = %i , Channels = %i " , nbuffer[0], nbuffer[1], nbuffer[2] , nbuffer[4]);
00154 
00155           int offset = 5;
00156           for(int channel=0;channel<channels;channel++)
00157           {
00158            printf("\n Channel# : %d, On/Off= %i, Present_Curr= %f, Present_Vol= %f", channel,nbuffer[offset] ,*((float *)(nbuffer+offset+1)), *((float *)(nbuffer+offset+5)));
00159            offset+=9;
00160           }*/
00161           
00162                                         
00163 }
00164 
00165 bool check_Command(unsigned char *buf)
00166 { 
00167   //printf("\n RECIEVED from Client:\ndelim = %i , command = %i , length = %d" ,*recv_data, *(&recv_data[1]), *(&recv_data[2]),*((float *)(nbuffer+5)));
00168 }
00169 void send_Ack()
00170 {         
00171           //bool correctCommand=check_Command();
00172           p = nbuffer;
00173           char delim = 2;
00174           char command = 5;
00175           ushort length = 1;
00176           
00177           *p = delim;
00178           p++;
00179           *p = command;
00180           p++;
00181           *((ushort *)p) = length;
00182           p+=2;
00183 
00184           char ecode = 0;
00185           
00186          *p=ecode;
00187           
00188           BUFF_SIZE = 5;
00189           send(connected,(void*)&nbuffer,BUFF_SIZE, 0);
00190           //printf("\n ACK from Server: delim = %i , command = %i , length = %i , ecode = %i " , nbuffer[0], nbuffer[1], nbuffer[2] , nbuffer[4]);
00191          
00192 }
00193 
00194 void switch_OnOffChannel(char channel, char status)
00195 {
00196 int ch;
00197 int state;
00198 ch = (int) channel;
00199 ch = ch - 48;
00200 //printf("%d channel to %c ",ch,status);
00201 state = (int) status;
00202 state = state - 48;
00203 if(state>1)
00204 state = 1;
00205 board_info.on_off[ch] = state;
00206 }
00207 
00208 void BoradcastWithFrq(int connected)
00209 {
00210 
00211 while(true)
00212  {
00213     
00214     take_boardMeasurments(); 
00215     send(connected,(void*)&nbuffer,BUFF_SIZE, 0);
00216     double Frq = ((double)1/broadcastFrq);
00217     sleep(Frq);
00218     //printf("broadcast frq = %d and wait = %f\n",broadcastFrq,Frq);
00219  }
00220 }
00221 
00222 void thread_for_client(int connected)
00223 {
00224             boost::thread broadcaster;
00225 
00226             take_boardSpecifications();
00227             char command=0;
00228             while (1)
00229             { 
00230               //printf("command = %d\n",command);
00231               if(command!=4)
00232               send(connected,(void*)&nbuffer,BUFF_SIZE, 0);
00233               bytes_recieved = recv(connected,recv_data,255,0);
00234               
00235                    
00236               command = recv_data[1];
00237               if(command==1)
00238                    {
00239                     printf("\n RECIEVED from Client(%d):\ndelim = %i, command = %i,length = %d, BroadCastFrequency = %d", connected ,*recv_data, *(&recv_data[1]), *(&recv_data[2]), *((unsigned char *)&recv_data[4]));
00240                     unsigned char b_frq = *((unsigned char *)&recv_data[4]);
00241                     broadcastFrq = (int) b_frq;
00242                     broadcaster.interrupt();
00243                     broadcaster = boost::thread(BoradcastWithFrq, connected);
00244                     
00245                    }
00246               else if(command==3)
00247                  { printf("\n RECIEVED from Client(%d):\ndelim = %i, command = %i,length = %i " ,connected ,*recv_data, *(&recv_data[1]), *(&recv_data[2]), *(&recv_data[4]), *(&recv_data[5]));
00248                    send_Ack();
00249                    take_boardMeasurments();
00250                  }
00251               else if(command==4)
00252                    {
00253                      switch_OnOffChannel(*(&recv_data[4]),*(&recv_data[5]));
00254                      printf("\n RECIEVED from Client(%d):\ndelim = %i, command = %i,length = %d,channel = %c, State = %c ", connected ,*recv_data, *(&recv_data[1]), *(&recv_data[2]), *(&recv_data[4]), *(&recv_data[5]));
00255                     send_Ack();
00256                    }
00257               fflush(stdout);
00258             }
00259 }
00260 
00261 
00262 
00263 int main()
00264 {
00265 
00266         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00267             perror("Socket");
00268             exit(1);
00269         }
00270 
00271         if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&True,sizeof(int)) == -1) {
00272             perror("Setsockopt");
00273             exit(1);
00274         }
00275         
00276         server_addr.sin_family = AF_INET;         
00277         server_addr.sin_port = htons(5000);     
00278         server_addr.sin_addr.s_addr = INADDR_ANY; 
00279         bzero(&(server_addr.sin_zero),8); 
00280 
00281         if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))
00282                                                                        == -1) {
00283             perror("Unable to bind");
00284             exit(1);
00285         }
00286 
00287         if (listen(sock, 5) == -1) {
00288             perror("Listen");
00289             exit(1);
00290         }
00291                 
00292               
00293         fflush(stdout);
00294         set_channels();
00295         boost::thread cthread;
00296         
00297         while(1)
00298         {  
00299 
00300             sin_size = sizeof(struct sockaddr_in);
00301             printf("TCPServer Waiting for client on port 5000.....\n");
00302             int client_sock = accept(sock, (struct sockaddr *)&client_addr, (socklen_t*)&sin_size);
00303 
00304             printf(" Got request for connection from (%s , %d)\n",
00305                    inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
00306             cthread = boost::thread(thread_for_client, client_sock);
00307       }       
00308 
00309       close(sock);
00310       return 0;
00311 } 
00312 


tug_ist_diagnosis_board
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:23