#include <RioMMIntSimul.h>
Inherits CRioMMObject.
Public Member Functions | |
CRioMMIntSimul (char *, unsigned int, RioBlock) | |
~CRioMMIntSimul (void) | |
int | init (void) |
void | Stop (void) |
void | Pause (void) |
void | Play (void) |
void | SyncMedia (void *) |
Private Member Functions | |
void | StartTgif (void) |
int | ConnectTgif (void) |
Private Attributes | |
char | BaseName [MAXPATHSIZE] |
int | pid |
int | pipe_tgif [2] |
unsigned int | BlockOffset |
RioBlock | NextBlock |
Definition at line 21 of file RioMMIntSimul.h.
CRioMMIntSimul::CRioMMIntSimul | ( | char * | ModelName, | |
unsigned int | blocksize, | |||
RioBlock | numBuffers | |||
) |
Definition at line 35 of file RioMMIntSimul.cpp.
00036 :CRioMMObject( ModelName, blocksize, numBuffers ) 00037 { 00038 char *ext; 00039 00040 NextBlock = 0; 00041 BlockOffset = 0; 00042 pid = 0; 00043 00044 /* Extract objname from simulation file name */ 00045 strcpy( BaseName, ModelName ); 00046 ext = strrchr( BaseName, '.' ); 00047 if( strcmp( ext, ".simul" ) == 0 ) 00048 *ext = 0; 00049 }
CRioMMIntSimul::~CRioMMIntSimul | ( | void | ) |
Definition at line 52 of file RioMMIntSimul.cpp.
int CRioMMIntSimul::ConnectTgif | ( | void | ) | [private] |
Definition at line 159 of file RioMMIntSimul.cpp.
00160 { 00161 struct hostent *phe; 00162 struct servent *pse; 00163 struct protoent *ppe; 00164 struct sockaddr_in sin; 00165 int s; 00166 int type; 00167 char buffer[sizeof(T_Packet)]; 00168 fd_set soc_pool; 00169 int max_desc; 00170 struct timeval timeout; 00171 char *host = "localhost"; 00172 char *protocol = "udp"; 00173 00174 bzero( (char *)&sin, sizeof( sin ) ); 00175 sin.sin_family = AF_INET; 00176 00177 if( (pse = getservbyname( service, protocol )) != NULL ) 00178 sin.sin_port = pse->s_port; 00179 else 00180 if( (sin.sin_port = htons( (u_short)atoi( service ) )) == 0 ) 00181 { 00182 RioErr << "Can't get \"" << service << "\" service entry" << endl; 00183 return( -1 ); 00184 } 00185 00186 if( (phe = gethostbyname( host )) != NULL ) 00187 bcopy(phe->h_addr,(char *)&sin.sin_addr, phe->h_length); 00188 else 00189 if( (sin.sin_addr.s_addr = inet_addr( host )) == INADDR_NONE ) 00190 { 00191 RioErr << "Can't get \"" << host << "\" host entry" << endl; 00192 return( -1 ); 00193 } 00194 00195 if( (ppe = getprotobyname( protocol )) == NULL ) 00196 { 00197 RioErr << "Can't get \"" << protocol << "\" protocol entry" << endl; 00198 return( -1 ); 00199 } 00200 00201 if( strcmp( protocol, "udp" ) == 0 ) 00202 type = SOCK_DGRAM; 00203 else 00204 type = SOCK_STREAM; 00205 00206 s = socket( PF_INET, type, ppe->p_proto ); 00207 if( s < 0 ) 00208 { 00209 RioErr << "Can't create socket!" << endl; 00210 return( -1 ); 00211 } 00212 00213 if( connect( s, (struct sockaddr *)&sin, sizeof( sin ) ) < 0 ) 00214 { 00215 RioErr << "Can't connect to " << host << "." << service << endl; 00216 return( -1 ); 00217 } 00218 00219 /* send a fake packet to the interface */ 00220 if( send( s, buffer, sizeof( T_Packet ), 0 ) < 0 ) 00221 { 00222 Rioperror( "send" ); 00223 return( -1 ); 00224 } 00225 00226 /* zero the pool and set the bits corresponding to the socket */ 00227 FD_ZERO( &soc_pool ); 00228 FD_SET( s, &soc_pool ); 00229 max_desc = s + 1; 00230 timeout.tv_sec = 3; 00231 timeout.tv_usec = 0; 00232 00233 if( select( max_desc, &soc_pool, NULL, NULL, &timeout ) < 0 ) 00234 { 00235 Rioperror( "select" ); 00236 return( -1 ); 00237 } 00238 00239 if( FD_ISSET(s, &soc_pool)) 00240 { 00241 /* receive fake packet from tgif */ 00242 if( recv( s, buffer, sizeof( T_Packet ), MSG_WAITALL ) < 0 ) 00243 { 00244 Rioperror( "recv" ); 00245 return( -1 ); 00246 } 00247 } 00248 else 00249 { 00250 RioErr << "Simulator Timeout expired!" << endl; 00251 return( -1 ); 00252 } 00253 00254 RioErr << "Simulator connected." << endl; 00255 return s; 00256 }
int CRioMMIntSimul::init | ( | void | ) | [virtual] |
Implements CRioMMObject.
Definition at line 57 of file RioMMIntSimul.cpp.
void CRioMMIntSimul::Pause | ( | void | ) | [virtual] |
Implements CRioMMObject.
Definition at line 87 of file RioMMIntSimul.cpp.
00088 { 00089 #ifdef RIO_DEBUG2 00090 RioErr << "[IntSimul]: Entrei na Plause" << endl; 00091 #endif 00092 }
void CRioMMIntSimul::Play | ( | void | ) | [virtual] |
Implements CRioMMObject.
Definition at line 94 of file RioMMIntSimul.cpp.
00095 { 00096 int soc; 00097 char msg[ 150 ]; 00098 T_Packet pack_in, pack_out; 00099 00100 #ifdef RIO_DEBUG2 00101 RioErr << "[IntSimul]: Entrei na Play" << endl; 00102 #endif 00103 00104 StartTgif(); 00105 sleep( 3 ); 00106 strcpy( msg, "call_one_arg_shortcut(Tangram2StartSimulator,\"0;0;/dev/null;0;ANY;0;0;I;0;OFF;0;0;\")" ); 00107 write( pipe_tgif[1], msg, strlen( msg ) ); 00108 soc = ConnectTgif(); 00109 while( 1 ) 00110 { 00111 recv_packet( soc, &pack_in ); 00112 unmount_packet( pack_in ); 00113 mount_packet( pack_in, &pack_out ); 00114 send_packet( soc, pack_out ); 00115 } 00116 }
void CRioMMIntSimul::StartTgif | ( | void | ) | [private] |
Definition at line 119 of file RioMMIntSimul.cpp.
00120 { 00121 char path[ 200 ]; 00122 00123 // create pipe to send data to tgif 00124 if( pipe( pipe_tgif ) < 0 ) 00125 { 00126 Rioperror("pipe(). Failed to create pipe to tgif."); 00127 return; 00128 } 00129 00130 // associate pipe input to stdin 00131 if( dup2( pipe_tgif[0], 0 ) == -1 ) 00132 { 00133 Rioperror("dup2(). Failed to associate pipe to stdin."); 00134 return; 00135 } 00136 00137 // start program tgif 00138 if( (pid = fork()) != 0 ) 00139 { 00140 if( pid < 0 ) 00141 { 00142 Rioperror("fork(). Failed to fork process."); 00143 return; 00144 } 00145 } 00146 else 00147 { 00148 //run tgif 00149 sprintf( path, "/tmp/%s", BaseName ); 00150 if( execlp( "tgif", "tgif", path, "-exec", "-", NULL ) == -1 ) 00151 { 00152 Rioperror( "execlp(): Failed to execute tgif." ); 00153 exit( 1 ); 00154 } 00155 } 00156 }
void CRioMMIntSimul::Stop | ( | void | ) | [virtual] |
Implements CRioMMObject.
Definition at line 75 of file RioMMIntSimul.cpp.
00076 { 00077 #ifdef RIO_DEBUG2 00078 RioErr << "[IntSimul]: Entrei na Stop" << endl; 00079 #endif 00080 00081 NextBlock = 0; 00082 BlockOffset = 0; 00083 00084 CRioMMObject::Stop(); 00085 }
void CRioMMIntSimul::SyncMedia | ( | void * | param | ) | [virtual] |
Reimplemented from CRioMMObject.
Definition at line 63 of file RioMMIntSimul.cpp.
char CRioMMIntSimul::BaseName[MAXPATHSIZE] [private] |
Definition at line 24 of file RioMMIntSimul.h.
unsigned int CRioMMIntSimul::BlockOffset [private] |
Definition at line 27 of file RioMMIntSimul.h.
RioBlock CRioMMIntSimul::NextBlock [private] |
Definition at line 28 of file RioMMIntSimul.h.
int CRioMMIntSimul::pid [private] |
Definition at line 25 of file RioMMIntSimul.h.
int CRioMMIntSimul::pipe_tgif[2] [private] |
Definition at line 26 of file RioMMIntSimul.h.