Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "csapp.h"
- #include "smallServerLib.c"
- #define BUFFLEN 256
- #define SET 0
- #define GET 1
- #define DIGEST 2
- #define RUN 3
- void set(char* variableName, char* value, char** varnamearray, char** valarray, int* counter);
- char* get(char* variableName, char** varnamearray, char** valarray, int counter);
- int main(int argc, char** argv){
- //initialize all needed variables
- int listenfd, connfd, port, SecretKey;
- socklen_t clientLen;
- rio_t rio;
- struct sockaddr_in clientaddr;
- struct hostent *hp;
- char *haddrp;
- char** varnamearray;
- char** valarray;
- int counter =0;
- SecretKey = atoi(argv[2]);
- port = atoi(argv[1]);
- //listen for connection request
- listenfd = Open_listenfd(port);
- //establish connection
- while(1){
- clientLen = sizeof(clientaddr);
- connfd = Accept(listenfd, (SA*)&clientaddr, &clientLen);
- hp = Gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr),AF_INET);
- haddrp = inet_ntoa(clientaddr.sin_addr);
- printf("%s %s\n", hp->h_name, haddrp);
- Rio_readinitb(&rio, connfd);
- //initialize init struct
- struct init_request sireq;
- Rio_readn(connfd, &sireq, sizeof(sireq));
- struct init_response sires;
- //if secret key is wrong, replies -1
- if ((long)SecretKey != ntohl(sireq.secretKey)){
- printf("\nSecret key = %d\n", ntohl(sireq.secretKey));
- printf("--------------------------\n");
- //return to client
- sires.returnCode = -1;
- Rio_writen(connfd, &sires, sizeof(sires));
- }
- //else do this switch if statement tree
- else{
- //SET
- if(ntohs(sireq.requestType == SET)){
- //return to client
- sires.returnCode = 0;
- Rio_writen(connfd, &sires, sizeof(sires));
- char svariableName[15];
- char svalue[100];
- struct set_request sreq;
- Rio_readn(connfd, &sreq, 19+strlen(sreq.val));
- strcpy(svariableName, sreq.varName);
- strcpy(svalue, sreq.val);
- set(svariableName, svalue, varnamearray, valarray, &counter);
- }
- //GET
- else if(ntohs(sireq.requestType) == GET){
- //return to client
- sires.returnCode = 0;
- Rio_writen(connfd, &sires, sizeof(sires));
- char svariableName[15];
- struct get_request sreq;
- Rio_readn(connfd, &sreq, sizeof(svariableName));
- strcpy(svariableName, sreq.varName);
- char* retVal = get(svariableName, varnamearray, valarray, counter);
- printf("return value: %s\n", retVal);
- }
- //DIGEST
- else if(ntohl(sireq.requestType) == DIGEST){
- printf("digest");
- digest();
- }
- //RUN
- else if(ntohl(sireq.requestType) == RUN){
- printf("run");
- run();
- }
- //EVERYTHING ELSE
- else {
- sires.returnCode = -1;
- }
- printf("you made it all the way through once\n");
- }
- }
- return 0;
- }
- //helper functions
- void set(char* variableName, char* value, char** varnamearray, char** valarray, int* counter){
- //add values and variables to arrays
- varnamearray[*counter] = variableName;
- valarray[*counter] = value;
- printf("name: %s\n", varnamearray[*counter]);
- printf("value: %s\n", valarray[*counter]);
- (counter)++;
- }
- char* get(char* variableName, char** varnamearray, char** valarray, int counter){
- //find index of variableName
- printf("namecorrect: %s\n", variableName);
- printf("counter: %d\n", counter);
- for (int i=0; i<counter; i++){
- printf("namearray: %s\n", varnamearray[i]);
- printf("valuearray: %s\n", valarray[i]);
- if (varnamearray[i]==variableName){
- printf("hiday");
- return valarray[i];
- }
- }
- return 0;
- }
- int digest(){
- return 0;
- }
- int run(){
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement