// // Stdio Daemon // Neal Probert // #include #include #include #include #include #include #include #include #include #include #include #include "UriParse.h" #include "StdioDaemon.h" #include "netlib.h" //**************************************************************************** // defines //***************************************************************************/ //**************************************************************************** // macros //***************************************************************************/ //**************************************************************************** // structs & typedefs //***************************************************************************/ //**************************************************************************** // global constants //***************************************************************************/ //**************************************************************************** // global variables //***************************************************************************/ //**************************************************************************** // static constants //***************************************************************************/ //**************************************************************************** // static variables //***************************************************************************/ //**************************************************************************** // static functions //***************************************************************************/ //**************************************************************************** // C++ functions //***************************************************************************/ StdioDaemon::StdioDaemon() { // logging log_open( STDIOD_NAME ); m_RemoteIn = m_DgramIn = -1; m_Stdin = 0; AddFd( m_Stdin ); } StdioDaemon::~StdioDaemon() { } /* TCP ***********************************************************************/ int StdioDaemon::ServerSocket(void) { char buf[2048]; int n = 0; if ( IsSet( m_RemoteIn ) ) { // from server (tbd) int n = Client.Recv( buf, sizeof(buf) ); if ( n > 0 ) fputs( buf, stdout ); } else if ( IsSet( m_Stdin ) ) { // packet from GPS device while ( fgets( buf, sizeof(buf), stdin ) ) { int n = strlen( buf ); if ( n>0 ) ClientCast( buf, n ); } } else if ( IsSet( m_DgramIn ) ) { // RTCM packet from UDP broadcast (once a minute) int n = Dgram.RecvFrom( buf, sizeof(buf) ); // send to GPS device if ( n > 0 ) fputs( buf, stdout ); } return n; } int StdioDaemon::ClientIn( int sock, const char *buf, int n ) { return fputs( buf, stdout ); } void StdioDaemon::ClientCast( const char *buf, int n ) { // broadcast it Bcast.SendTo( buf, n ); // send to clients TcpServer::ClientCast( buf, n ); } /* GPS ***********************************************************************/ int StdioDaemon::TcpConnect( const char *uri ) { UriParse parsed; parsed.setUri( uri ); const char *proto = parsed.getProto(); if ( strcasecmp( proto, "http" ) == 0 || strcasecmp( proto, "https" ) == 0 ) { // web site } else if ( strcasecmp( proto, "telnet" ) == 0 || strlen( proto ) == 0 ) { // straight TCP client m_RemoteIn = Client.Connect( parsed.getHost(), parsed.getService() ); } return m_RemoteIn; } int StdioDaemon::UdpListen( const char *uri ) { m_DgramIn = Dgram.ListenUri( uri ); if ( m_DgramIn > 0 ) AddFd( m_DgramIn ); return m_DgramIn; } int StdioDaemon::UdpBroadcast( const char *uri ) { return Bcast.CasterUri( uri ); }