User:Artagnon/Sandbox

From Wikipedia, the free encyclopedia

[edit] Artagnon's personal sandbox

Mission Mars Code

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
 
#define SERVPORT 5003
#define BUFLEN 1000
 
int botID;
char type;
 
typedef  struct S{
 
        int n;         // vision radius, which defines 2n+1 square array
        char** vis;    // vision matrix of 2n+1 size, containg characters indicating height, obstacle, etc
        int** bots;    // it is a 2D array 2n+1 size
                        // -1 if no bot, botid if there is a bot
        char ino;      // In or outside exploration zone, '1' is in, '0' is out 
        int len;       // Length of local message
        char *loc;     // Local message
} env;
 
 
// FUNCTION TO RETURN THE STRUCTURE CONTAINING ELEMENTS OF PARAMETER
env parseString(char *par)
{env ob;
        char *str  =  NULL;
 
        int botc = 0;
        str  =  strtok(par, ";");
        ob.n  =  int(str[0]);
        str  =  strtok(NULL, ";");
        int l = 0;
        ob.vis  =  (char**)calloc(2*ob.n+1, sizeof(char*));
        ob.bots  =  (int**)calloc(2*ob.n+1, sizeof(int*));
        for(int i = 0; i<2*ob.n+1; i++){
                ob.vis[i]  =  (char*)calloc(2*ob.n+1, sizeof(char));
                ob.bots[i]  =  (int*)calloc(2*ob.n+1, sizeof(int));
                for(int j = 0; j<2*ob.n+1; j++){
                        ob.vis[i][j]  =  str[l];
                        if(str[l] = ='R'){
                                ob.bots[i][j]  =  (int)str[(2*ob.n+1)*(2*ob.n+1)+botc];
                                botc++;
                        }
                        else
                                ob.bots[i][j] = -1;
                        l++;
                }
        }
        str  =  strtok(NULL, ";");
        ob.ino  =  str[0];
        str  =  strtok(NULL, ";");
        if(str! = NULL){
                ob.len  =  int(str[0]);
                str  =  strtok(NULL, ";");
                ob.loc  =  str;
        }
        else{
                ob.len = 0;
                ob.loc = NULL;
        }
        return ob;
}
 
 
// FUNCTION TO GENERATE THE MESSAGE TO BE SENT TO THE SIMULATOR
char * ProcessMessageCommunicator (char *buf)
{/*
    Incase you want to use the parser, define an object of env, for e.g
    env object;
    object =  parseString(buf);
    Now your object is loaded with the values. Use the elements of the object as defined
    in the structure.
 
  */
        static char msg[100];
        char move[] = "nsew";
        int h,i,j;
        h = (int)buf[0];
        /*
           Code of generating the message string (ie move or read or write requests ) 
           must be written here
 
           Incase u dont want to return any message then use 
           return NULL, instead of return msg
 
           A sample code has been given, which prints the Environment Info if it is not 
           memory record, or the message sent by the simulator (ie if it is a memory record).
           Then it will generate a random move in 4 directions.
         */
        // taking input from user and sending that to the simulator
        // printf ("Enter your message: ");
        // scanf("%s",msg)
        if (buf[1] == ';')
        { 
                for(i = 0;i<2*h+1;i++)
                {
                        printf ("\n");
                        for (j = 0;j<2*h+1;j++)
                                printf ("%c", buf[i*(2*h+1)+2+j]);         
                }
                printf ("\n\n");
        }
        else
        {
                printf ("Message received from simulator: %s\n",buf);
        }
        /*
           Code for displaying elements of sample object
           printf("N of array : %d\n", ob.n);
           for( i = 0; i<2*ob.n+1; i++){
           for(j = 0; j<2*ob.n+1; j++){
           printf("%c", ob.vis[i][j]);
           }
           printf("\n");
           }
           printf("Input or output : %c \n", ob.ino);
         */
        msg[0] = move[rand()%4];
        msg[1] = ';';
        msg[2] = 'c';
        msg[3] = 'o';
        msg[4] = 'm';
        msg[5] = 'm';
        msg[6] = 0;
 
 
        return msg;
}
 
