58 signal(RTSIGIGN, SIG_IGN);
61 dataFileOpenTime = 0.;
63 data_save_path =
"data/syslogs";
64 data_log_time_length = 120.;
85 logss <<
"Setting effective user id to euid_called (" <<
euid_called <<
") failed. Errno says: " << strerror(errno) <<
".";
99 logss <<
"Setting effective user id to euid_real (" <<
euid_real <<
") failed. Errno says: " << strerror(errno) <<
".";
111 struct sched_param schedpar;
113 schedpar.sched_priority = prio;
120 else rv = sched_setscheduler(0, SCHED_OTHER, &schedpar);
125 logss <<
"Setting scheduler priority to " << prio <<
" failed. Errno says: " << strerror(errno) <<
". ";
156 error_report(Logger::LOG_LEV_ERROR,
"fifo_list not yet setup.");
263 std::cout <<
"Connecting fifo list\n";
274 struct sigaction act;
284 if(sigaction(SIGIO, &act, 0) < 0)
287 logss <<
"Error setting signal handler for SIGIO. Errno says: " << strerror(errno) <<
".";
292 log_msg(Logger::LOG_LEV_INFO,
"Installed signal handling for SIGIO");
299 struct sigaction act;
305 act.sa_flags = SA_SIGINFO;
311 if(sigaction(RTSIGIO, &act, 0) < 0)
314 logss <<
"Error setting signal handler for RTSIGIO. Errno says: " << strerror(errno) <<
".";
319 log_msg(Logger::LOG_LEV_INFO,
"Installed signal handling for RTSIGIO");
325 if(sigaction(SIGIO, &act, 0) < 0)
328 logss <<
"Error setting backup signal handler for SIGIO. Errno says: " << strerror(errno) <<
".";
333 log_msg(Logger::LOG_LEV_INFO,
"Installed backup signal handling for SIGIO");
335 signal(RTSIGIGN, SIG_IGN);
336 log_msg(Logger::LOG_LEV_INFO,
"Ignoring RTSIGIGN");
349 error_report(Logger::LOG_LEV_ERROR,
"R/T SIGIO overflow.");
399 logss <<
"Error returned from ::write_fifo_channel on channel " << ch <<
".";
420 if(
fl.
fifo_ch[ch].last_atime_out == fst.st_atime)
436 logss <<
"Error returned from ::write_fifo_channel on channel " << ch <<
".";
479 nottimedout = poll(&pfd, 1, (
int) (.1*
wait_to*1000.));
483 nottimedout = poll(&pfd, 1, (
int) (
wait_to*1000.));
488 logss <<
"Poll returned error (not a timeout). errno says: " << strerror(errno) <<
"\n";
498 if(
fl.
fifo_ch[ch].server_response[0] ==
'\0' || !nottimedout)
504 logss <<
"FIFO response timeout. wait_to = " <<
wait_to <<
"secs.";
509 fl.
fifo_ch[ch].last_atime_out = fst.st_atime;
520 logss <<
"FIFO response timeout clear.";
537 error_report(Logger::LOG_LEV_ERROR,
"Attempt to set wait_to to negative number.");
542 logss <<
"Wait timeout (wait_to) set to " <<
wait_to <<
" secs.";
546 if(to < 0)
return -1;
567 if(com ==
"")
return "";
575 std::string response;
582 if(
request_control(CMODE_REMOTE, 1) == CMODE_REMOTE)
return "REMOTE\n";
583 else return "FAIL\n";
585 else return "UNKOWN COMMAND:" + com +
"\n";
589 if(com ==
"REMOTE" || com ==
"XREMOTE")
594 else return "FAIL\n";
609 else return "FAIL\n";
614 else return "FAIL\n";
624 return "UNKOWN COMMAND: " + com +
"\n";
636 if(com ==
"")
return "";
644 int pos = com.find_first_of(
"\n\r", 0);
645 if(pos > -1) com.erase(pos, com.length()-pos);
647 std::string response;
655 else return "FAIL\n";
657 else return "UNKOWN COMMAND:" + com +
"\n";
661 if(com ==
"LOCAL" || com ==
"XLOCAL")
666 else return "FAIL\n";
680 else return "FAIL\n";
685 else return "FAIL\n";
694 return "UNKOWN COMMAND: " + com +
"\n";
706 if(com ==
"")
return "";
714 int pos = com.find_first_of(
"\n\r", 0);
715 if(pos > -1) com.erase(pos, com.length()-pos);
717 std::string response;
724 if(
request_control(CMODE_SCRIPT, 1) == CMODE_SCRIPT)
return "SCRIPT\n";
725 else return "FAIL\n";
727 else return "UNKOWN COMMAND:" + com +
"\n";
731 if(com ==
"SCRIPT" || com ==
"XSCRIPT")
736 else return "FAIL\n";
750 else return "FAIL\n";
755 else return "FAIL\n";
764 return "UNKOWN COMMAND: " + com +
"\n";
782 if(com ==
"")
return "";
784 int pos = com.find_first_of(
"\n\r", 0);
785 if(pos > -1) com.erase(pos, com.length()-pos);
789 if(com ==
"AUTO" || com ==
"XAUTO")
794 else return "FAIL\n";
810 else return "FAIL\n";
815 else return "FAIL\n";
820 return "UNKOWN COMMAND: " + com +
"\n";
866 std::cout <<
get_app_name() <<
": " << lmsg <<
" (log level " << LogLevel <<
")\n";
870 int VisAOApp_base::start_profiler()
872 struct sched_param schedpar;
873 schedpar.sched_priority = 0;
877 pthread_attr_init(&attr);
878 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
880 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
881 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
882 pthread_attr_setschedparam(&attr, &schedpar);
884 return pthread_create(&th, &attr, &__start_profiler, (
void *) profile);
919 clock_gettime(CLOCK_REALTIME, &bsb->last_update);
925 std::string VisAOApp_base::getDataFileName()
931 gettimeofday(&tv, 0);
934 fname = getenv(
"VISAO_ROOT");
936 fname += data_save_path;
938 fname += data_file_prefix;
946 int VisAOApp_base::checkDataFileOpen()
949 if(dataFileOpen && (
get_curr_time()-dataFileOpenTime > data_log_time_length))
952 dataFileOpen =
false;
958 fname = getDataFileName();
959 dataof.open(fname.c_str());
962 Logger::get()->log(Logger::LOG_LEV_ERROR,
"Error opening data file. AOsystem data may not be logged correctly");
963 dataFileOpen =
false;
964 dataFileOpenTime = 0;
985 std::string tmp, com, response;
990 com = fc->server_response;
1005 std::string tmp, com, response;
1010 com = fc->server_response;
1017 std::cerr <<
"Fix this bug! " << __FILE__ <<
" line " << __LINE__ <<
"\n";
1030 std::string tmp, com, response;
1035 com = fc->server_response;
1050 std::string response;
1056 response = vab->
auto_command(fc->server_response, fc->seqmsg);
1066 pthread_attr_t attr;
1067 pthread_attr_init(&attr);
1068 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1069 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
1078 fifo_channel *fc = (fifo_channel *)ptr;
1081 pthread_mutex_lock(&vab->
my_mutex);
1085 pthread_mutex_unlock(&vab->
my_mutex);
1093 pthread_attr_t attr;
1094 pthread_attr_init(&attr);
1095 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1096 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
1105 fifo_channel *fc = (fifo_channel *)ptr;
1108 pthread_mutex_lock(&vab->
my_mutex);
1112 pthread_mutex_unlock(&vab->
my_mutex);
1120 pthread_attr_t attr;
1121 pthread_attr_init(&attr);
1122 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1123 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
1131 fifo_channel *fc = (fifo_channel *)ptr;
1134 pthread_mutex_lock(&vab->
my_mutex);
1138 pthread_mutex_unlock(&vab->
my_mutex);
1146 pthread_attr_t attr;
1147 pthread_attr_init(&attr);
1148 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1149 pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
1157 fifo_channel *fc = (fifo_channel *)ptr;
1160 pthread_mutex_lock(&vab->
my_mutex);
1164 pthread_mutex_unlock(&vab->
my_mutex);
1174 if(com->length() == 0)
1178 pos = com->find_first_of(
"\n\r", 0);
1185 if(pos < (
int)com->length()-1)
1187 if((*com)[pos+1] ==
'\n' || (*com)[pos+1] ==
'\r') com->erase(pos, 1);
1190 *comln = com->substr(0, pos);
1191 com->erase(0, pos+1);
1197 void * __start_profiler(
void * ptr)
virtual ~VisAOApp_base()
Virtual destructor.
int fifo_list_do_pending_read(fifo_list *fl)
Runs through the pending reads, dispatching each handler in turn.
std::string control_mode_string()
Convenience function to return the control type string, e.g. "REMOTE".
int connect_fifo_list_nolock(fifo_list *fl)
Open each fifo in the list, without exclusive locks.
int write_fifo_channel(fifo_channel *fc, const char *com, int comlen)
Write data to the output channel.
void * __com_remote_thread_handler(void *ptr)
The start function for remote handling with a thread.
int nchan
The number of channels.
int statusboard_shmemid
The ID of the shared memory block.
int setup_fifo_list(fifo_list *fl, int nch)
Setup a fifo_list.
double get_curr_time(void)
Gets the current CLOCK_REALTIME time, returns it in seconds to double precision.
int com_local_handler(fifo_channel *fc)
fifo_channel handler for local commands
int com_remote_thread_handler(fifo_channel *fc)
Thread version of the fifo_channel handler for remote commands.
int com_auto_thread_handler(fifo_channel *fc)
Thread version of the fifo_channel handler for auto commands.
static int default_control_mode
The default control mode.
double wait_to
The timeout for waiting on responses from FIFOs in seconds.
void * __com_auto_thread_handler(void *ptr)
The start function for auto handling with a thread.
int read_fifo_channel(fifo_channel *fc)
Read data from the input fifo channel.
int setup_sigio()
Setup the SIGIO signal handling.
pthread_mutex_t my_mutex
Mutex used by the threaded versions of the standard command handlers.
std::ostringstream logss
Conveninence string stream for building log messages.
void * statusboard_shmemptr
The pointer to the shared memory block for the statusboard.
virtual void error_report(int LogLevel, std::string emsg)
Report an error. Also calls log_msg.
virtual std::string __local_command(std::string com, char *seqmsg)
Processing for local commands.
int RT_priority
The real-time scheduling priority. Default is 0.
int write_fifo_channel_seqmsg(fifo_channel *fc, const char *com, int comlen, char *seqmsg)
Write data to the output channel, including a sequence message.
int set_RT_priority(int prio)
Set the real-time priority of the current process.
std::string com_path
The control fifo path base name.
virtual int update_statusboard()
Update the status board.
virtual int setup_baseApp(bool usethreads=false)
Install fifo channels.
int check_fifo_list_RTpending()
Check for pending reads for the fifo list while using the RT signals.
int wopen_nonblock(char *fname, int nolock)
Open a file for non-blocking writes.
fifo_channel * fifo_ch
An array of fifo_channels.
uid_t euid_real
The real user id of the proces.
std::string com_path_local
The path for the local control fifos.
std::string app_name
The name of the application.
int setup_fifo_list(int nfifos)
Allocate the fifo_list.
VisAOApp_base()
Default constructor.
#define VISAO_SCHED_POLICY
The real-time scheduling policy used by the VisAO system.
uid_t euid_called
The user id of the process as called (that is when the constructor gets called).
void catch_fifo_response_list(int signum)
A SIGIO handler, which uses global_fifo_list to call the appropriate handler.
std::string com_path_remote
The path for the remote control fifos.
std::string com_path_script
The path for the script control fifos.
virtual std::string __script_command(std::string com, char *seqmsg)
Processing for script commands - not normally overriden.
key_t statusboard_shmemkey
The key used to lookup the shared memory.
virtual std::string auto_command(std::string com, char *seqmsg)
Processing for auto commands - to be overriden.
int getnextline(std::string *comln, std::string *com)
Utility function for parsing command buffers line by line.
int TimeToDie
Global set by SIGTERM.
void catch_fifo_pending_reads(int signum, siginfo_t *siginf, void *ucont)
A handler for a R/T SIGIO, which just updates the pending reads meta data but does no actual signal h...
std::string control_mode_response()
Convenience function to return the control type response string, e.g. "A\n".
int com_script_handler(fifo_channel *fc)
fifo_channel handler for script commands
void * attach_shm(size_t *sz, key_t mkey, int shmemid)
Attach to a shared memory buffer and get its size.
fifo_list fl
The list of named-pipe fifos used for inter-process comms.
The base class for VisAO applications.
std::string com_path_auto
The path for the auto control fifos.
int RTSIGIO_overflow
flag indicating that the signal queue has overflown.
int get_fifo_channel_response(std::string *resp, int ch)
Check, and wait if neccessary, for data on a fifo channel.
double pause_time
Time to pause during application main loop.
virtual std::string remote_command(std::string com)
Called by __remote_command after control state logic.
virtual int request_control(int cmode)
Calls request_control(cmode, 0).
int com_script_thread_handler(fifo_channel *fc)
Thread version of the fifo_channel handler for script commands.
int set_fifo_list_rtsig(fifo_list *fl)
Change from SIGIO to real time signals.
int com_local_thread_handler(fifo_channel *fc)
Thread version of the fifo_channel handler for local commands.
int set_app_name(std::string an)
Set the application name.
uid_t suid
The save-set user id of the process.
int connect_fifo_list(fifo_list *fl)
Open each fifo in the list, with exclusive lock on the input fifo.
virtual void dataLogger(timeval tv)
Log data at intervals.
int set_statusboard_shmemkey(key_t mkey)
Set the status board shared memory key. Does nothing else.
int init_fifo_list(fifo_list *fl)
Initialize the fifo_list.
virtual std::string __remote_command(std::string com, char *seqmsg)
Processing for remote commands.
virtual std::string script_command(std::string com)
Called by __script_command after control state logic.
void * __com_script_thread_handler(void *ptr)
The start function for script handling with a thread.
Declarations for VisAOApp_base.
int set_wait_to(double to)
Set the wait timeout for fifo channel responses.
int create_statusboard(size_t sz)
Creates and attaches to the statusboard shared memory.
virtual std::string get_app_name()
Get the application name.
int set_euid_called()
Changes the user id of the process to euid_called.
int connect_fifo_list()
Connect the fifo_list with exclusive locking.
static int control_mode
The current control mode.
int setup_RTsigio()
Setup SIGIO signal handling using realtime signals.
int get_visao_filename(char *buffer, struct timeval *tv)
Writes the standard visao unique filename for a timeval to the buffer.
virtual std::string local_command(std::string com)
Called by __local_command after control state logic.
int com_auto_handler(fifo_channel *fc)
fifo_channel handler for auto commands
int connect_fifo_list_nolock()
Connect the fifo_list without exclusive locking.
void catch_fifo_standard_sigio(int signum)
A handler for normal SIGIO when we should have gotten the R/T SIGIO, implying signal queue overflow...
void * __com_local_thread_handler(void *ptr)
The start function for local handling with a thread.
The namespace of VisAO software.
virtual std::string post_auto_command(std::string com, char *seqmsg=0)
Processing for auto commands - not normally overriden.
virtual void log_msg(int LogLevel, std::string lmsg)
Log a message.
int tot_pending_reads
The total number of pending reads.
#define RWBUFF_SZ
The size of the i/o buffer.
int create_shmem(int *shmemid, key_t mkey, size_t sz)
Create a shared memory buffer.
int set_euid_real()
Changes the user id fo the process to the real user id.
int com_remote_handler(fifo_channel *fc)
fifo_channel handler for remote commands
int set_fifo_list_channel(fifo_list *fl, int nch, int buffsz, const char *fin, const char *fout, int(*inp_hand)(fifo_channel *), void *adata)
Set the details of one channel in the list.
int write_fifo_channel(int ch, const char *com, int comlen, std::string *resp)
Write data to a fifo_channel, and get a response if desired.