566
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3 #include <string.h>
|
|
4 #include <fcntl.h>
|
|
5 #include <sys/types.h>
|
|
6 #include <sys/mman.h>
|
|
7 #include <sys/stat.h>
|
|
8 #include <unistd.h>
|
|
9 #include <arpa/inet.h>
|
|
10 #include "SceneGraphRoot.h"
|
|
11 #include "lindaapi.h"
|
|
12 #include "send_linda.h"
|
|
13
|
|
14 #define HOSTNAME "localhost"
|
|
15 #define PORT_NUM 10000
|
|
16 #define LISTEN_PORT 1
|
|
17 #define MULTI_NUM 10
|
588
|
18 #define SEND_DATA_SIZE 24 //sizeof(float) * 6;
|
|
19 void
|
|
20 send_position(SceneGraphPtr node)
|
|
21 {
|
|
22 char *data;
|
|
23 char send_data[SEND_DATA_SIZE]; // xyz[3] and angle[3]
|
|
24 int tapleid = node->id * 10 + 1;
|
|
25 int fd = *(int*)node->propertyptr;
|
|
26 int seq;
|
|
27 int i;
|
|
28
|
|
29 for (i = 0; i < 3; i ++) {
|
|
30 ((float *)send_data)[i] = htonl(node->xyz[i]);
|
|
31 ((float *)send_data)[i+3] = htonl(node->angle[i]);
|
|
32 }
|
|
33 seq = psx_in(fd, tapleid);
|
|
34 do {
|
|
35 psx_sync_n();
|
|
36 data = (char *)psx_reply(seq);
|
|
37 } while (data == NULL);
|
|
38 psx_out(fd, tapleid, (unsigned char *)send_data, SEND_DATA_SIZE);
|
|
39 psx_sync_n();
|
|
40 psx_free(data);
|
|
41 }
|
566
|
42
|
|
43 void
|
|
44 root_move(SceneGraphPtr node, int w, int h)
|
|
45 {
|
|
46 Pad *pad = sgroot->getController();
|
588
|
47 int flag = 0;
|
566
|
48 if (pad->right.isHold() || pad->left.isHold()) {
|
586
|
49 if (pad->right.isHold()) {
|
|
50 node->xyz[0] += 5.0f;
|
588
|
51 flag = 1;
|
586
|
52 } else if (pad->left.isHold()) {
|
|
53 node->xyz[0] -= 5.0f;
|
588
|
54 flag = 1;
|
586
|
55 }
|
566
|
56 }
|
|
57
|
|
58 if (pad->down.isHold() || pad->up.isHold() ) {
|
586
|
59 if (pad->down.isHold()) {
|
|
60 node->xyz[1] += 5.0f;
|
588
|
61 flag = 1;
|
586
|
62 } else if (pad->up.isHold()) {
|
|
63 node->xyz[1] -= 5.0f;
|
588
|
64 flag = 1;
|
586
|
65 }
|
566
|
66 }
|
|
67
|
588
|
68 /*
|
566
|
69 ここでキー入力を向こうに送る
|
586
|
70 */
|
588
|
71
|
|
72 if (flag) {
|
|
73 send_position(node);
|
|
74 }
|
566
|
75 }
|
|
76
|
|
77 void
|
|
78 root_collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree)
|
|
79 {
|
|
80 }
|
|
81
|
|
82 void
|
|
83 move(SceneGraphPtr node, int w, int h)
|
|
84 {
|
|
85 }
|
|
86
|
|
87 void
|
|
88 collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree)
|
|
89 {
|
|
90 }
|
|
91
|
|
92 void *
|
|
93 file_map(const char *filename, int *size) {
|
|
94 int fd;
|
|
95 void *addr;
|
|
96 struct stat sb;
|
|
97
|
|
98 if ((fd = open(filename, O_RDONLY)) == -1) {
|
586
|
99 fprintf(stderr, "Can't open %s\n", filename);
|
|
100 perror(NULL);
|
566
|
101 }
|
|
102 if (fstat(fd, &sb) == -1) {
|
586
|
103 fprintf(stderr, "Can't fstat %s\n", filename);
|
|
104 perror(NULL);
|
566
|
105 }
|
|
106 *size = sb.st_size;
|
|
107 addr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
108 if (addr == MAP_FAILED) {
|
586
|
109 perror("mmap error\n");
|
|
110 exit(EXIT_FAILURE);
|
566
|
111 }
|
|
112 close(fd);
|
|
113
|
|
114 return addr;
|
|
115 }
|
|
116
|
|
117
|
|
118
|
|
119 int get_serial_id(int fd) {
|
|
120 char *data;
|
|
121 int serial;
|
|
122 int seq;
|
|
123
|
|
124 seq = psx_in(fd, 65535);
|
587
|
125 do {
|
|
126 psx_sync_n();
|
|
127 data = (char *)psx_reply(seq);
|
|
128 } while (data == 0);
|
566
|
129 serial = atoi(data + LINDA_HEADER_SIZE);
|
|
130 psx_free(data);
|
|
131
|
|
132 return serial;
|
|
133 }
|
|
134
|
|
135
|
|
136 void
|
|
137 send_xml(int tspace, int xml_id, void *addr, int size) {
|
|
138 psx_out(tspace, xml_id, (unsigned char *)addr, size);
|
|
139 psx_sync_n();
|
|
140 }
|
|
141
|
|
142 /*
|
|
143 void
|
|
144 mainLoop(int tid, int write_id, int fd)
|
|
145 {
|
|
146 void *addr;
|
|
147 struct stat sb;
|
|
148
|
|
149 if (fstat(fd, &sb) == -1) {
|
|
150 perror("fstat");
|
|
151 exit(1);
|
|
152 }
|
|
153 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
154 if (addr==NULL) {
|
|
155 perror("mmap");
|
|
156 exit(1);
|
|
157 }
|
|
158
|
|
159 printf("file size=%d\n", sb.st_size);
|
|
160
|
|
161 psx_out(tid, write_id, addr, sb.st_size);
|
|
162 psx_sync_n();
|
|
163
|
|
164 return;
|
|
165 }
|
|
166 */
|
|
167
|
|
168 static char *xml;
|
577
|
169 static char *linda = HOSTNAME;
|
566
|
170
|
|
171 MainLoopPtr
|
586
|
172 send_linda::init(Viewer *sgr, int screen_w, int screen_h)
|
566
|
173 {
|
|
174 void *addr;
|
|
175 int size;
|
|
176 int tspace;
|
|
177 int serial;
|
|
178 int xml_id;
|
|
179
|
|
180 SceneGraphPtr sgp;
|
|
181 SceneGraphPtr root;
|
587
|
182
|
|
183 root = sgroot->createSceneGraph();
|
566
|
184 root->set_move_collision(root_move, root_collision);
|
586
|
185
|
|
186 addr = file_map(xml, &size);
|
587
|
187 sgp = sgroot->createSceneGraph();
|
|
188 sgroot->createFromXMLmemory(sgroot->tmanager, sgp, (char *)addr, size);
|
566
|
189 sgp->set_move_collision(move, collision);
|
|
190
|
|
191 root->addChild(sgp);
|
586
|
192
|
587
|
193 tspace = open_linda_java(linda, PORT_NUM);
|
588
|
194 root->propertyptr = sgroot->tmanager->allocate(sizeof(int));
|
|
195 root->property_size = sizeof(int);
|
|
196 *(int *)root->propertyptr = tspace;
|
|
197
|
566
|
198 serial = get_serial_id(tspace);
|
588
|
199 root->id = serial;
|
566
|
200 xml_id = serial * 10;
|
587
|
201 send_xml(tspace, xml_id, addr, size);
|
566
|
202
|
587
|
203 int client_id = htonl(serial);
|
|
204 printf("serial id = %d\n", client_id);
|
|
205 send_xml(tspace, LISTEN_PORT, (unsigned char *)&client_id, sizeof(int));
|
588
|
206
|
|
207 int pos_id = serial * 10 + 1;
|
|
208 char pos_data[SEND_DATA_SIZE];
|
|
209 for (int i = 0; i < 6; i++) {
|
|
210 ((float *)pos_data)[i] = htonl(0.0f);
|
|
211 }
|
|
212 send_xml(tspace, pos_id, (unsigned char *)pos_data, SEND_DATA_SIZE);
|
|
213
|
587
|
214 sgroot->setSceneData(root);
|
586
|
215 return sgr;
|
566
|
216 }
|
|
217
|
|
218 extern Application *
|
|
219 application() {
|
|
220 return new send_linda();
|
|
221 }
|
|
222
|
|
223
|
|
224 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n";
|
|
225
|
|
226 extern int init(TaskManager *manager, int argc, char *argv[]);
|
|
227 extern void task_initialize();
|
|
228 static void TMend(TaskManager *manager);
|
|
229
|
|
230 int
|
|
231 TMmain(TaskManager *manager, int argc, char *argv[])
|
|
232 {
|
|
233 task_initialize();
|
|
234 manager->set_TMend(TMend);
|
|
235
|
|
236 for(int i=0;i<argc;i++) {
|
588
|
237 if (strcmp(argv[i],"-xml") == 0 && i+1<=argc) {
|
|
238 xml = argv[i+1];
|
|
239 } else if (strcmp(argv[i],"-linda") == 0 && i+1<=argc) {
|
|
240 linda = argv[i+1];
|
|
241 }
|
566
|
242 }
|
|
243
|
577
|
244 if (xml==0) {
|
|
245 printf("-xml xml-file is required\n");
|
|
246 exit(0);
|
|
247 }
|
|
248
|
566
|
249 return init(manager, argc, argv);
|
|
250
|
|
251 }
|
|
252
|
|
253 void
|
|
254 TMend(TaskManager *manager)
|
|
255 {
|
|
256 printf("test_nogl end\n");
|
|
257 }
|
|
258
|
|
259 /* end */
|
|
260
|