|
| 1 | +extern "C"{ |
| 2 | +#include <sys/socket.h> |
| 3 | +#include <sys/types.h> |
| 4 | +#include <netinet/in.h> |
| 5 | +#include <netdb.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include <errno.h> |
| 11 | +#include <arpa/inet.h> |
| 12 | +#include <ctype.h> |
| 13 | +#include <stdarg.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdint.h> |
| 16 | +#include <inttypes.h> |
| 17 | +} |
| 18 | +#include <string> |
| 19 | +#include <iostream> |
| 20 | +#include "protocolBaseServer.h" |
| 21 | +#include "todoFactory.h" |
| 22 | +#include <memory> |
| 23 | +// various bits for floating point types-- |
| 24 | +// varies for different architectures |
| 25 | +typedef float float32_t; |
| 26 | +typedef double float64_t; |
| 27 | + |
| 28 | +/* |
| 29 | +** packi16() -- store a 16-bit int into a char buffer (like htons()) |
| 30 | +*/ |
| 31 | +void packi16(unsigned char *buf, unsigned int i) |
| 32 | +{ |
| 33 | + *buf++ = i>>8; *buf++ = i; |
| 34 | +} |
| 35 | + |
| 36 | +/* |
| 37 | +** packi32() -- store a 32-bit int into a char buffer (like htonl()) |
| 38 | +*/ |
| 39 | +/* |
| 40 | +void packi32(unsigned char *buf, unsigned long i) |
| 41 | +{ |
| 42 | + *buf++ = i>>24; *buf++ = i>>16; |
| 43 | + *buf++ = i>>8; *buf++ = i; |
| 44 | +} |
| 45 | + |
| 46 | +bool DoRead(const int & sock,void * data,uint32_t size){ |
| 47 | + uint32_t bytesRead=0; |
| 48 | + while(bytesRead!=size){ |
| 49 | + uint32_t readThisTime; |
| 50 | + do{ |
| 51 | + readThisTime=recv(sock,data+bytesRead,size-bytesRead,0); // Receive the message length |
| 52 | + }while((readThisTime == -1) && (errno == EINTR)); |
| 53 | + if(readThisTime<0){ |
| 54 | + std::cout << "read failure" << std::endl; |
| 55 | + return false; |
| 56 | + } |
| 57 | + bytesRead+=readThisTime; |
| 58 | + } |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +bool DoWrite(const int & sock,void * data,uint32_t size){ |
| 64 | + uint32_t bytesWrite=0; |
| 65 | +// std::cout << "size: " << size << st::endl; |
| 66 | + while(bytesWrite!=size){ |
| 67 | + uint32_t WriteThisTime; |
| 68 | + do{ |
| 69 | + WriteThisTime=send(sock,data+bytesWrite,size-bytesWrite,0); // Receive the message length |
| 70 | + }while((WriteThisTime == -1) && (errno == EINTR)); |
| 71 | + if(WriteThisTime<0){ |
| 72 | + std::cout << "Write failure" << std::endl; |
| 73 | + return false; |
| 74 | + } |
| 75 | + bytesWrite+=WriteThisTime; |
| 76 | + } |
| 77 | + std::cout << "bytes Write: " << bytesWrite << std::endl; |
| 78 | + return true; |
| 79 | +} |
| 80 | + |
| 81 | +*/ |
| 82 | +int main(void) |
| 83 | +{ |
| 84 | + int sockfd = 0,n = 0; |
| 85 | + unsigned char sendBuff[2]; |
| 86 | + struct sockaddr_in serv_addr; |
| 87 | + int i = 200; |
| 88 | + packi16(sendBuff,i); |
| 89 | + |
| 90 | + // data |
| 91 | + // strcat(total,(char*)sendBuffFloat); |
| 92 | + if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0) |
| 93 | + { |
| 94 | + printf("\n Error : Could not create socket \n"); |
| 95 | + return 1; |
| 96 | + } |
| 97 | + |
| 98 | + serv_addr.sin_family = AF_INET; |
| 99 | + serv_addr.sin_port = htons(3000); |
| 100 | + serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 101 | + |
| 102 | + if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0) |
| 103 | + { |
| 104 | + printf("\n Error : Connect Failed \n"); |
| 105 | + return 1; |
| 106 | + } |
| 107 | +/* |
| 108 | + n = write(sockfd, sendBuff, sizeof(sendBuff)); |
| 109 | + while(n!=strlen((char*)sendBuff)&&n!=-1 ){ |
| 110 | + n = write(sockfd, sendBuff, sizeof(sendBuff)); |
| 111 | + } |
| 112 | + if(n<0){ |
| 113 | + printf("Error Sending Data\n"); |
| 114 | + } |
| 115 | +*/ |
| 116 | +//framing up the execute class from the client side |
| 117 | +//here we are sending an execute statement and expecting some feedback |
| 118 | +//the type identifier goes first 0x01 |
| 119 | +//the server reads this and knows what action it is to take |
| 120 | +char mtype=0x01; |
| 121 | +std::string smtype(1,mtype); |
| 122 | +std::string dataToSend = "ls -al /home/ajonen"; |
| 123 | + |
| 124 | +std::unique_ptr<todoFactory> tmpTodoFactory; //create factory |
| 125 | +std::unique_ptr<aTodo> anExecTodo=tmpTodoFactory->createAtodo(mtype,dataToSend); //create ExecTodo from factory |
| 126 | +protocolBaseServer pBase(sockfd); //create socket connection |
| 127 | +anExecTodo->SendBack(pBase);//send the exec through the socket for running |
| 128 | +//now read back in results , this functionality should be added to the factory as a member function |
| 129 | +//readATodo |
| 130 | +pBase.DoRead(); |
| 131 | +char tmpType=pBase.getTotalMessage().at(0); |
| 132 | +std::string remainder=pBase.getTotalMessage().substr(1); |
| 133 | +//reset |
| 134 | +anExecTodo=tmpTodoFactory->retrieveAtodo(tmpType,remainder); |
| 135 | +//dataToSend=mtype+dataToSend; |
| 136 | +//std::cout << "datatosend: " << dataToSend << std::endl; |
| 137 | +//std::cout << "size: " << dataToSend.length() << std::endl; |
| 138 | + |
| 139 | +//pBase.DoWrite(smtype); |
| 140 | + |
| 141 | +//uint32_t dataLength = htonl(dataToSend.size()); // Ensure network byte order |
| 142 | +//bool ok; |
| 143 | +//ok=DoWrite(sockfd,&dataLength,sizeof(uint32_t)); |
| 144 | +//ok=DoWrite(sockfd,&(dataToSend[0]),dataToSend.size()); |
| 145 | +//std::cout << "Send ok: "<< ok << std::endl; |
| 146 | + |
| 147 | +/* |
| 148 | + * now we are going to read in the results of std::cout and std::cerr |
| 149 | +//this needs to be debugged further |
| 150 | +pBase.DoRead(); |
| 151 | +std::string strLengthOfOut=pBase.getTotalMessage().substr(0,sizeof(int32_t)); |
| 152 | +//remove the length of stdcout from the message |
| 153 | +pBase.setMessage(pBase.getTotalMessage().substr(sizeof(int32_t))); |
| 154 | +//extract the length of std out which is the first 4 bytes in network byte order |
| 155 | +char clengthOfOut[sizeof(int32_t)]; |
| 156 | +strcpy(clengthOfOut,strLengthOfOut.substr(0,sizeof(int32_t)).c_str()); |
| 157 | +int32_t lengthOfStdOut; |
| 158 | +memcpy(&lengthOfStdOut,clengthOfOut+0,sizeof(int32_t)); |
| 159 | +//convert to host byte order |
| 160 | +lengthOfStdOut=ntohl(lengthOfStdOut); |
| 161 | +//remove stdOut from string; |
| 162 | +std::string stdOut=pBase.getTotalMessage().substr(0,lengthOfStdOut); |
| 163 | +std::cout << "stdOut: " << stdOut << std::endl; |
| 164 | +//reset totalMessage |
| 165 | +*/ |
| 166 | +//int num=send(sockfd,&dataLength ,sizeof(uint32_t) ,MSG_CONFIRM); // Send the data length |
| 167 | +//int numSent=send(sockfd,dataToSend.c_str(),dataToSend.size(),MSG_CONFIRM); // Send the string |
| 168 | +//std::cout << "actual sent: " << numSent; |
| 169 | + |
| 170 | + |
| 171 | + return 0; |
| 172 | +} |
0 commit comments