1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * EventPlayer.cpp: client that plays back events from file |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2006 Purdue Research Foundation |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | |
---|
17 | #include <stdlib.h> |
---|
18 | #include <unistd.h> |
---|
19 | #include <sstream> |
---|
20 | #include <string.h> |
---|
21 | #include <time.h> |
---|
22 | #include <sys/time.h> |
---|
23 | #include <iostream> |
---|
24 | #include <assert.h> |
---|
25 | |
---|
26 | #include "clientlib.h" |
---|
27 | #include "Event.h" |
---|
28 | |
---|
29 | Event* event[300]; |
---|
30 | int cur_event = 0; |
---|
31 | double interval_sum = 0; |
---|
32 | |
---|
33 | int width = 512; |
---|
34 | int height = 512; |
---|
35 | char screen_buffer[512*512*3]; |
---|
36 | int screen_size = width * height; |
---|
37 | |
---|
38 | int socket_fd; //socekt file descriptor |
---|
39 | |
---|
40 | /* |
---|
41 | void display() |
---|
42 | { |
---|
43 | //paste to screen |
---|
44 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
45 | glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, screen_buffer); |
---|
46 | glFlush(); |
---|
47 | glutSwapBuffers(); |
---|
48 | return; |
---|
49 | } |
---|
50 | */ |
---|
51 | |
---|
52 | void key(unsigned char key, int x, int y) |
---|
53 | { |
---|
54 | switch (key) { |
---|
55 | case 'q': |
---|
56 | exit(0); |
---|
57 | break; |
---|
58 | default: |
---|
59 | return; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /* |
---|
64 | *Client communicates with the server |
---|
65 | */ |
---|
66 | void idle(void) |
---|
67 | { |
---|
68 | //send requests |
---|
69 | |
---|
70 | if (cur_event >= (int)(sizeof(event)/sizeof(event[0]))) { |
---|
71 | float ave = interval_sum / (cur_event-1); |
---|
72 | float fps = 1/ave; |
---|
73 | TRACE("Average frame time = %.6f\n", ave); |
---|
74 | TRACE("Frames per second = %f\n", fps); |
---|
75 | exit(0); |
---|
76 | } |
---|
77 | |
---|
78 | Event *cur = event[cur_event]; |
---|
79 | std::stringstream msgstream; |
---|
80 | std::string msg; |
---|
81 | |
---|
82 | switch (cur->type){ |
---|
83 | case 0: //rotate |
---|
84 | msgstream << "camera " << cur->parameter[0] << " " |
---|
85 | << cur->parameter[1] << " " |
---|
86 | << cur->parameter[2] << " " << endl; |
---|
87 | break; |
---|
88 | case 1: //move |
---|
89 | msgstream << "move " << cur->parameter[0] << " " |
---|
90 | << cur->parameter[1] << " " |
---|
91 | << cur->parameter[2] << " " << endl; |
---|
92 | break; |
---|
93 | case 2: //other |
---|
94 | msgstream << "refresh " << cur->parameter[0] << " " |
---|
95 | << cur->parameter[1] << " " |
---|
96 | << cur->parameter[2] << " " << endl; |
---|
97 | break; |
---|
98 | default: |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | msg = msgstream.str(); |
---|
103 | |
---|
104 | //sleep a little |
---|
105 | struct timespec ts; |
---|
106 | ts.tv_sec = 0; |
---|
107 | ts.tv_nsec = cur->msec*1000000000; |
---|
108 | nanosleep(&ts, 0); |
---|
109 | |
---|
110 | //start timer |
---|
111 | struct timeval clock; |
---|
112 | gettimeofday(&clock, NULL); |
---|
113 | double start_time = clock.tv_sec + clock.tv_usec/1000000.0; |
---|
114 | |
---|
115 | //send msg |
---|
116 | //TRACE("Writing message %04d to server: '%s'\n", cur_event, msg.c_str()); |
---|
117 | int status = write(socket_fd, msg.c_str(), strlen(msg.c_str())); |
---|
118 | if (status <= 0) { |
---|
119 | perror("socket write"); |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | #if DO_RLE |
---|
124 | int sizes[2]; |
---|
125 | status = read(socket_fd, &sizes, sizeof(sizes)); |
---|
126 | TRACE("Reading %d,%d bytes\n", sizes[0], sizes[1]); |
---|
127 | int len = sizes[0] + sizes[1]; |
---|
128 | #else |
---|
129 | int len = width * height * 3; |
---|
130 | #endif |
---|
131 | |
---|
132 | //receive screen update |
---|
133 | int remain = len; |
---|
134 | char* ptr = screen_buffer; |
---|
135 | while (remain > 0) { |
---|
136 | status = read(socket_fd, ptr, remain); |
---|
137 | if (status <= 0) { |
---|
138 | perror("socket read"); |
---|
139 | return; |
---|
140 | } else { |
---|
141 | remain -= status; |
---|
142 | ptr += status; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | //TRACE("Read message to server.\n"); |
---|
147 | |
---|
148 | //end timer |
---|
149 | gettimeofday(&clock, NULL); |
---|
150 | double end_time = clock.tv_sec + clock.tv_usec/1000000.0; |
---|
151 | |
---|
152 | double time_interval = end_time - start_time; |
---|
153 | |
---|
154 | if (cur_event != 0) { |
---|
155 | interval_sum += time_interval; |
---|
156 | } |
---|
157 | cur_event++; |
---|
158 | |
---|
159 | TRACE("% 6d %.6f\n", len, time_interval); |
---|
160 | } |
---|
161 | |
---|
162 | int init_client(char* host, char* port, char* file){ |
---|
163 | |
---|
164 | //load the event file |
---|
165 | FILE* fd = fopen(file, "r"); |
---|
166 | |
---|
167 | //load events |
---|
168 | for (int i = 0; i < (int)(sizeof(event)/sizeof(event[0])); i++) { |
---|
169 | int type; |
---|
170 | float param[3]; |
---|
171 | double interval; |
---|
172 | fscanf(fd, "%d %f %f %f %lf\n", &type, param, param+1, param+2, &interval); |
---|
173 | event[i] = new Event(type, param, interval); |
---|
174 | TRACE("%d %f %f %f\n", type, param[0], param[1], param[2]); |
---|
175 | } |
---|
176 | fclose(fd); |
---|
177 | |
---|
178 | socket_fd = connect_renderer(host, atoi(port), 100); |
---|
179 | if (socket_fd == -1) { |
---|
180 | ERROR("Could not connect to a server.\n"); |
---|
181 | return 1; |
---|
182 | } |
---|
183 | |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | void |
---|
188 | help(const char *argv0) |
---|
189 | { |
---|
190 | TRACE("Syntax: %s addr port eventfile\n", argv0); |
---|
191 | exit(1); |
---|
192 | } |
---|
193 | |
---|
194 | /* Program entry point */ |
---|
195 | int |
---|
196 | main(int argc, char *argv[]) |
---|
197 | { |
---|
198 | //parameters: host and port and event file |
---|
199 | if (argc != 4) |
---|
200 | help(argv[0]); |
---|
201 | |
---|
202 | assert(init_client(argv[1], argv[2], argv[3])==0); |
---|
203 | |
---|
204 | /* |
---|
205 | glutInit(&argc, argv); |
---|
206 | glutInitWindowSize(width,height); |
---|
207 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
---|
208 | |
---|
209 | glutCreateWindow("Client"); |
---|
210 | |
---|
211 | glutDisplayFunc(display); |
---|
212 | glutKeyboardFunc(key); |
---|
213 | glutIdleFunc(idle); |
---|
214 | |
---|
215 | glClearColor(0.,0.,0.,0.); |
---|
216 | glutMainLoop(); |
---|
217 | */ |
---|
218 | while (1) |
---|
219 | idle(); |
---|
220 | |
---|
221 | return 0; |
---|
222 | } |
---|