// FUNCTION TO GENERATE THE MESSAGE TO BE SENT TO THE SIMULATOR
char * ProcessMessageExplorer (char *buf)
{/*
    Incase you want to use the parser, define an object of env, for e.g
    env object;
    object  =  parseString(buf);
    Now your object is loaded with the values. Use the elements of the object as defined
    in the structure.
 
  */
        static char msg[100];
        char move[] = "nsew";
        int h,i,j;
        h = (int)buf[0];
        /*
           Code of generating the message string (ie move or read or write requests ) 
           must be written here
 
           Incase u dont want to return any message then use 
           return NULL, instead of return msg
 
           A sample code has been given, which prints the Environment Info if it is not 
           memory record, or the message sent by the simulator (ie if it is a memory record).
           Then it will generate a random move in 4 directions.
         */
        // taking input from user and sending that to the simulator
        // printf ("Enter your message: ");
        // scanf("%s",msg)
        if (buf[1] == ';')
        { 
                for(i = 0;i<2*h+1;i++)
                {
                        printf ("\n");
                        for (j = 0;j<2*h+1;j++)
                                printf ("%c", buf[i*(2*h+1)+2+j]);         
                }
                printf ("\n\n");
        }
        else
        {
                printf ("Message received from simulator: %s\n",buf);
        }
        /*
           Code for displaying elements of sample object
           printf("N of array : %d\n", ob.n);
           for( i = 0; i<2*ob.n+1; i++){
           for(j = 0; j<2*ob.n+1; j++){
           printf("%c", ob.vis[i][j]);
           }
           printf("\n");
           }
           printf("Input or output : %c \n", ob.ino);
         */
        msg[0] = move[rand()%4];
        msg[1] = ';';
        msg[2] = 'e';
        msg[3] = 'x';
        msg[4] = 'p';
        msg[5] = 'l';
        msg[6] = 'r';
        msg[7] = 0;
 
        return msg;
}
 
 
/* 
   general format of the command line entry
 
   gcc linclient.c -o client
   ./client [<ip of the simulator>]
 
   the ip is optional, if not present it will assume the value of the 
   ip of the simulator as 127.0.0.1, that it will connect to the simulator 
   on the local machine. 
 
 */
int main(int argc, char *argv[])
{
        int sock,snd,bnd,rcv,len;
        struct sockaddr_in addsnd,addrcv,addfrm;
        char buf[BUFLEN],cnct[] = {"connect"},*ip = "127.0.0.1",*str;
        if (argc>1) ip = argv[1];
 
        addsnd.sin_family = AF_INET;
        addsnd.sin_port = htons(SERVPORT);
        inet_aton(ip,&addsnd.sin_addr);
        memset(&(addsnd.sin_zero),'\0',8);
 
        addrcv.sin_family = AF_INET;
        addrcv.sin_port = 0;
        addrcv.sin_addr.s_addr = INADDR_ANY;
        memset(&(addrcv.sin_zero),'\0',8);
 
        sock = socket(AF_INET,SOCK_DGRAM,0); // creating a socket 
 
        // binding the socket to a random unused port of the system
        bnd = bind(sock,(struct sockaddr*)&addrcv,sizeof(struct sockaddr)); 
        if (bnd<0)
        {
                printf ("\nBinding failure.\n");
                exit(0);
        }
        else
                printf ("\nBinding successful.\n");
 
        do   
        {
                // sending the connect signal
                snd = sendto (sock,cnct,strlen(cnct)+1,0,(struct sockaddr*)&addsnd,sizeof(struct sockaddr));
 
                if (snd>0)
                        printf("Request for connection sent.\n");
                else
                {
                        printf ("Failed to send connection request\n");
                        continue;
                }
                // checking for acknowledgement
                len = sizeof(struct sockaddr);
                rcv = recvfrom(sock,buf,BUFLEN-1,0,(struct sockaddr*)&addfrm,&len);
 
                if (rcv>0)
                {
                        buf[rcv] = 0;
                        sscanf(buf, "connected;%c;%d", &type, &botID);
                        printf ("Acknowledgement received: %s\n",buf);
                }
 
        } while  (strcmp(buf,"wait") = =0||snd<0);
 
        do  // loop for alternately sending the message and receiving message from simulator
        { 
                // receiving the message from the simulator
                len = sizeof(struct sockaddr);
                rcv = recvfrom(sock,buf,BUFLEN-1,0,(struct sockaddr*)&addfrm,&len);
                if (snd> = 0)
                        buf[rcv] = 0;
                else
                {
                        printf ("Failed to receive message\n");
                        break;
                }
                if (strcmp (buf,"connection terminated") = =0) break;
                // function to generate the message that has to be sent to the simulator  
                if(type == 'e')
                        str = ProcessMessageExplorer(buf);
                else
                        str = ProcessMessageCommunicator(buf);
 
                // sending the message to the simulator
                if (str != NULL)
                {
                        printf ("Message sent to simulator: %s\n", str);
                        snd = sendto (sock,str,strlen(str)+1,0,(struct sockaddr*)&addsnd,sizeof(struct sockaddr));
                        if (snd<0)
                        {
                                printf ("Failed to send the request\n");
                                break;
                        }
 
                }
        }  while (strcmp(buf,"connection terminated")! = 0);
        printf ("connection terminated.\n");
        close(sock);
        return 0;
}