Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

emcdummy.cc

Go to the documentation of this file.
00001 /*
00002   emcdummy.cc
00003 
00004   Dummy EMC that just accepts any NML command, and echoes a done status
00005   right away. Used for debugging external processes, e.g., FBICS.
00006 
00007   Note to coders: interface to this dummy process like this:
00008 
00009   // open NML buffers
00010   static RCS_CMD_CHANNEL * emcCommandBuffer = new RCS_CMD_CHANNEL(emcFormat, "emcCommand", "emc", "emc.nml");
00011   static RCS_STAT_CHANNEL * emcStatusBuffer = new RCS_STAT_CHANNEL(emcFormat, "emcStatus", "emc", "emc.nml");
00012   static EMC_STAT emcStatus = (EMC_STAT *) emcStatusBuffer->get_address();
00013   static int serial_number = 0;
00014   EMC_TASK_PLAN_OPEN open_msg;
00015   EMC_TASK_PLAN_RUN run_msg;
00016 
00017   // send open message
00018   strcpy(open_msg.file, "test.ngc");
00019   open_msg.serial_number = ++serial_number;
00020   commandBuffer->write(open_msg);
00021 
00022   // check for done
00023   for (;;)
00024   {
00025     statusBuffer->read();
00026     if (emcStatus->echo_serial_number == serial_number &&
00027         emcStatus->status == RCS_DONE)
00028     {
00029       break;
00030     }
00031     // else it's still executing
00032 
00033     // send run message
00034     run_msg.serial_number = ++serial_number;
00035     run_msg.line = 0; // run from beginning
00036     commandBuffer->write(run_msg);
00037 
00038     // check for done
00039     for (;;)
00040     {
00041       statusBuffer->read();
00042       if (emcStatus->echo_serial_number == serial_number &&
00043           emcStatus->status == RCS_DONE)
00044       {
00045         break;
00046       }
00047       // else it's still executing
00048     }
00049 
00050     // repeat
00051 */
00052 
00053 #include <stdlib.h>             // exit()
00054 #include <signal.h>             // signal(), SIGINT
00055 #include <string.h>             // strncpy()
00056 
00057 #include "rcs.hh"               // RCS_CMD_MSG, etc, esleep()
00058 #include "emc.hh"               // EMC NML
00059 
00060 // NML channels
00061 static RCS_CMD_CHANNEL * emcCommandBuffer = 0;
00062 static RCS_STAT_CHANNEL * emcStatusBuffer = 0;
00063 static NML * emcErrorBuffer = 0;
00064 
00065 // NML command channel data pointer
00066 static RCS_CMD_MSG * emcCommand = 0;
00067 
00068 // global EMC status
00069 EMC_STAT * emcStatus = 0;
00070 
00071 // name of NML file
00072 static char nmlFile[256] = "emc.nml";
00073 
00074 static int startup()
00075 {
00076   // get the NML command buffer
00077   emcCommandBuffer = new RCS_CMD_CHANNEL(emcFormat, "emcCommand", "emc", nmlFile);
00078   if (! emcCommandBuffer->valid())
00079     {
00080       rcs_print_error("can't get emcCommand buffer\n");
00081       return -1;
00082     }
00083 
00084   // get our command data structure
00085   emcCommand = emcCommandBuffer->get_address();
00086 
00087   // get the NML status buffer
00088   emcStatusBuffer = new RCS_STAT_CHANNEL(emcFormat, "emcStatus", "emc", nmlFile);
00089   if (! emcStatusBuffer->valid())
00090     {
00091       rcs_print_error("can't get emcStatus buffer\n");
00092       return -1;
00093     }
00094 
00095   // get our status data structure
00096   emcStatus = new EMC_STAT;
00097 
00098   // get the error log buffer
00099   emcErrorBuffer = new NML(nmlErrorFormat, "emcError", "emc", nmlFile);
00100   if (! emcErrorBuffer->valid())
00101     {
00102       rcs_print_error("can't get emcError buffer\n");
00103       return -1;
00104     }
00105 
00106   return 0;
00107 }
00108 
00109 // called to deallocate resources
00110 static int shutdown()
00111 {
00112   // delete the NML channels
00113 
00114   if (0 != emcErrorBuffer)
00115     {
00116       delete emcErrorBuffer;
00117       emcErrorBuffer = 0;
00118     }
00119 
00120   if (0 != emcStatusBuffer)
00121     {
00122       delete emcStatusBuffer;
00123       emcStatusBuffer = 0;
00124       emcStatus = 0;
00125     }
00126 
00127   if (0 != emcCommandBuffer)
00128     {
00129       delete emcCommandBuffer;
00130       emcCommandBuffer = 0;
00131       emcCommand = 0;
00132     }
00133 
00134   if( 0 != emcStatus)
00135     {
00136       delete emcStatus;
00137       emcStatus = 0;
00138     }
00139 
00140   return 0;
00141 }
00142 
00143 // signal handling code to stop main loop
00144 static int done;
00145 static void quit(int sig)
00146 {
00147   // set main's done flag
00148   done = 1;
00149 
00150   // restore signal handler
00151   signal(SIGINT, quit);
00152 }
00153 
00154 /*
00155   syntax: a.out {<nml file>}
00156 
00157   If <nml file> is provided, it will use this, otherwise it will use the
00158   default "emc.nml".
00159 
00160   This process will access the buffers "emcCommand", "emcStatus", and
00161   "emcError", as process "emc".
00162 
00163   ^C will nicely terminate this process.
00164 */
00165 int main(int argc, char *argv[])
00166 {
00167   NMLTYPE type;
00168 
00169   // set print destination to stdout, for console apps
00170   set_rcs_print_destination(RCS_PRINT_TO_STDOUT);
00171 
00172   // blank out the annoying RCS version message
00173   rcs_version_printed = 1;
00174 
00175   if (argc > 1)
00176     {
00177       // first arg is NML file-- copy it to global used in startup()
00178       strcpy(nmlFile, argv[1]);
00179     }
00180 
00181   // set up NML
00182   if (0 != startup())
00183     {
00184       exit(1);
00185     }
00186 
00187   // fill in NML status with initial blank values
00188   emcStatus->command_type = 0;
00189   emcStatus->echo_serial_number = 0;
00190   emcStatus->status = RCS_DONE;
00191 
00192   // enter dummy loop
00193   done = 0;
00194   while (! done)
00195     {
00196       // read command
00197       type = emcCommandBuffer->read();
00198 
00199       if (0 != type &&
00200           -1 != type)
00201         {
00202           // new command-- show "executing" immediately
00203           emcStatus->command_type = emcCommand->type;
00204           emcStatus->echo_serial_number = emcCommand->serial_number;
00205           emcStatus->status = RCS_EXEC;
00206           emcStatusBuffer->write(emcStatus);
00207         }
00208 
00209       // wait a second
00210       esleep(1.0);
00211 
00212       // now show "done"
00213       emcStatus->status = RCS_DONE;
00214       emcStatusBuffer->write(emcStatus);
00215     }
00216 
00217   if (0 != shutdown())
00218     {
00219       exit(1);
00220     }
00221 
00222   exit(0);
00223 }

Generated on Sun Dec 2 15:27:38 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001