parse_command.cc
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:6k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #include <stdio.h>
- #include <errno.h>
- #if HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "sockaddr.h"
- #include "vpusher.h"
- #include "../../client/src/play_commands.h"
- //#define DEBUG
- void Appl::parse_command( const char *cmd, const Sockaddr &addr )
- {
- const static int maxCommandLength = 100;
- istrstream input( cmd );
- string command;
- command.resize( maxCommandLength );
- // first line contains only the command
- input.getline( ( char *)command.data(), maxCommandLength );
- trim_string( command );
-
- // find the handler
- commandsMapT::iterator it = commandsMap.find( command );
- if( it == commandsMap.end() ) // sorry
- {
- log( 1, "Unknown command: %s", cmd );
- # ifdef DEBUG
- log( 1, "known commands:" );
- for( it = commandsMap.begin(); it != commandsMap.end(); it++ )
- log( 3, "%s", (*it).first.c_str() );
- # endif
- return;
- }
- log( 3, "command: %s", cmd );
-
- // i'm sorry, guys, it's c++. Sometimes it looks very strange :-)
- (this->*(*it).second)( input, addr );
- }
- void Appl::play_impl( istrstream &input, const Sockaddr &addr )
- {
- cout << "play" << endl;
-
- if( users.find( addr ) != users.end() ) { // old user, delete old job
- RemoteUser &user = *users[ addr ];
- user.cleanup_jobs( inusedBuffers );
- }
- else {
- users[ addr ] = new RemoteUser( addr ); // new
- cout << "new user, addr " << addr << endl;
- }
- RemoteUser &user = *users[ addr ];
- assert( user.jobs.empty() );
- // read remote port to push data to
- int remoteportnum;
- string s;
- s.resize( 100 );
- input.getline( (char *) s.c_str(), 100 );
- sscanf( s.c_str(), "%d", &remoteportnum );
- // read time from where to play
- s.resize( 100 );
- input.getline( (char *) s.c_str(), 100 );
- cout << "time to start" << s.c_str() << endl;
- struct tm firstt;
- STRTOTIME( s.c_str(), firstt );
- bool firstFile = true;
- while( input )
- {
- // add new job
- JobElem newJob( user, input, firstFile, firstt );
- firstFile = false;
- if( !newJob.valid() ) // error
- break;
- user.jobs.push( newJob );
- }
- // reset this user sequence number
- user.sequence = 0;
- // create output socket and
- user.outputSocket = socket( PF_INET, SOCK_DGRAM, 0 );
- if( -1 == user.outputSocket )
- {
- perror( "socket " );
- return;
- }
- // connect to port number, that we got as argument
- struct sockaddr_in saddr;
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons( remoteportnum );
- saddr.sin_addr = user.socketAddr.addr.sin_addr;
- if( -1 == connect( user.outputSocket,
- (struct sockaddr *) &saddr, sizeof( saddr )))
- {
- perror( "connect " );
- return;
- }
- // first load event
- Event le( Event::load );
- le.userSocketAddr = user.socketAddr;
- le.when.now();
- eventQueue.push( le );
- // and just after first send event
- Event se( Event::send );
- se.userSocketAddr = user.socketAddr;
- se.when = le.when;
- se.when.tv_usec() += 100;
- eventQueue.push( se );
- log( 1, "play to user identified as %s, destination port %d, job size %d",
- user.socketAddr.get_description().c_str(), remoteportnum,
- user.jobs.size() );
- }
- void Appl::stop_impl( istrstream &input, const Sockaddr &addr )
- {
- if( users.find( addr ) == users.end() )
- {
- // nothing to do
- return;
- }
- RemoteUser &user = *users[ addr ];
- user.cleanup_jobs( inusedBuffers );
- }
- void Appl::setfilename_impl( istrstream &input, const Sockaddr &addr )
- {
- // the filename
- videoFileName.resize( MAX_PATH );
- input.getline( (char *) videoFileName.c_str(), MAX_PATH );
- videoFileName.resize( strlen( videoFileName.c_str() ) + 1 );
- // open the file
- if( videoFileHandle != -1 )
- close( videoFileHandle );
- log( 1, "Open new filename %s", videoFileName.c_str() );
- videoFileHandle = open( videoFileName.c_str(),
- O_WRONLY | O_CREAT | O_TRUNC, 0660 );
- if( videoFileHandle == -1 )
- {
- // first reason: path do not exists
- int pos = 0;
- while( true )
- {
- // from the next character
- pos = videoFileName.find( '/', pos + 1 );
- if( pos == -1 )
- break;
- string path = videoFileName.substr( 0, pos );
- path.reserve( path.length() + 1 );
- // vvv is that really needed? vvv
- *(char*)( path.c_str() + path.length() ) = ' ';
- if( -1 == mkdir( path.c_str(), 0770 ))
- {
- // if mkdir fails that's fine, directory may exists
- log( 2, "can't create dir %s, %s", path.c_str(),
- sys_errlist[ errno ] );
- }
- }
- // the try to open again...
- videoFileHandle =
- open( videoFileName.c_str(),O_WRONLY | O_CREAT | O_TRUNC, 0660 );
- }
- if( videoFileHandle == -1 )
- {
- log( 0, "can't open new file, %s", sys_errlist[ errno ] );
- perror( "open" );
- videoFileHandle = -1;
- return;
- }
- }
- void Appl::stoprecording_impl( istrstream &cmd, const Sockaddr &addr )
- {
- if( videoFileHandle != -1 )
- close( videoFileHandle );
- videoFileHandle = -1;
- videoFileName = "";
- }
- void Appl::accept_stream_impl( istrstream &input, const Sockaddr &addr )
- {
- // no parameters - push server should know the port number from environment
- bindAcceptWaitSocket();
- }
- void Appl::add_broadcast_destination_impl
- ( istrstream &input, const Sockaddr &addr )
- {
- // get the IP address or host name
- string s_addr;
- s_addr.resize( 100 );
- input.getline( (char *) s_addr.c_str(), 100 );
- trim_string( s_addr );
- // and the port number
- string s_port;
- s_port.resize( 10 );
- input.getline( (char *) s_port.c_str(), 10 );
- trim_string( s_port );
- int portnum = 0;
- sscanf( s_port.c_str(), "%d", &portnum );
- if( !portnum )
- return;
- Sockaddr broadcast_dest_addr( s_addr, portnum );
- // add broadcast to user: this is special user, which broadcasts
- if( broadcast_user == NULL )
- {
- log( 0, "Strong error: broadcast_user is zero" );
- return;
- }
- broadcast_user->add_broadcast_channel( broadcast_dest_addr );
- }
- void Appl::trim_string( string &s )
- {
- char ch;
- int pos;
- s.c_str();
- for( pos = 0, ch = s[0]; ch == ' ' || ch == 't'; pos ++ )
- ch = s[pos];
- if( pos > 0 )
- {
- s = s.substr( pos );
- }
- for( pos = s.length() - 1, ch = s[pos]; ch == ' ' || ch == 't'; pos -- )
- ch = s[pos];
- s.resize( strlen( s.c_str() ));
- if( pos < s.length() - 1 )
- {
- s = s.substr( 0, pos );
- }
- s.c_str();
- }