Ignore:
Timestamp:
Nov 15, 2011, 11:56:36 AM (13 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/blt4/packages/vizservers/pymolproxy/pymolproxy.c

    r2542 r2680  
    1010 *      server.  Responses from the pymol server are translated into Tcl
    1111 *      commands and send to the molvisviewer widget. For example, resulting
    12  *      image rendered offscreen is returned as BMP-formatted image data.
     12 *      image rendered offscreen is returned as ppm-formatted image data.
    1313 *
    1414 *  Copyright (c) 2004-2006  Purdue Research Foundation
     
    9696static Stats stats;
    9797
    98 #define READTRACE       0
     98#define READTRACE       1
    9999#define EXPECTTRACE     0
    100100#define WRITETRACE      0
     
    446446    proxyPtr->error = 0;
    447447    proxyPtr->status = TCL_OK;
    448 }
    449 
    450 static int
    451 CreateTmpDir(Tcl_Interp *interp)
    452 {
    453     const char script[] = {
    454         "set path \"/tmp/pymol[pid]\"\n"
    455         "if { [file exists $path] } {\n"
    456         "    file delete -force $path\n"
    457         "}\n"
    458         "file mkdir $path\n"
    459     };
    460     return Tcl_GlobalEval(interp, script);
    461 }
    462 
    463 static void
    464 DestroyTmpDir()
    465 {
    466     char cmd[BUFSIZ];
    467 
    468     sprintf(cmd, "/bin/rm -rf /tmp/pymol%d", getpid());
    469     if (system(cmd) < 0) {
    470         ERROR("can't delete tmp directory: %s\n", strerror(errno));
    471     }
    472448}
    473449
     
    872848}
    873849
    874 
    875 static int
    876 BmpCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
    877 {
    878     char buffer[BUFSIZ];
    879     unsigned int nBytes=0;
    880     PymolProxy *proxyPtr = clientData;
    881     Image *imgPtr;
    882     size_t length;
    883     clear_error(proxyPtr);
    884 
    885     if (proxyPtr->flags & INVALIDATE_CACHE)
    886         proxyPtr->cacheId++;
    887 
    888     proxyPtr->flags &= ~(UPDATE_PENDING|FORCE_UPDATE|INVALIDATE_CACHE);
    889 
    890     /* Force pymol to update the current scene. */
    891     UpdateSettings(proxyPtr);
    892 
    893     Pymol(proxyPtr, "refresh\n");
    894     Pymol(proxyPtr, "bmp -\n");
    895     if (Expect(proxyPtr, "bmp image follows: ", buffer, BUFSIZ) != TCL_OK) {
    896         return TCL_ERROR;
    897     }
    898     if (sscanf(buffer, "bmp image follows: %d\n", &nBytes) != 1) {
    899         Tcl_AppendResult(interp, "can't get # bytes from \"", buffer, "\"",
    900                          (char *)NULL);
    901         return TCL_ERROR;
    902     }
    903     sprintf(buffer, "nv>image %d %d %d %d\n", nBytes, proxyPtr->cacheId,
    904             proxyPtr->frame, proxyPtr->rockOffset);
    905 
    906     length = strlen(buffer);
    907     imgPtr = NewImage(proxyPtr, nBytes + length);
    908     strcpy(imgPtr->data, buffer);
    909     if (ReadFollowingData(&proxyPtr->server, imgPtr->data + length, nBytes)
    910         != BUFFER_OK) {
    911         ERROR("can't read %d bytes for \"image follows\" buffer: %s", nBytes,
    912               strerror(errno));
    913         return  TCL_ERROR;
    914     }
    915     stats.nFrames++;
    916     stats.nBytes += nBytes;
    917     return proxyPtr->status;
    918 }
    919 
    920850static int
    921851CartoonCmd(ClientData clientData, Tcl_Interp *interp, int argc,
     
    12451175    }
    12461176    {
     1177        int f;
     1178        ssize_t nWritten;
    12471179        char fileName[200];
    1248         FILE *f;
    1249         ssize_t nWritten;
    1250         static unsigned long count = 0;
    1251 
     1180
     1181        strcpy(fileName, "/tmp/pdb.XXXXXX");
    12521182        proxyPtr->status = TCL_ERROR;
    1253         sprintf(fileName, "/tmp/pymol%d/%ld.pdb", getpid(), count);
    1254         count++;
    1255         f = fopen(fileName, "w");
    1256         if (f == NULL) {
    1257             Tcl_AppendResult(interp, "can't create temporary file \"",
    1258                              fileName, "\": ", Tcl_PosixError(interp),
    1259                              (char *)NULL);
     1183        f = mkstemp(fileName);
     1184        if (f < 0) {
     1185            Tcl_AppendResult(interp, "can't create temporary file \"",
     1186                fileName, "\":", Tcl_PosixError(interp), (char *)NULL);
    12601187            goto error;
    12611188        }
    1262         nWritten = fwrite(data, sizeof(char), nBytes, f);
     1189        nWritten = write(f, data, nBytes);
    12631190        if (nBytes != nWritten) {
    12641191            Tcl_AppendResult(interp, "can't write PDB data to \"",
    12651192                             fileName, "\": ", Tcl_PosixError(interp),
    12661193                             (char *)NULL);
    1267             fclose(f);
     1194            close(f);
    12681195            goto error;
    12691196        }
    1270         fclose(f);
    1271         Pymol(proxyPtr, "load %s,%s,%d\n", fileName, name, state);
     1197        close(f);
     1198        Pymol(proxyPtr, "loadandremovepdbfile %s,%s,%d\n", fileName, name, state);
    12721199        proxyPtr->status = TCL_OK;
    12731200    }
     
    14001327        return TCL_ERROR;
    14011328    }
     1329    stats.nFrames++;
     1330    stats.nBytes += nBytes;
     1331    return proxyPtr->status;
     1332}
     1333
     1334
     1335static int
     1336PpmCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
     1337{
     1338    char buffer[BUFSIZ];
     1339    int nBytes=0;
     1340    PymolProxy *proxyPtr = clientData;
     1341    size_t length;
     1342    Image *imgPtr;
     1343
     1344    clear_error(proxyPtr);
     1345
     1346    /* Force pymol to update the current scene. */
     1347    Pymol(proxyPtr, "refresh\n");
     1348    Pymol(proxyPtr, "png -,format=1\n");
     1349
     1350    if (Expect(proxyPtr, "ppm image follows: ", buffer, BUFSIZ-1) != TCL_OK) {
     1351        return TCL_ERROR;
     1352    }
     1353    if (sscanf(buffer, "ppm image follows: %d\n", &nBytes) != 1) {
     1354        Tcl_AppendResult(interp, "can't get # bytes from \"", buffer, "\"",
     1355                         (char *)NULL);
     1356        return TCL_ERROR;
     1357    }
     1358    sprintf(buffer, "nv>image %d %d %d %d\n", nBytes, proxyPtr->cacheId,
     1359            proxyPtr->frame, proxyPtr->rockOffset);
     1360    length = strlen(buffer);
     1361    imgPtr = NewImage(proxyPtr, nBytes + length);
     1362    strcpy(imgPtr->data, buffer);
     1363    if (ReadFollowingData(&proxyPtr->server, imgPtr->data + length, nBytes)
     1364        != BUFFER_OK) {
     1365        ERROR("can't read %d bytes for \"image follows\" buffer: %s", nBytes,
     1366              strerror(errno));
     1367        return  TCL_ERROR;
     1368    }
     1369#ifdef notdef
     1370    if (Expect(proxyPtr, " ScenePNG", buffer, BUFSIZ-1) != TCL_OK) {
     1371        return TCL_ERROR;
     1372    }
     1373#endif
    14021374    stats.nFrames++;
    14031375    stats.nBytes += nBytes;
     
    20061978ProxyInit(int cin, int cout, char *const *argv)
    20071979{
     1980    char stderrFile[200];
    20081981    int status, result = 0;
    20091982    int sin[2];
     
    20272000
    20282001    parent = getpid();
     2002    sprintf(stderrFile, "/tmp/pymol%d.stderr", parent);
    20292003
    20302004    /* Fork the new process.  Connect I/O to the new socket.  */
     
    20372011
    20382012    interp = Tcl_CreateInterp();
    2039     if (CreateTmpDir(interp) != TCL_OK) {
    2040         ERROR(Tcl_GetStringResult(interp));
    2041     }
    20422013    Tcl_MakeSafe(interp);
    20432014
     
    20592030        dup2(sin[0],  0);               // stdin
    20602031        dup2(sout[1], 1);               // stdout
    2061         sprintf(path, "/tmp/pymol%d/stderr", parent);
    2062         f = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
     2032        f = open(stderrFile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    20632033        if (f < 0) {
    20642034            ERROR("can't open server error file `%s': %s", path,
     
    20972067    proxy.interp = interp;
    20982068
    2099     Tcl_CreateCommand(interp, "bmp",           BmpCmd,           &proxy, NULL);
    21002069    Tcl_CreateCommand(interp, "cartoon",       CartoonCmd,       &proxy, NULL);
    21012070    Tcl_CreateCommand(interp, "cartoontrace",  CartoonTraceCmd,  &proxy, NULL);
     
    21082077    Tcl_CreateCommand(interp, "pan",           PanCmd,           &proxy, NULL);
    21092078    Tcl_CreateCommand(interp, "png",           PngCmd,           &proxy, NULL);
     2079    Tcl_CreateCommand(interp, "ppm",           PpmCmd,           &proxy, NULL);
    21102080    Tcl_CreateCommand(interp, "print",         PrintCmd,         &proxy, NULL);
    21112081    Tcl_CreateCommand(interp, "raw",           RawCmd,           &proxy, NULL);
     
    21332103    PollForEvents(&proxy);
    21342104
     2105    unlink(stderrFile);
    21352106    close(proxy.cout);
    21362107    close(proxy.sout);
     
    21592130    INFO("pymol server process ended (result=%d)", result);
    21602131
    2161     DestroyTmpDir();
    21622132    Tcl_DeleteInterp(interp);
    21632133   
     
    22322202            if (line != NULL) {
    22332203                Debug("STDOUT>%.*s", nBytes, line);
    2234                 Debug("Done with pymol stdout\n");
     2204                INFO("Garbage found from pymol server: %.%s", nBytes, line);
    22352205            } else if (nBytes == BUFFER_CONTINUE) {
    2236                 Debug("Done with pymol stdout\n");
    2237                 goto error;             /* Pymol server died unexpectedly. */
     2206                Debug("No data found on pymol stdout\n");
     2207                continue;
    22382208            } else {
    22392209                ERROR("can't read pymol stdout (nBytes=%d): %s\n", nBytes,
     
    23012271            if ((nChannels == 0) || (proxyPtr->flags & FORCE_UPDATE)) {
    23022272                if (proxyPtr->flags & UPDATE_PENDING) {
    2303                     Tcl_Eval(proxyPtr->interp, "bmp");
     2273                    Tcl_Eval(proxyPtr->interp, "ppm");
    23042274                    proxyPtr->flags &= ~UPDATE_PENDING;
    23052275                }
Note: See TracChangeset for help on using the changeset viewer.