00001 #include "Client.h"
00002 Client::Client()
00003 {
00004 host = gethostbyname("127.0.0.1");
00005 server_addr.sin_family = AF_INET;
00006 server_addr.sin_port = htons(5000);
00007 server_addr.sin_addr = *((struct in_addr *)host->h_addr);
00008 bzero(&(server_addr.sin_zero),8);
00009 initiate = false;
00010 char *message1 = "Thread 1";
00011 char *message2 = "Thread 2";
00012
00013
00014
00015 create_thread();
00016 }
00017
00018 Client::~Client()
00019 {
00020 close(sock);
00021 }
00022
00023 void* proxy_function(void* foo_ptr){
00024 static_cast<Client*>(foo_ptr)->thread_function();
00025 }
00026
00027 void Client::create_thread(){
00028 pthread_t thread;
00029 thread=pthread_create(&thread, NULL, proxy_function, this);
00030 }
00031
00032 void Client::thread_function()
00033 {
00034 while(1)
00035 {
00036 bytes_recieved=recv(sock,recv_data,1024,0);
00037 recv_data[bytes_recieved] = '\0';
00038
00039 }
00040 }
00041
00042 void Client::explore()
00043 {
00044 printf("Thread");
00045 }
00046 void* Client::print_message_function( void *ptr )
00047 {
00048 char *message;
00049 message = (char *) ptr;
00050 printf("%s \n", message);
00051 }
00052
00053 void Client::start_stopBroadcasting(int frqy)
00054 {
00055 unsigned char frq = (unsigned char) frqy;
00056 delim = 2;
00057 printf("function");
00058 command = 1;
00059 length = 1;
00060 memcpy(nbuffer,&delim, sizeof(delim));
00061 memcpy(&nbuffer[sizeof(delim)],&command, sizeof(command));
00062 memcpy(&nbuffer[sizeof(delim)+sizeof(command)], &length, sizeof(length));
00063 memcpy(&nbuffer[sizeof(delim)+sizeof(command)+sizeof(length)], &frq, sizeof(frq));
00064 send(sock,(void*)&nbuffer,sizeof(nbuffer), 0);
00065 printf("\nCOMMAND SENT: delim = %i , command = %i , length = %d, Broadcastfrequency = %d" ,*nbuffer, *(&nbuffer[sizeof(delim)]), *(&nbuffer[sizeof(delim)+sizeof(command)]),frq);
00066 }
00067
00068 void Client::on_offChannel(char channel, char status)
00069 {
00070 delim = 2;
00071 command = 4;
00072 length = 2;
00073 memcpy(nbuffer,&delim, sizeof(delim));
00074 memcpy(&nbuffer[sizeof(delim)],&command, sizeof(command));
00075 memcpy(&nbuffer[sizeof(delim)+sizeof(command)], &length, sizeof(length));
00076 memcpy(&nbuffer[sizeof(delim)+sizeof(command)+sizeof(length)], &channel, sizeof(channel));
00077 memcpy(&nbuffer[sizeof(delim)+sizeof(command)+sizeof(length)+sizeof(channel)], &status, sizeof(status));
00078 send(sock,(void*)&nbuffer,sizeof(nbuffer), 0);
00079 printf("\nCOMMAND SENT: delim = %i , command = %i , length = %d , channel#= %c, ON/OFF= %c" ,*nbuffer, *(&nbuffer[sizeof(delim)]), *(&nbuffer[sizeof(delim)+sizeof(command)]),*(&nbuffer[sizeof(delim)+sizeof(command)+sizeof(length)]),*(&nbuffer[sizeof(delim)+sizeof(command)+sizeof(length)+sizeof(channel)]));
00080 }
00081
00082
00083 void Client::re_initClient()
00084 {
00085 close(sock);
00086 Client();
00087 initClient();
00088 }
00089
00090 void Client::initClient()
00091 {
00092 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00093 perror("Socket");
00094 exit(1);
00095 }
00096 if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
00097 perror("Connection error!");
00098 exit(1);
00099 }
00100 initiate = true;
00101 }
00102
00103 void Client::receiveFromServer()
00104 {
00105
00106
00107 bytes_recieved=recv(sock,recv_data,1024,0);
00108 recv_data[bytes_recieved] = '\0';
00109
00110
00111 char command = recv_data[1];
00112 ushort length = *(&recv_data[2]);
00113 int channels = *(&recv_data[4]);
00114
00115 if(command == 0)
00116 {
00117 int offset = 5;
00118 for(int channel=0;channel<channels;channel++)
00119 {
00120
00121 offset = offset + 8;
00122 }
00123 }else
00124 {
00125 int offset = 5;
00126 for(int channel=0;channel<channels;channel++)
00127 {
00128
00129 offset = offset + 9;
00130 }
00131 }
00132
00133 }
00134
00135
00136 void Client::requestMeasurments()
00137 {
00138 delim = 2;
00139 command = 3;
00140 length = 0;
00141 memcpy(nbuffer,&delim, sizeof(delim));
00142 memcpy(&nbuffer[sizeof(delim)],&command, sizeof(command));
00143 memcpy(&nbuffer[sizeof(delim)+sizeof(command)], &length, sizeof(length));
00144 send(sock,(void*)&nbuffer,sizeof(nbuffer), 0);
00145 printf("\nCOMMAND SENT: delim = %i , command = %i , length = %i " ,nbuffer[0], nbuffer[sizeof(delim)], nbuffer[sizeof(delim)+sizeof(command)]);
00146 }