163 lines
3.8 KiB
C++
163 lines
3.8 KiB
C++
//
|
|
// Stdio Daemon
|
|
// Neal Probert
|
|
//
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/timeb.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
|
|
#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 );
|
|
}
|