1 | /****************************************************************************** |
---|
2 | * Program: JuxtaView for SAGE |
---|
3 | * Module: JuxtaSCUICommon.cpp - Part of JuxtaView's UI |
---|
4 | * Authors: Arun Rao, arao@evl.uic.edu, |
---|
5 | Ratko Jagodic, rjagodic@evl.uic.edu, |
---|
6 | Nicholas Schwarz, schwarz@evl.uic.edu, |
---|
7 | Luc Renambot, luc@evl.uic.edu, |
---|
8 | et al. |
---|
9 | Date: 30 September 2004 |
---|
10 | Modified: 9 September 2005 |
---|
11 | * |
---|
12 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
13 | * University of Illinois at Chicago |
---|
14 | * |
---|
15 | * All rights reserved. |
---|
16 | * |
---|
17 | * Redistribution and use in source and binary forms, with or without |
---|
18 | * modification, are permitted provided that the following conditions are met: |
---|
19 | * |
---|
20 | * * Redistributions of source code must retain the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer. |
---|
22 | * * Redistributions in binary form must reproduce the above |
---|
23 | * copyright notice, this list of conditions and the following disclaimer |
---|
24 | * in the documentation and/or other materials provided with the distribution. |
---|
25 | * * Neither the name of the University of Illinois at Chicago nor |
---|
26 | * the names of its contributors may be used to endorse or promote |
---|
27 | * products derived from this software without specific prior written permission. |
---|
28 | * |
---|
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
33 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
34 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
35 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
36 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
37 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
38 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
39 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
40 | * |
---|
41 | * Direct questions, comments etc to schwarz@evl.uic.edu or |
---|
42 | * http://www.evl.uic.edu/cavern/forum/ |
---|
43 | * |
---|
44 | *****************************************************************************/ |
---|
45 | |
---|
46 | |
---|
47 | #include <stdio.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include "JuxtaUIServer.h" |
---|
50 | #include <string.h> |
---|
51 | |
---|
52 | JuxtaUIServer::JuxtaUIServer() |
---|
53 | { |
---|
54 | server = new QUANTAnet_tcpServer_c(); |
---|
55 | client = NULL; |
---|
56 | recmsg = NULL; |
---|
57 | } |
---|
58 | |
---|
59 | JuxtaUIServer::~JuxtaUIServer() |
---|
60 | { |
---|
61 | if(client) |
---|
62 | client->close(); |
---|
63 | |
---|
64 | if(server) |
---|
65 | server->close(); |
---|
66 | |
---|
67 | if(client) |
---|
68 | delete client; |
---|
69 | |
---|
70 | if(server) |
---|
71 | delete server; |
---|
72 | |
---|
73 | if(recmsg) |
---|
74 | delete [] recmsg; |
---|
75 | } |
---|
76 | |
---|
77 | bool JuxtaUIServer::Init(char* configfilename) |
---|
78 | { |
---|
79 | FILE* fin = fopen(configfilename,"r"); |
---|
80 | if(!fin) { |
---|
81 | printf("return\n"); |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | int port; |
---|
87 | fscanf(fin,"%d\n",&port); |
---|
88 | |
---|
89 | fprintf(stderr, "!!!****************SERVER PORT 1: %d\n ", port); |
---|
90 | if( server->init(port) < 0) |
---|
91 | { |
---|
92 | printf("!!!******************SERVER PORT 2: %d\n ", port); |
---|
93 | server->close(); |
---|
94 | delete server; |
---|
95 | server = NULL; |
---|
96 | fclose(fin); |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | fprintf(stderr, "!!!****************SERVER PORT 3: %d\n ", port); |
---|
101 | fclose(fin); |
---|
102 | return true; |
---|
103 | } |
---|
104 | |
---|
105 | char* JuxtaUIServer::WaitForMessage() |
---|
106 | { |
---|
107 | if(recmsg == NULL) |
---|
108 | recmsg = new char[TRANSMISSION_SIZE]; |
---|
109 | memset(recmsg,0,TRANSMISSION_SIZE); |
---|
110 | static int size = TRANSMISSION_SIZE; |
---|
111 | |
---|
112 | if(client == NULL && server != NULL) |
---|
113 | { |
---|
114 | fprintf(stderr,"Waiting For Client To Connect.\n"); |
---|
115 | client = server->waitForNewConnection(); |
---|
116 | fprintf(stderr,"Ready For Messages.\n"); |
---|
117 | } |
---|
118 | |
---|
119 | if(client) |
---|
120 | { |
---|
121 | |
---|
122 | int re = client->read(recmsg,&size,QUANTAnet_tcpClient_c::NON_BLOCKING); |
---|
123 | |
---|
124 | // no data is read |
---|
125 | if (re == QUANTAnet_tcpClient_c::NON_BLOCKING_HAS_NO_DATA) { |
---|
126 | strcpy(recmsg, JVRESEND); |
---|
127 | return recmsg; |
---|
128 | } |
---|
129 | |
---|
130 | // data is read |
---|
131 | else { |
---|
132 | return recmsg; |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | client->read(recmsg,&size,QUANTAnet_tcpClient_c::NON_BLOCKING); |
---|
137 | printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-%s\n", recmsg); |
---|
138 | return recmsg; |
---|
139 | */ |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | return NULL; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | void JuxtaUIServer::SendNormalizedExtents(float x, float y, float w, float h) |
---|
148 | { |
---|
149 | int size; |
---|
150 | if(client) |
---|
151 | { |
---|
152 | fprintf(stderr,"Sending normalized coords and dims:\n%f, %f\n%f x %f\n",x,y,w,h); |
---|
153 | size = sizeof(float) * 4; |
---|
154 | float array[4]; |
---|
155 | array[0] = x; |
---|
156 | array[1] = y; |
---|
157 | array[2] = w; |
---|
158 | array[3] = h; |
---|
159 | client->write((const char*)array,&size,QUANTAnet_tcpClient_c::BLOCKING); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | void JuxtaUIServer::SendOverview(int w, int h,unsigned char* bmp) |
---|
165 | { |
---|
166 | int size; |
---|
167 | if(client) |
---|
168 | { |
---|
169 | fprintf(stderr,"Sending overview of size: %d x %d\n",w,h); |
---|
170 | size = sizeof(int) * 2; |
---|
171 | int array[2]; |
---|
172 | array[0] = w; |
---|
173 | array[1] = h; |
---|
174 | //send pixel dimensions first |
---|
175 | client->write((const char*)array,&size,QUANTAnet_tcpClient_c::BLOCKING); |
---|
176 | //send pixels next |
---|
177 | size = w * h * 3; |
---|
178 | client->write((const char*)bmp,&size,QUANTAnet_tcpClient_c::BLOCKING); |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | } |
---|
183 | |
---|
184 | void JuxtaUIServer::GetNormalizedFreeTranslation(float* x, float* y) |
---|
185 | { |
---|
186 | float array[2] = { 0.0 , 0.0 }; |
---|
187 | int size = sizeof(float) * 2; |
---|
188 | |
---|
189 | if( client ) |
---|
190 | { |
---|
191 | fprintf(stderr,"Reading normalized free translation from client!\n"); |
---|
192 | client->read((char*)array,&size,QUANTAnet_tcpClient_c::BLOCKING); |
---|
193 | |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | *x = array[0]; |
---|
198 | *y = array[1]; |
---|
199 | } |
---|
200 | |
---|