remote.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001,2002,2003 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Andreas Klust <klust@users.sf.net>
00009  * WWW Home: http://gxsm.sf.net
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00027 
00028 #include <fstream>
00029 
00030 #include <cstdio>
00031 #include <cstdlib>
00032 #include <cmath>
00033 #include <cstring>
00034 
00035 #include <gnome.h>
00036 
00037 // #include <unistd.h>
00038 #include <sys/time.h>
00039 #include <sys/types.h>
00040 
00041 
00042 #include "glbvars.h"
00043 #include "remote.h"
00044 
00045 
00046 // remote:
00047 // basics of remote control
00048 // - open / close fifo
00049 // - reading and splitting comands
00050 // - error check
00051 remote::remote(){
00052   waitflg=FALSE;
00053   remotefifo=0;
00054   statusfifo=0;
00055 }
00056 
00057 remote::~remote(){ 
00058   rcclose(); 
00059 }
00060 
00061 void remote::rcopen(gchar *crtlfifo, gchar *statfifo){ 
00062   if(!remotefifo){ // only once !!
00063     remotefifo = open(crtlfifo, O_RDONLY);
00064   }
00065   if(!statusfifo && statfifo){ // only once !!
00066     statusfifo = open(statfifo, O_WRONLY);
00067   }
00068 }
00069 
00070 void remote::rcclose(){ 
00071   if(remotefifo){
00072     close(remotefifo);
00073     remotefifo=0;
00074   }
00075   if(statusfifo){
00076     close(statusfifo);
00077     statusfifo=0;
00078   }
00079 }
00080 
00081 int remote::writestatus(gchar *mld){
00082   if(statusfifo){
00083     return write(statusfifo, mld, strlen(mld));
00084   }
00085   return 0;
00086 }
00087 
00088 int remote::readfifo(GIOChannel *src){
00089         gchar *cmdstring = g_new (gchar, 1024);
00090 //      gchar *cmdstring = NULL;
00091         gchar **cmdlist, **cmd;
00092         gsize size;
00093         memset (cmdstring, 0, 1024);
00094 //      GError *error = NULL; // needs glibc2.x ...
00095 
00096         XSM_DEBUG(DBG_L2, "**** remote::readfifo read()" );
00097 
00098 //      g_io_channel_read_to_end (src, &cmdstring, &size, &error);
00099         size=read(remotefifo, cmdstring, 1024);
00100 
00101         if(size){
00102                 XSM_DEBUG(DBG_L2, "**** remote::readfifo DATA:@"<<cmdstring<<"@" );
00103                 cmdlist = g_strsplit(cmdstring, "\n", 100);
00104                 cmd = cmdlist;
00105                 
00106                 while(*cmd)
00107                         eval(*cmd++);
00108                 
00109                 g_strfreev(cmdlist);
00110                 XSM_DEBUG(DBG_L2, "**** remote::readfifo eval done " );
00111         }
00112         g_free (cmdstring);
00113         XSM_DEBUG(DBG_L2, "**** remote::readfifo return " );
00114         return TRUE;
00115 }
00116 
00117 int remote::eval(char *cmdstr){
00118   XSM_DEBUG(DBG_L2, "Remote: got cmd =>" << cmdstr << "<=" );
00119   return 0;
00120 }
00121 
00122 // Task is to check remote input and pe-parse it
00123 int remote_crtl::eval(gchar *cmdstr){
00124   int n;
00125   gchar **cmdargv, **p, *r, *rr;
00126 
00127   // strip cmdstring
00128   // convert Spaces to Tabs (\t), Quotes "\ " are remaining 
00129   for(rr=cmdstr, r=cmdstr; *r; ++r){
00130     if(rr==cmdstr && (*r == ' ' || *r == '\t')) continue;
00131     if(*r == '\\' && *(r+1)){ *rr++ = *++r; continue; }
00132     if(*r == ' ' || *r == '\t'){ 
00133       *rr++ = '\t'; 
00134       while(*(r+1) == ' ') ++r; 
00135       continue; 
00136     }
00137     *rr++ = *r;
00138   }
00139   while(*rr) *rr++ = ' ';
00140   g_strstrip(cmdstr);
00141 
00142   cmdargv = g_strsplit(cmdstr, "\t", 10);
00143 
00144   for(n=0, p=cmdargv; *p; ++p, ++n);
00145 
00146   if(n>1){
00147     addcmd(cmdargv);
00148     sendcmd(cmdargv[0], cmdargv[1], cmdargv);
00149   }else{
00150     senderror("Sorry, please be a bit more verbose !");
00151   }
00152 
00153   g_strfreev(cmdargv);
00154   return 0;
00155 }
00156 
00157 remote_crtl::remote_crtl(remote_verb *rvlist) : remote(){
00158   rlist = rvlist;
00159   rfkt = NULL;
00160 }
00161 
00162 remote_crtl::~remote_crtl(){
00163 }
00164 
00165 int remote_crtl::ExecAction(gchar **arglist){ 
00166   if(rfkt){ 
00167           XSM_DEBUG(DBG_L2, "EA: excuting..." );
00168     if(rfkt->CmdFkt)
00169       (rfkt->CmdFkt)(arglist, rfkt->data);
00170     else
00171       XSM_DEBUG(DBG_L2, "CmdFkt is Zero !" );
00172     XSM_DEBUG(DBG_L2, "EA: done" );
00173     rfkt=NULL; 
00174   } 
00175   return 0;
00176 }
00177 
00178 gboolean remote_crtl::FindFktExec(char *verb, char *cmd, gchar **arglist){
00179   remote_verb *rt=rlist;
00180   remote_cmd  *cl=NULL;
00181   while(rt->verb && rt->typ != REMO_ENDLIST){
00182     if(strncmp(rt->verb, verb, MAX(strlen(verb) , strlen(rt->verb))))
00183       rt++;
00184     else{
00185       cl = rt->cmd_list;
00186       switch(rt->typ){
00187       case REMO_ACTION:
00188         while(cl->cmd){
00189           if(strncmp(cl->cmd, cmd, MAX(strlen(cmd) , strlen(cl->cmd))))
00190             cl++;
00191           else{
00192             rfkt = cl;                // spool Action
00193             ExecAction(arglist);
00194             XSM_DEBUG (DBG_L3, "Exec OK.");
00195             return TRUE;
00196           }
00197         }
00198         break;
00199 
00200       case REMO_MENUACTION:
00201         rfkt = cl;                // spool Action
00202         ExecAction(arglist);
00203         return TRUE;
00204 
00205       case REMO_SETENTRY:
00206         checkentry(arglist);
00207         XSM_DEBUG (DBG_L3, "Set Entry OK.");
00208         return TRUE;
00209 
00210       case REMO_CBACTION:
00211         checkactions(arglist);
00212         XSM_DEBUG (DBG_L3, "Action done.");
00213         return TRUE;
00214 
00215       default: break;
00216       }
00217     }
00218   }
00219   return FALSE;
00220 }
00221 
00222 void remote_crtl::senderror(char *errstr){
00223   if(!FindFktExec("ERROR", errstr, NULL))
00224     XSM_DEBUG_ERROR (DBG_L1, "No Errorfkt: " << errstr );
00225 }
00226 
00227 void remote_crtl::sendcmd(char *cmd, char *arg, gchar **arglist){
00228   if(!FindFktExec(cmd, arg, arglist))
00229     XSM_DEBUG_ERROR (DBG_L1, "No Cmdfkt: " << cmd << " " << arg );
00230 }
00231 
00232 void remote_crtl::quit(){
00233   if(!FindFktExec("quit", NULL, NULL))
00234     XSM_DEBUG_ERROR (DBG_L1, "No Quitfkt: -- " );
00235 }
00236 
00237 
00238 
00239 
00240 

Generated on Sat Apr 1 09:04:03 2006 for GXSM by  doxygen 1.4.6