Ignore:
Timestamp:
Apr 2, 2013, 1:31:30 PM (11 years ago)
Author:
ldelgass
Message:

Add writer thread to nanovis (set USE_THREADS in Makefile), more refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanovis/FlowCmd.cpp

    r3598 r3605  
    2525#include "nvconf.h"
    2626
     27#include "nanovisServer.h"
    2728#include "nanovis.h"
    2829#include "CmdProc.h"
    2930#include "Command.h"
     31#include "PPMWriter.h"
    3032#include "FlowCmd.h"
    3133#include "FlowTypes.h"
     
    4143#include "Trace.h"
    4244
     45using namespace nv;
    4346using namespace vrmath;
    4447
     
    231234        return TCL_ERROR;
    232235    }
    233     Rappture::Buffer buf;
     236    Rappture::Buffer buf(nBytes);
    234237    TRACE("Flow data loading bytes: %d components: %d", nBytes, nComponents);
    235238    if (GetDataStream(interp, buf, nBytes) != TCL_OK) {
    236239        return TCL_ERROR;
    237240    }
     241    char *bytes = (char *)buf.bytes();
     242    size_t length = buf.size();
     243
    238244    Rappture::Unirect3d *dataPtr;
    239245    dataPtr = new Rappture::Unirect3d(nComponents);
    240246
    241247    Flow *flow = (Flow *)clientData;
    242     size_t length = buf.size();
    243     char *bytes = (char *)buf.bytes();
    244248    if ((length > 4) && (strncmp(bytes, "<DX>", 4) == 0)) {
    245249        if (!dataPtr->importDx(result, nComponents, length - 4, bytes + 4)) {
     
    292296    {
    293297        char info[1024];
    294         ssize_t nWritten;
    295         size_t length;
    296 
    297         length = sprintf(info, "nv>data tag %s min %g max %g\n",
    298                          flow->name(), dataPtr->magMin(), dataPtr->magMax());
    299         nWritten  = write(1, info, length);
    300         assert(nWritten == (ssize_t)strlen(info));
     298        int length =
     299            sprintf(info, "nv>data tag %s min %g max %g\n",
     300                    flow->name(), dataPtr->magMin(), dataPtr->magMax());
     301#ifdef USE_THREADS
     302        queueResponse(info, (size_t)length, Response::VOLATILE);
     303#else
     304        if (SocketWrite(info, (size_t)length) < 0) {
     305            return TCL_ERROR;
     306        }
     307#endif
    301308    }
    302309    NanoVis::eventuallyRedraw(NanoVis::MAP_FLOWS);
     
    10641071
    10651072static int
    1066 ppmWriteToFile(Tcl_Interp *interp, const char *path, FlowVideoSwitches *switchesPtr)
    1067 {
    1068     int f;
    1069    
    1070     /* Open the named file for writing. */
    1071     f = creat(path, 0600);
    1072     if (f < 0) {
    1073         Tcl_AppendResult(interp, "can't open temporary image file \"", path,
    1074                          "\": ", Tcl_PosixError(interp), (char *)NULL);
    1075         return TCL_ERROR;
    1076     }
    1077     // Generate the PPM binary file header
    1078     char header[200];
    1079 #define PPM_MAXVAL 255
    1080     sprintf(header, "P6 %d %d %d\n", switchesPtr->width, switchesPtr->height,
    1081         PPM_MAXVAL);
    1082 
    1083     size_t header_length = strlen(header);
    1084     size_t wordsPerRow = (switchesPtr->width * 24 + 31) / 32;
    1085     size_t bytesPerRow = wordsPerRow * 4;
    1086     size_t rowLength = switchesPtr->width * 3;
    1087     size_t numRecords = switchesPtr->height + 1;
    1088 
    1089     struct iovec *iov;
    1090     iov = (struct iovec *)malloc(sizeof(struct iovec) * numRecords);
    1091 
    1092     // Add the PPM image header.
    1093     iov[0].iov_base = header;
    1094     iov[0].iov_len = header_length;
    1095 
    1096     // Now add the image data, reversing the order of the rows.
    1097     int y;
    1098     unsigned char *srcRowPtr = NanoVis::screenBuffer;
    1099     /* Reversing the pointers for the image rows.  PPM is top-to-bottom. */
    1100     for (y = switchesPtr->height; y >= 1; y--) {
    1101         iov[y].iov_base = srcRowPtr;
    1102         iov[y].iov_len = rowLength;
    1103         srcRowPtr += bytesPerRow;
    1104     }
    1105     if (writev(f, iov, numRecords) < 0) {
    1106         Tcl_AppendResult(interp, "writing image to \"", path, "\" failed: ",
    1107                          Tcl_PosixError(interp), (char *)NULL);
    1108         free(iov);
    1109         close(f);
    1110         return TCL_ERROR;
    1111     }
    1112     close(f);
    1113     free(iov);
    1114     return TCL_OK;
    1115 }
    1116 
    1117 static int
    11181073MakeImageFiles(Tcl_Interp *interp, char *tmpFileName,
    11191074               FlowVideoSwitches *switchesPtr, bool *cancelPtr)
    11201075{
    11211076    struct pollfd pollResults;
    1122     pollResults.fd = fileno(NanoVis::stdin);
     1077    pollResults.fd = fileno(nv::g_fIn);
    11231078    pollResults.events = POLLIN;
    11241079#define PENDING_TIMEOUT          10  /* milliseconds. */
     
    11601115
    11611116        sprintf(tmpFileName + length, "/image%d.ppm", i);
    1162         result = ppmWriteToFile(interp, tmpFileName, switchesPtr);
     1117        result = nv::writePPMFile(tmpFileName, NanoVis::screenBuffer,
     1118                                  switchesPtr->width, switchesPtr->height);
    11631119        if (result != TCL_OK) {
    11641120            break;
     
    12301186    sprintf(cmd,"nv>image -type movie -token \"%s\" -bytes %lu\n",
    12311187            token, (unsigned long)data.size());
    1232     NanoVis::sendDataToClient(cmd, data.bytes(), data.size());
     1188    nv::sendDataToClient(cmd, data.bytes(), data.size());
    12331189    return TCL_OK;
    12341190}
     
    12921248        sprintf(tmpFileName + length, "/image%d.ppm", i);
    12931249        unlink(tmpFileName);
    1294     }       
     1250    }
    12951251    tmpFileName[length] = '\0';
    12961252    rmdir(tmpFileName);
     
    13431299 *    associative array.
    13441300 */
    1345 int
    1346 FlowCmdInitProc(Tcl_Interp *interp)
    1347 {
    1348     Tcl_CreateObjCommand(interp, "flow", FlowCmdProc, NULL, NULL);
    1349     return TCL_OK;
    1350 }
     1301void
     1302FlowCmdInitProc(Tcl_Interp *interp, ClientData clientData)
     1303{
     1304    Tcl_CreateObjCommand(interp, "flow", FlowCmdProc, clientData, NULL);
     1305}
Note: See TracChangeset for help on using the changeset viewer.