Exemple #1
0
static void echo_query(NODE *n)
{
   switch(n -> kind){
      case N_CREATETABLE:            /* for CreateTable() */
         printf("create table %s (", n -> u.CREATETABLE.relname);
         print_attrtypes(n -> u.CREATETABLE.attrlist);
         printf(")");
         printf(";\n");
         break;
      case N_CREATEINDEX:            /* for CreateIndex() */
         printf("create index %s(%s);\n", n -> u.CREATEINDEX.relname,
               n -> u.CREATEINDEX.attrname);
         break;
      case N_DROPINDEX:            /* for DropIndex() */
         printf("drop index %s(%s);\n", n -> u.DROPINDEX.relname,
               n -> u.DROPINDEX.attrname);
         break;
      case N_DROPTABLE:            /* for DropTable() */
         printf("drop table %s;\n", n -> u.DROPTABLE.relname);
         break;
      case N_LOAD:            /* for Load() */
         printf("load %s(\"%s\");\n",
               n -> u.LOAD.relname, n -> u.LOAD.filename);
         break;
      case N_HELP:            /* for Help() */
         printf("help");
         if(n -> u.HELP.relname != NULL)
            printf(" %s", n -> u.HELP.relname);
         printf(";\n");
         break;
      case N_PRINT:            /* for Print() */
         printf("print %s;\n", n -> u.PRINT.relname);
         break;
      case N_SET:                                 /* for Set() */
         printf("set %s = \"%s\";\n", n->u.SET.paramName, n->u.SET.string);
         break;
      case N_QUERY:            /* for Query() */
         printf("select ");
         print_relattrs(n -> u.QUERY.relattrlist);
         printf("\n from ");
         print_relations(n -> u.QUERY.rellist);
         printf("\n");
         if (n->u.QUERY.conditionlist) {
            printf("where ");
            print_conditions(n->u.QUERY.conditionlist);
         }
         printf(";\n");
         break;
      case N_INSERT:            /* for Insert() */
         printf("insert into %s values ( ",n->u.INSERT.relname);
         print_values(n -> u.INSERT.valuelist);
         printf(");\n");
         break;
      case N_DELETE:            /* for Delete() */
         printf("delete %s ",n->u.DELETE.relname);
         if (n->u.DELETE.conditionlist) {
            printf("where ");
            print_conditions(n->u.DELETE.conditionlist);
         }
         printf(";\n");
         break;
      case N_UPDATE:            /* for Update() */
         {
            printf("update %s set ",n->u.UPDATE.relname);
            print_relattr(n->u.UPDATE.relattr);
            printf(" = ");
            struct node *rhs = n->u.UPDATE.relorvalue;

            /* The RHS can be either a relation.attribute or a value */
            if (rhs->u.RELATTR_OR_VALUE.relattr) {
               /* Print out the relation.attribute */
               print_relattr(rhs->u.RELATTR_OR_VALUE.relattr);
            } else {
               /* Print out the value */
               print_value(rhs->u.RELATTR_OR_VALUE.value);
            }
            if (n->u.UPDATE.conditionlist) {
               printf("where ");
               print_conditions(n->u.UPDATE.conditionlist);
            }
            printf(";\n");
            break;
         }
      default:   // should never get here
         break;
   }
   fflush(stdout);
}
int main(int argc, char**argv)
{
	fprintf(stderr, "Flint:Entangled Sparks: Frame Contoller for Entagngled Sparks Version %1.2f\n", VESRION);
	fprintf(stderr, "Copyright (C)2014 Josh Gardiner [email protected]\n");
	fprintf(stderr, "\nThis program comes with ABSOLUTELY NO WARRANTY; for details type -w'\n");
	fprintf(stderr, "This is free software, and you are welcome to redistribute it\n");
	fprintf(stderr, "under certain conditions; type -c for details.\n");
	fprintf(stderr, "\ntype -h for help\n\n");

    // Create Packet types
    espDataPacket sendpacket; // We Send espData Packets
    espAckPacket recvpacket;  // We Recive espAck Packets

    // Setup Options
    int cmd_option;
    int port = 1535;
    char *server = "127.0.0.1";
    // Parse cmd line options
    while((cmd_option=getopt(argc, argv, "hp:s:wc")) != EOF)
    switch(cmd_option)
    {
        default:
        case 'h': print_flint_help();
        case 'p': port=atoi(optarg); break;
        case 's': server=(optarg); break;
        case 'w': print_warranty();
        case 'c': print_conditions();
    }


    struct sockaddr_in myaddr;
    struct sockaddr_in remaddr;

    int socket1;
    int slen=sizeof(remaddr);

    int recvlen;

    if ((socket1=socket(AF_INET, SOCK_DGRAM, 0))==-1)
        printf("socket created\n");

    memset((char *)&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(0);

    int optval=1;
    setsockopt(socket1,SOL_SOCKET,SO_BROADCAST,&optval,sizeof optval); // enables bcast

    // Not Required if using polling
    //int nonBlocking = 1;
    //if ( fcntl( socket1, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 )
    //{
    //  printf( "failed to set non-blocking\n" );
    //  return -1;
    //}


    if (bind(socket1, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind failed");
        return 0;
    }  
    
    memset((char *) &remaddr, 0, sizeof(remaddr));
    remaddr.sin_family = AF_INET;
    remaddr.sin_port = htons(port);
    if (inet_aton(server, &remaddr.sin_addr)==0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }


    struct sched_param schedparm;
    memset(&schedparm, 0, sizeof(schedparm));
    schedparm.sched_priority = 1; // lowest rt priority
    sched_setscheduler(0, SCHED_FIFO, &schedparm);

    struct itimerspec timspec;
    bzero(&timspec, sizeof(timspec));
    timspec.it_interval.tv_sec = 0;
    timspec.it_interval.tv_nsec = PACKET_INTERVAL;
    //timspec.it_value.tv_sec = 0;
    timspec.it_value.tv_nsec =1;

    int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
    timerfd_settime(timerfd, 0, &timspec, 0);
    //timerfd_settime()
    //fprintf(stderr,"timer1=%d\n",timer1);

    struct pollfd ufds[2];
    ufds[0].fd = timerfd;
    ufds[0].events = POLLIN; //| POLLPRI; // check for normal or out-of-band

    ufds[1].fd = socket1;
    ufds[1].events = POLLIN; //| POLLPRI; // check for normal or out-of-band

    int rv;
    uint64_t loopcount=0;
    uint32_t frameid=0;
    //uint32_t acks=0;

    Clock flintClock;
    Clock flintOffsetClock;
    flintOffsetClock=initClock();
    flintClock=getProgramClock(flintOffsetClock);


    uint32_t testdata=0XABCDEF01;

    for(;;)
    {
        rv = poll(ufds, 2, 1);
        //testloopcount++;
        if (rv == -1) 
        {
                perror("poll"); // error occurred in poll()
        } else if (rv == 0) 
        {
        // DO Nothing
        
        } else 
        {
            if (ufds[0].revents & POLLIN) // Timer
            {
                // Used to Send Packets at Regular Intervals
                read(timerfd, &loopcount, sizeof(uint64_t)); //reset Timer

                // Ensure to re write every field of espDataPacket or
                // do sendpacket=data_ntoh(sendpacket) first.
                flintClock=getProgramClock(flintOffsetClock);

                sendpacket.prot_header   = 0;
                sendpacket.frameid       = frameid;
                sendpacket.cmd           = 20;
                sendpacket.data          = testdata;
                sendpacket.ptime_sec     = flintClock.seconds;
                sendpacket.ptime_usec    = flintClock.useconds;
                sendpacket.clockadj_usec = 0;

                //acks=0;

                printf("SENT Packet FrameID=  %d\n",sendpacket.frameid);

                sendpacket=data_hton(sendpacket);
                if (sendto(socket1, &sendpacket, sizeof(espDataPacket), 0, (struct sockaddr *)&remaddr, slen)==-1) 
                {
                    perror("sendto");
                    exit(1);
                }
                frameid++;
                testdata++;

                }
            if (ufds[1].revents & POLLIN) // Recive Socket
            {
                recvlen = recvfrom(socket1, &recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&remaddr, (socklen_t *)&slen);
                if (recvlen > 0) 
                {
                    recvpacket=ack_ntoh(recvpacket);
                    Print_espAckPacket(recvpacket);
                }
            }
        }

    }
	return 0;
}