]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/ORTEAppSendThread.c
JORTE: ignore 'int-to-pointer' and 'pointer-to-int' compiler warnings
[orte.git] / orte / liborte / ORTEAppSendThread.c
1 /*
2  *  $Id: ORTEAppSendThread.c,v 0.0.0.1  2003/08/21 
3  *
4  *  DEBUG:  section 24                  Sending thread
5  *
6  *  -------------------------------------------------------------------  
7  *                                ORTE                                 
8  *                      Open Real-Time Ethernet                       
9  *                                                                    
10  *                      Copyright (C) 2001-2006                       
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic  
12  *                      http://dce.felk.cvut.cz                       
13  *                      http://www.ocera.org                          
14  *                                                                    
15  *  Author:              Petr Smolik    petr.smolik@wo.cz             
16  *  Advisor:             Pavel Pisa                                   
17  *  Project Responsible: Zdenek Hanzalek                              
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *  
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *  
30  */ 
31
32 #include "orte_all.h"
33
34 /*****************************************************************************/
35 void ORTESendData(ORTEDomain *d,ObjectEntryAID *objectEntryAID,Boolean meta) {
36   struct sockaddr_in  des; 
37   ObjectEntryOID      *object;
38   AppParams           *appParams;
39   int                 i;
40   CDR_Codec           *cdrCodec=&d->taskSend.mb.cdrCodec;
41   Port                port;
42
43   object=objectEntryAID->aobject;
44   if (object) {
45     appParams=(AppParams*)object->attributes;
46     if (!ObjectEntryMulticast_is_empty(object)) {
47       for(i=0;i<appParams->metatrafficMulticastIPAddressCount;i++) {
48         des.sin_family=AF_INET; 
49         des.sin_addr.s_addr = htonl(appParams->metatrafficMulticastIPAddressList[i]);
50         des.sin_port = htons((uint16_t)object->multicastPort); 
51         if (d->taskSend.mb.cdrCodecDirect) {
52           sock_sendto (&d->taskSend.sock,
53                        d->taskSend.mb.cdrCodecDirect->buffer,
54                        d->taskSend.mb.cdrCodecDirect->wptr,
55                        &des,
56                        sizeof(des)); 
57         } else {
58           sock_sendto (&d->taskSend.sock,
59                        cdrCodec->buffer,
60                        cdrCodec->wptr,
61                        &des,
62                        sizeof(des)); 
63         }
64       }
65     } else {
66       for(i=0;i<appParams->unicastIPAddressCount;i++) {
67         des.sin_family=AF_INET; 
68         des.sin_addr.s_addr = htonl(appParams->unicastIPAddressList[i]);
69         if (meta) {
70           port=appParams->metatrafficUnicastPort;
71         } else {
72           port=appParams->userdataUnicastPort; 
73         }
74         des.sin_port = htons((uint16_t)port); 
75         if (d->taskSend.mb.cdrCodecDirect) {
76           sock_sendto (&d->taskSend.sock,
77                        d->taskSend.mb.cdrCodecDirect->buffer,
78                        d->taskSend.mb.cdrCodecDirect->wptr,
79                        &des,
80                        sizeof(des)); 
81         } else {
82           sock_sendto (&d->taskSend.sock,
83                        cdrCodec->buffer,
84                        cdrCodec->wptr,
85                        &des,
86                        sizeof(des)); 
87         }
88       }
89     }
90   } else {
91     debug(24,1) ("ORTEAppSendThread: no aobjectEntryOID connected to objectEntryAID!\n");
92   }
93   //prepare buffer for next sending
94   cdrCodec->wptr=RTPS_HEADER_LENGTH;
95   d->taskSend.mb.needSend=ORTE_FALSE;
96   d->taskSend.mb.containsInfoReply=ORTE_FALSE;
97   d->taskSend.mb.cdrCodecDirect=NULL;
98 }
99
100 /*****************************************************************************/
101 void ORTEAppSendThread(TaskProp *tp) {
102   struct timespec     wtime; 
103   NtpTime             actTime,nextExpire,whenExpire,sleepingTime;
104   int32_t             s,ms;
105   ORTEDomain          *d=tp->d;
106
107   debug(24,10) ("ORTEAppSendThread: start\n");
108   d->objectEntry.htimNeedWakeUp=ORTE_TRUE;
109
110   while (!tp->terminate) {
111     actTime=getActualNtpTime();
112
113     pthread_rwlock_wrlock(&d->objectEntry.htimRootLock);
114     if (htimerRoot_next_expire(&d->objectEntry,&nextExpire)==0) {
115       NTPTIME_BUILD(whenExpire,300); //max time for sleeping (no. events)
116       NtpTimeAdd(nextExpire,actTime,whenExpire);
117     }
118     pthread_rwlock_unlock(&d->objectEntry.htimRootLock);
119     NtpTimeDisAssembToUs(wtime.tv_sec,wtime.tv_nsec,nextExpire);
120     wtime.tv_nsec*=1000;  //conver to nano seconds
121     NtpTimeSub(sleepingTime,nextExpire,actTime);
122     NtpTimeDisAssembToMs(s,ms,sleepingTime);
123     if (s<0) s=ms=0;
124
125     debug(24,4) ("ORTEAppSendThread: sleeping for %lis %lims\n",s,ms);
126     if (!((wtime.tv_sec==0) && (wtime.tv_nsec==0))) {
127       pthread_mutex_lock(&d->objectEntry.htimSendMutex);
128       if (d->objectEntry.htimSendCondValue==0) {
129         pthread_cond_timedwait(&d->objectEntry.htimSendCond,
130                                &d->objectEntry.htimSendMutex,
131                                &wtime);
132       }
133       d->objectEntry.htimSendCondValue=0;
134       pthread_mutex_unlock(&d->objectEntry.htimSendMutex);
135     }
136
137     debug(24,7) ("ORTEAppSendThread: fired\n");
138     actTime=getActualNtpTime();
139     pthread_rwlock_wrlock(&d->objectEntry.objRootLock);
140     pthread_rwlock_wrlock(&d->objectEntry.htimRootLock);
141     d->objectEntry.htimNeedWakeUp=ORTE_FALSE;
142
143     htimerRoot_run_expired(d,&actTime);
144
145     d->objectEntry.htimNeedWakeUp=ORTE_TRUE;
146     pthread_rwlock_unlock(&d->objectEntry.htimRootLock);
147     pthread_rwlock_unlock(&d->objectEntry.objRootLock);
148   }
149   debug(24,10) ("ORTEAppSendThread: finished\n");
150   pthread_exit(NULL);
151 }
152