source: trunk/src/testing/app/volvis/vTF.cpp @ 4

Revision 4, 6.4 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/********************************************************************************
2 * Volatile - Volume Visualization Software for SAGE
3 * Copyright (C) 2004 Electronic Visualization Laboratory,
4 * University of Illinois at Chicago
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above
14 *    copyright notice, this list of conditions and the following disclaimer
15 *    in the documentation and/or other materials provided with the distribution.
16 *  * Neither the name of the University of Illinois at Chicago nor
17 *    the names of its contributors may be used to endorse or promote
18 *    products derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Direct questions, comments etc about Volatile to www.evl.uic.edu/cavern/forum
33 *********************************************************************************/
34
35#ifdef V_NETTF
36#include "vTF.h"
37#include "vUI.h"
38#include "global.h"
39#include <string.h>
40
41//send the UI events to the network.
42//Checks if there is a new tcp client that has connected in
43//Add that to the image list
44void* netThreadCB(void* data) {
45        vTF* tf = (vTF*)data;
46        while(1) {
47                tf->process();
48                QUANTAusleep(100);
49        }
50}
51
52vTF::vTF()
53{
54        tcpServer = 0;
55        tcpClient = 0;
56}
57
58vTF::~vTF() {
59        tcpClient->close();
60        tcpServer->close();
61}
62
63void vTF::init()
64{
65        //quanta stuff
66//      QUANTAinit();
67        //create a TCP server that gets the image data
68        tcpServer= new QUANTAnet_tcpServer_c;
69        if (tcpServer->init(7352) == 0)    {
70                exit(1);
71        }
72        QUANTAts_thread_c *networkListenerThread = new QUANTAts_thread_c;
73        networkListenerThread->create(netThreadCB, (void*) this);
74}
75
76void vTF::checkForConnections() {
77        QUANTAnet_tcpClient_c* newClient = tcpServer->checkForNewConnections(0);
78        if (newClient) { //new client is connecting
79                if (tcpClient) { //if an old client already exists
80                        fprintf(stderr,"Deleting Old client\n");
81                        tcpClient->close(); //close the old client
82                        delete tcpClient; //deallocate it
83                }
84                tcpClient = newClient;
85                tcpClient->getRemoteIP(clientIP);
86                fprintf(stderr,"New Client %s connected at port %u \n", clientIP, tcpClient->getSelfPort());
87        }
88}
89#define HISTSIZE 256*256
90void vTF::sendHistogram() {
91        unsigned char histogram[HISTSIZE];
92        int haveHist = global.volume->hist2D(histogram);
93        int size;
94
95        //fprintf(stderr,"Going to send update histogram %d\n",haveHist);
96        //writing whether we need to update the histogram
97        size = sizeof(int);
98        tcpClient->setSockOptions(QUANTAnet_tcpClient_c::WRITE_BUFFER_SIZE, size);
99        int status = tcpClient->write((char*)&haveHist, &size, QUANTAnet_tcpClient_c::BLOCKING);
100        //fprintf(stderr,"Sinished send update histogram %d\n",haveHist);
101        if (status != QUANTAnet_tcpClient_c::OK)
102                tcpClient->showStatus(status, size);
103#ifdef SHOW_STATS
104        tcpClient->showStats("After sending update histogram"," ");
105#endif
106        if (haveHist) { //if we have to update the hist, send the histogram
107                size = HISTSIZE*sizeof(unsigned char);
108                tcpClient->setSockOptions(QUANTAnet_tcpClient_c::WRITE_BUFFER_SIZE, size);
109                int status = tcpClient->write((char*)histogram, &size, QUANTAnet_tcpClient_c::BLOCKING);
110                if (status != QUANTAnet_tcpClient_c::OK)
111                        tcpClient->showStatus(status, size);
112                else {
113                //      fprintf(stderr,"vTF::sendHistogram: have Hist and finished sending\n");
114                }
115#ifdef SHOW_STATS
116                tcpClient->showStats("After sending histogram"," ");
117#endif
118        }
119
120}
121
122void vTF::process() {
123        checkForConnections();
124        //now read from client
125        if (tcpClient) {
126                //fprintf(stderr,"Going to send hist and read from client\n");
127                //send histogram
128                sendHistogram();
129                int haveLUT;
130                //read if there is LUT
131                int dataSize = sizeof(int);
132                tcpClient->setSockOptions(QUANTAnet_tcpClient_c::READ_BUFFER_SIZE, dataSize);
133                int status = tcpClient->read((char*)&haveLUT, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
134                if (status == QUANTAnet_tcpClient_c::OK)  {
135                        //fprintf(stderr,"Reading Have LUT %d \n",haveLUT);
136                }
137                else if (status != QUANTAnet_tcpClient_c::NON_BLOCKING_HAS_NO_DATA) {
138                        fprintf(stderr,"Error while Reading Have LUT \n");
139                        tcpClient->showStatus(status, dataSize);
140                        //conection close just close the client
141                        tcpClient->close();
142                        delete tcpClient;
143                        tcpClient = 0;
144                }
145#ifdef SHOW_STATS
146                tcpClient->showStats("After receive"," ");
147#endif
148                if (haveLUT) {
149                //fprintf(stderr,"New LUT:Trying to read\n");
150                //set buffer size to the network data size
151                int dataSize = 256*4*sizeof(char)+sizeof(int);
152                char* netBuffer= new char[dataSize];
153                tcpClient->setSockOptions(QUANTAnet_tcpClient_c::READ_BUFFER_SIZE, dataSize);
154                int status = tcpClient->read(netBuffer, &dataSize, QUANTAnet_tcpClient_c::BLOCKING);
155                //fprintf(stderr,"Reading LUT %d bytes\n",dataSize);
156                if (status == QUANTAnet_tcpClient_c::OK)  {
157                        setLUT(dataSize,(unsigned char*)netBuffer);
158                        //fprintf(stderr,"Got LUT %d bytes\n",dataSize);
159                }
160                else if (status != QUANTAnet_tcpClient_c::NON_BLOCKING_HAS_NO_DATA) {
161                        fprintf(stderr,"Connection close with client %s\n", clientIP);
162                        tcpClient->showStatus(status, dataSize);
163                        //conection close just close the client
164                        tcpClient->close();
165                        delete tcpClient;
166                        tcpClient = 0;
167                }
168#ifdef SHOW_STATS
169                tcpClient->showStats("After receive"," ");
170#endif
171                delete [] netBuffer;
172                }
173                else {
174                //      fprintf(stderr,"No LUT received\n");
175                }
176
177        }
178
179}
180
181#endif
Note: See TracBrowser for help on using the repository browser.