Ignore:
Timestamp:
May 22, 2015 2:47:15 PM (9 years ago)
Author:
ldelgass
Message:

move old processCommands to Command.cpp

Location:
nanovis/branches/1.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nanovis/branches/1.2

  • nanovis/branches/1.2/Command.cpp

    r5588 r5603  
    3737#include <unistd.h>                     /* Needed for getpid, gethostname,
    3838                                         * write, etc. */
    39 
     39#if !defined(USE_NEW_EVENT_LOOP) && !defined(USE_THREADS)
     40#include <fcntl.h>
     41#endif
    4042#include <sstream>
    4143
     
    22362238}
    22372239
     2240#if !defined(USE_NEW_EVENT_LOOP) && !defined(USE_THREADS)
     2241int
     2242nv::processCommands(Tcl_Interp *interp, FILE *inFile, int fdOut)
     2243{
     2244    int ret = 0;
     2245    int status = TCL_OK;
     2246
     2247    Tcl_DString cmdbuffer;
     2248    Tcl_DStringInit(&cmdbuffer);
     2249
     2250    int flags = fcntl(0, F_GETFL, 0);
     2251    fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
     2252
     2253    //  Read and execute as many commands as we can from stdin...
     2254    bool isComplete = false;
     2255    while ((!feof(inFile)) && (status == TCL_OK)) {
     2256        //
     2257        //  Read the next command from the buffer.  First time through we
     2258        //  block here and wait if necessary until a command comes in.
     2259        //
     2260        //  BE CAREFUL: Read only one command, up to a newline.  The "volume
     2261        //  data follows" command needs to be able to read the data
     2262        //  immediately following the command, and we shouldn't consume it
     2263        //  here.
     2264        //
     2265        while (!feof(inFile)) {
     2266            int c = fgetc(inFile);
     2267            char ch;
     2268            if (c <= 0) {
     2269                if (errno == EWOULDBLOCK) {
     2270                    break;
     2271                }
     2272                return -1;
     2273            }
     2274            ch = (char)c;
     2275            Tcl_DStringAppend(&cmdbuffer, &ch, 1);
     2276            if (ch == '\n') {
     2277                isComplete = Tcl_CommandComplete(Tcl_DStringValue(&cmdbuffer));
     2278                if (isComplete) {
     2279                    break;
     2280                }
     2281            }
     2282        }
     2283        // no command? then we're done for now
     2284        if (Tcl_DStringLength(&cmdbuffer) == 0) {
     2285            break;
     2286        }
     2287        if (isComplete) {
     2288            // back to original flags during command evaluation...
     2289            fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
     2290            struct timeval start, finish;
     2291            gettimeofday(&start, NULL);
     2292            status = ExecuteCommand(interp, &cmdbuffer);
     2293            gettimeofday(&finish, NULL);
     2294            // non-blocking for next read -- we might not get anything
     2295            fcntl(0, F_SETFL, flags | O_NONBLOCK);
     2296            isComplete = false;
     2297            g_stats.cmdTime += (MSECS_ELAPSED(start, finish) / 1.0e+3);
     2298            g_stats.nCommands++;
     2299        }
     2300    }
     2301    fcntl(0, F_SETFL, flags);
     2302
     2303    if (status != TCL_OK) {
     2304        if (handleError(interp, status, fdOut) < 0) {
     2305            return -1;
     2306        }
     2307        TRACE("Leaving on ERROR");
     2308    }
     2309
     2310    return ret;
     2311}
     2312
     2313#endif
     2314
    22382315/**
    22392316 * \brief Execute commands from client in Tcl interpreter
Note: See TracChangeset for help on using the changeset viewer.