Ignore:
Timestamp:
Oct 12, 2011, 1:02:01 PM (13 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/pymolproxy/pymolproxy.c

    r2460 r2614  
    9696static Stats stats;
    9797
    98 #define READTRACE       0
     98#define READTRACE       1
    9999#define EXPECTTRACE     0
    100100#define WRITETRACE      0
    101101
    102 static int debug = FALSE;
     102static int debug = TRUE;
    103103
    104104static FILE *flog;
     
    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 
    875850static int
    876851BmpCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
     
    12451220    }
    12461221    {
     1222        int f;
     1223        ssize_t nWritten;
    12471224        char fileName[200];
    1248         FILE *f;
    1249         ssize_t nWritten;
    1250         static unsigned long count = 0;
    1251 
     1225
     1226        strcpy(fileName, "/tmp/pdb.XXXXXX");
    12521227        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);
     1228        f = mkstemp(fileName);
     1229        if (f < 0) {
     1230            Tcl_AppendResult(interp, "can't create temporary file \"",
     1231                fileName, "\":", Tcl_PosixError(interp), (char *)NULL);
    12601232            goto error;
    12611233        }
    1262         nWritten = fwrite(data, sizeof(char), nBytes, f);
     1234        nWritten = write(f, data, nBytes);
    12631235        if (nBytes != nWritten) {
    12641236            Tcl_AppendResult(interp, "can't write PDB data to \"",
    12651237                             fileName, "\": ", Tcl_PosixError(interp),
    12661238                             (char *)NULL);
    1267             fclose(f);
     1239            close(f);
    12681240            goto error;
    12691241        }
    1270         fclose(f);
    1271         Pymol(proxyPtr, "load %s,%s,%d\n", fileName, name, state);
     1242        close(f);
     1243        Pymol(proxyPtr, "loadandremovepdbfile %s,%s,%d\n", fileName, name, state);
    12721244        proxyPtr->status = TCL_OK;
    12731245    }
     
    14001372        return TCL_ERROR;
    14011373    }
     1374    stats.nFrames++;
     1375    stats.nBytes += nBytes;
     1376    return proxyPtr->status;
     1377}
     1378
     1379
     1380static int
     1381PpmCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
     1382{
     1383    char buffer[BUFSIZ];
     1384    int nBytes=0;
     1385    PymolProxy *proxyPtr = clientData;
     1386    size_t length;
     1387    Image *imgPtr;
     1388
     1389    clear_error(proxyPtr);
     1390
     1391    /* Force pymol to update the current scene. */
     1392    Pymol(proxyPtr, "refresh\n");
     1393    Pymol(proxyPtr, "png -,format=1\n");
     1394
     1395    if (Expect(proxyPtr, "ppm image follows: ", buffer, BUFSIZ-1) != TCL_OK) {
     1396        return TCL_ERROR;
     1397    }
     1398    if (sscanf(buffer, "ppm image follows: %d\n", &nBytes) != 1) {
     1399        Tcl_AppendResult(interp, "can't get # bytes from \"", buffer, "\"",
     1400                         (char *)NULL);
     1401        return TCL_ERROR;
     1402    }
     1403    sprintf(buffer, "nv>image %d %d %d %d\n", nBytes, proxyPtr->cacheId,
     1404            proxyPtr->frame, proxyPtr->rockOffset);
     1405    length = strlen(buffer);
     1406    imgPtr = NewImage(proxyPtr, nBytes + length);
     1407    strcpy(imgPtr->data, buffer);
     1408    if (ReadFollowingData(&proxyPtr->server, imgPtr->data + length, nBytes)
     1409        != BUFFER_OK) {
     1410        ERROR("can't read %d bytes for \"image follows\" buffer: %s", nBytes,
     1411              strerror(errno));
     1412        return  TCL_ERROR;
     1413    }
     1414#ifdef notdef
     1415    if (Expect(proxyPtr, " ScenePNG", buffer, BUFSIZ-1) != TCL_OK) {
     1416        return TCL_ERROR;
     1417    }
     1418#endif
    14021419    stats.nFrames++;
    14031420    stats.nBytes += nBytes;
     
    20062023ProxyInit(int cin, int cout, char *const *argv)
    20072024{
     2025    char stderrFile[200];
    20082026    int status, result = 0;
    20092027    int sin[2];
     
    20272045
    20282046    parent = getpid();
     2047    sprintf(stderrFile, "/tmp/pymol%d.stderr", parent);
    20292048
    20302049    /* Fork the new process.  Connect I/O to the new socket.  */
     
    20372056
    20382057    interp = Tcl_CreateInterp();
    2039     if (CreateTmpDir(interp) != TCL_OK) {
    2040         ERROR(Tcl_GetStringResult(interp));
    2041     }
    20422058    Tcl_MakeSafe(interp);
    20432059
     
    20592075        dup2(sin[0],  0);               // stdin
    20602076        dup2(sout[1], 1);               // stdout
    2061         sprintf(path, "/tmp/pymol%d/stderr", parent);
    2062         f = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
     2077        f = open(stderrFile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    20632078        if (f < 0) {
    20642079            ERROR("can't open server error file `%s': %s", path,
     
    21082123    Tcl_CreateCommand(interp, "pan",           PanCmd,           &proxy, NULL);
    21092124    Tcl_CreateCommand(interp, "png",           PngCmd,           &proxy, NULL);
     2125    Tcl_CreateCommand(interp, "ppm",           PpmCmd,           &proxy, NULL);
    21102126    Tcl_CreateCommand(interp, "print",         PrintCmd,         &proxy, NULL);
    21112127    Tcl_CreateCommand(interp, "raw",           RawCmd,           &proxy, NULL);
     
    21332149    PollForEvents(&proxy);
    21342150
     2151    unlink(stderrFile);
    21352152    close(proxy.cout);
    21362153    close(proxy.sout);
     
    21592176    INFO("pymol server process ended (result=%d)", result);
    21602177
    2161     DestroyTmpDir();
    21622178    Tcl_DeleteInterp(interp);
    21632179   
     
    23012317            if ((nChannels == 0) || (proxyPtr->flags & FORCE_UPDATE)) {
    23022318                if (proxyPtr->flags & UPDATE_PENDING) {
    2303                     Tcl_Eval(proxyPtr->interp, "bmp");
     2319                    Tcl_Eval(proxyPtr->interp, "ppm");
    23042320                    proxyPtr->flags &= ~UPDATE_PENDING;
    23052321                }
Note: See TracChangeset for help on using the changeset viewer.