Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.system > #596

how to integrate the both functions?

Newsgroups comp.os.linux.development.system
Date 2014-03-07 00:36 -0800
Message-ID <45f87d0d-05bb-498f-80e3-56768c169596@googlegroups.com> (permalink)
Subject how to integrate the both functions?
From Hemanth Venkatappa <hemanthvenkatappa@gmail.com>

Show all headers | View raw


void TASK3(Task100ms_Raster)
{
     struct timespec start, stop;



      if((startTime32 = clock_gettime( CLOCK_REALTIME, &start)) == -1 ) {
                  perror( "clock gettime" );

                }
            startTime32 =start.tv_sec + 0.0000001 * start.tv_nsec;



            XCP_FN_TYPE Xcp_CmdProcessor();




            if((stopTime32 = clock_gettime( CLOCK_REALTIME, &stop)) == -1 ) {
                      perror( "clock gettime" );

                    }

    stopTime32 =  stop.tv_sec + 0.0000001 * stop.tv_nsec;

    duration100ms = ( stop.tv_sec - start.tv_sec )
                     + (double)( stop.tv_nsec - start.tv_nsec )
                       / (double)BILLION;

}


The handler checks that the value stored in sival_ptr matches a given timerID
variable.  The sival_ptr is the same as the one we set in makeTimer(),
though here it lives in a different structure.
Obviously, it got copied from there to here on the way to this signal handler.
The point is that the timerID is what is used to determine which timer just went off
and determine what to do next


static void timerHandler( int sig, siginfo_t *si, void *uc )
{
    timer_t *tidp;

    tidp = si->si_value.sival_ptr;

    if ( *tidp == firstTimerID )

        TASK1(Task2ms_Raster);
   else if ( *tidp == secondTimerID )
       TASK2(Task10ms_Raster);
    else if ( *tidp == thirdTimerID )
        TASK3(Task100ms_Raster);
}



//The function takes a pointer to a timer_t variable that will be filled with the
//timer ID created by timer_create().  This pointer is also saved in the sival_ptr
//variable right before calling timer_create().  In this function notice that we
//always use the SIGRTMIN signal, so expiration of any timer causes this signal to
//be raised.  The signal handler I've written for that signal is timerHandler.



 static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
     //sigset_t mask;
    struct sigevent         te;
    struct itimerspec       its;
    struct sigaction        sa;
    int                     sigNo = SIGRTMIN;


     //Set up signal handler.
    memset(&sa, 0, sizeof(sa));
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = timerHandler;
    sigemptyset(&sa.sa_mask);
    if (sigaction(sigNo, &sa, NULL) == -1)
    {
        perror("sigaction");
    }

     //Set and enable alarm
    te.sigev_notify = SIGEV_SIGNAL;
    te.sigev_signo = sigNo;
    te.sigev_value.sival_ptr = timerID;
    timer_create(CLOCK_REALTIME, &te, timerID);

    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = intervalMS * 1000000;
    its.it_value.tv_sec = 0;
    its.it_value.tv_nsec = expireMS * 1000000;
    timer_settime(*timerID, 0, &its, NULL);


    return 1;

 }



int CreateSocket()
{


    socklen_t len = sizeof(client);
       // Socket creation for UDP

       acceptSocket=socket(AF_INET,SOCK_DGRAM,0);

       if(acceptSocket==-1)

       {

         printf("Failure: socket creation is failed, failure code\n");

         return 1;

       }

       else

       {

         printf("Socket started!\n");

       }



          //Bind the socket
     memset(&addr, 0, sizeof(addr));

     addr.sin_family=AF_INET;

     addr.sin_port=htons(port);

     addr.sin_addr.s_addr=htonl(INADDR_ANY);

     rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));

     if(rc== -1)

     {

       printf("Failure: listen, failure code:\n");

       return 1;

     }

     else

     {

       printf("Socket an port %d \n",port);

     }


     if(acceptSocket == -1)
     {
         printf("Fehler: accept, fehler code:\n");

          return 1;
     }
     else
     {

     while(rc!=-1)
         {


         rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);
         if(rc==0)
         {
           printf("Server has no connection..\n");
           break;
         }
         if(rc==-1)
         {
             printf("something went wrong with data %s", strerror(errno));
           break;
         }


         XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );


          }


     }

       close(acceptSocket);



       return 0;

     }






int main()
{

     Xcp_Initialize();
         CreateSocket();

// I am confused w.r.t this
                 makeTimer("First Timer", &firstTimerID, 2, 2);   // creating the timer for 2ms
                 makeTimer("Second Timer", &secondTimerID, 10, 10);    //creating the timer for 10ms
                 makeTimer("Third Timer", &thirdTimerID, 100, 100);  //creating the timer for 100ms



     return 0;
}

There is a client and server in my project - Client is a tool (INCA) for sending the data to the server via the ip address and port number (udp communication). Server is recieving the data from the client and I created a timer which is running on server side and calling some task for every 2ms, 10ms and 100ms. I have to call the specific API at 100ms (this is some protocol API) to process the data from the client for every 100ms (it has to process the data from the client for every 100ms).

Current situation : I programmed a server side program for recieving the data from the client via the ip address and port number and also created the timer.

My question : how to integrate the socket which is recieving the data from the client socket(); with the timer which i have created timer_create(); ??

Back to comp.os.linux.development.system | Previous | Next | Find similar


Thread

how to integrate the both functions? Hemanth Venkatappa <hemanthvenkatappa@gmail.com> - 2014-03-07 00:36 -0800

csiph-web