00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <ctype.h>
00019 #include <string.h>
00020 #include <stdio.h>
00021 #include <unistd.h>
00022 #include <stdlib.h>
00023 #include <pthread.h>
00024 #include <limits.h>
00025 #include <iostream>
00026 using namespace std;
00027
00028 #include "RioInterfaceTypes.h"
00029 #include "RioInterface.h"
00030 #include "RioMMObject.h"
00031 #include "RioMMIntSimul.h"
00032 #include "mmintsimul.h"
00033
00034
00035 CRioMMIntSimul::CRioMMIntSimul( char *ModelName, unsigned int blocksize, RioBlock numBuffers )
00036 :CRioMMObject( ModelName, blocksize, numBuffers )
00037 {
00038 char *ext;
00039
00040 NextBlock = 0;
00041 BlockOffset = 0;
00042 pid = 0;
00043
00044
00045 strcpy( BaseName, ModelName );
00046 ext = strrchr( BaseName, '.' );
00047 if( strcmp( ext, ".simul" ) == 0 )
00048 *ext = 0;
00049 }
00050
00051
00052 CRioMMIntSimul::~CRioMMIntSimul()
00053 {
00054 }
00055
00056
00057 int CRioMMIntSimul::init( void )
00058 {
00059 return( 1 );
00060 }
00061
00062
00063 void CRioMMIntSimul::SyncMedia( void *param )
00064 {
00065 RioErr << "[IntSimul]: Entrei na SyncMedia(" << (RioBlock)param << ")"
00066 << NextBlock << endl;
00067 if( (RioBlock)param >= NextBlock )
00068 {
00069 SendPcktToTgif();
00070 GetNextPckt();
00071 }
00072 CRioMMObject::SyncMedia( param );
00073 }
00074
00075 void CRioMMIntSimul::Stop()
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 }
00086
00087 void CRioMMIntSimul::Pause()
00088 {
00089 #ifdef RIO_DEBUG2
00090 RioErr << "[IntSimul]: Entrei na Plause" << endl;
00091 #endif
00092 }
00093
00094 void CRioMMIntSimul::Play()
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 }
00117
00118
00119 void CRioMMIntSimul::StartTgif()
00120 {
00121 char path[ 200 ];
00122
00123
00124 if( pipe( pipe_tgif ) < 0 )
00125 {
00126 Rioperror("pipe(). Failed to create pipe to tgif.");
00127 return;
00128 }
00129
00130
00131 if( dup2( pipe_tgif[0], 0 ) == -1 )
00132 {
00133 Rioperror("dup2(). Failed to associate pipe to stdin.");
00134 return;
00135 }
00136
00137
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
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 }
00157
00158
00159 int CRioMMIntSimul::ConnectTgif()
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
00220 if( send( s, buffer, sizeof( T_Packet ), 0 ) < 0 )
00221 {
00222 Rioperror( "send" );
00223 return( -1 );
00224 }
00225
00226
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
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 }