Mercurial > hg > Members > e085722 > Cerium
comparison Renderer/Test/dynamic_create.cc @ 0:04e28d8d3c6f
first commit
author | Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Nov 2010 01:23:25 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:04e28d8d3c6f |
---|---|
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 <rpc/types.h> | |
11 #include <rpc/xdr.h> | |
12 #include "SceneGraphRoot.h" | |
13 #include "lindaapi.h" | |
14 #include "dynamic_create.h" | |
15 | |
16 #define PORT 10000 | |
17 | |
18 #define SERIAL_REGIST_TUPLE_NO 1 | |
19 | |
20 #define RECV_DATA_SIZE sizeof(float) * 6 | |
21 | |
22 typedef struct client_ { | |
23 int id; | |
24 SceneGraphPtr sgp; | |
25 struct client_ *next; | |
26 } client_t; | |
27 | |
28 typedef struct { | |
29 int tid; | |
30 int sid; | |
31 int read_id; | |
32 SceneGraphPtr node; | |
33 SceneGraphRoot *sgroot; | |
34 TaskManager *manager; | |
35 client_t *clist; | |
36 } callback_arg; | |
37 | |
38 | |
39 void | |
40 client_list_init(TaskManager* manager, client_t *clist) | |
41 { | |
42 clist->id = -1; | |
43 clist->next = clist; | |
44 } | |
45 | |
46 void | |
47 client_list_update(TaskManager *manager, client_t *clist, int id, SceneGraphPtr sgp) | |
48 { | |
49 } | |
50 | |
51 void | |
52 client_list_delete(TaskManager *manager, client_t *clist, int id) | |
53 { | |
54 client_t *c, *prev; | |
55 for (c = clist->next, prev = clist; c->next != clist; c = c->next) { | |
56 if (c->id == id) { | |
57 prev->next = c->next; | |
58 return; | |
59 } | |
60 prev = clist; | |
61 } | |
62 if (c->id == id) { | |
63 prev->next = c->next; | |
64 return; | |
65 } | |
66 } | |
67 | |
68 | |
69 static void | |
70 earth_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, | |
71 SceneGraphPtr tree) | |
72 { | |
73 } | |
74 | |
75 static void | |
76 moon_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, | |
77 SceneGraphPtr tree) | |
78 { | |
79 } | |
80 | |
81 static void | |
82 set_position(SceneGraphPtr node, unsigned char *reply) { | |
83 char *data = (char *)(reply + LINDA_HEADER_SIZE); | |
84 // XDRの準備 | |
85 XDR xdrs; | |
86 xdrmem_create(&xdrs, data, RECV_DATA_SIZE, XDR_DECODE); | |
87 for (int i = 0; i < 3; i++) { | |
88 xdr_float(&xdrs, &node->xyz[i]); | |
89 } | |
90 } | |
91 | |
92 static void | |
93 moon_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
94 { | |
95 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; | |
96 // LindaServerから座標データを取得してオブジェクトに反映させる。 | |
97 | |
98 if (!node->resend_flag || node->seq_rd != node->seq) { | |
99 unsigned char *reply_rd = psx_reply(node->seq_rd); | |
100 if (reply_rd != NULL) { | |
101 set_position(node, reply_rd); | |
102 free(reply_rd); | |
103 return; | |
104 } | |
105 } | |
106 unsigned char *reply = psx_reply(node->seq); | |
107 if (reply != NULL) { | |
108 set_position(node, reply); | |
109 free(reply); | |
110 node->seq = psx_wait_rd(sgroot->tid, node->id * 10 + 1); | |
111 node->resend_flag = true; | |
112 } else if (node->resend_flag) { | |
113 node->seq_rd = psx_rd(sgroot->tid, node->id * 10 + 1); | |
114 node->resend_flag = false; | |
115 } | |
116 } | |
117 | |
118 static void | |
119 earth_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
120 { | |
121 // psx_sync_n(); in viewer::MainLoop | |
122 } | |
123 | |
124 SceneGraphPtr | |
125 create_sg(TaskManager *manager, SceneGraphRoot *sgroot, SceneGraphPtr parent, unsigned char *data, | |
126 int len, int serial_id) | |
127 { | |
128 SceneGraphPtr child = sgroot->createSceneGraph(); | |
129 parent->addChild(child); | |
130 // 読み込んだオブジェクトは、すべて、child の child になる。 | |
131 sgroot->createFromXMLmemory(sgroot->tmanager, child, (char *)data, len); | |
132 child->set_move_collision(moon_move, moon_collision); | |
133 child->id = serial_id; | |
134 child->seq = psx_wait_rd(sgroot->tid, serial_id * 10 + 1); | |
135 child->seq_rd = psx_rd(sgroot->tid, serial_id * 10 + 1); | |
136 child->resend_flag = false; | |
137 return child; | |
138 } | |
139 | |
140 | |
141 static void | |
142 callback_get_xml(unsigned char *xml_tuple, void *arg) { | |
143 int xml_len = psx_get_datalength(xml_tuple); | |
144 callback_arg *carg = (callback_arg *)arg; | |
145 unsigned char *xml_data = xml_tuple + LINDA_HEADER_SIZE; | |
146 SceneGraphPtr sgp; | |
147 // ここで create | |
148 sgp = create_sg(carg->manager, carg->sgroot, carg->node, xml_data, xml_len, carg->sid); | |
149 printf("%s size %d loaded\n", sgp->children->name, xml_len); | |
150 client_list_update(carg->manager, carg->clist, carg->sid, sgp); | |
151 free(arg); | |
152 free(xml_tuple); | |
153 } | |
154 | |
155 static void | |
156 callbacker(unsigned char *taple, void *arg) { | |
157 int serial_id, xml_id; | |
158 | |
159 unsigned char *data; | |
160 callback_arg *carg = (callback_arg *)arg; | |
161 | |
162 // 最初の4byteデータは使わない | |
163 data = taple + LINDA_HEADER_SIZE; | |
164 // clientのSerialIDを取得 | |
165 serial_id = ntohl(*(int *)data); | |
166 printf("serial id = %d\n", serial_id); // タプルを解放 | |
167 | |
168 // xml fileを取得する もうすでにxml fileが送信済みである事を期待 | |
169 // つまり、送信者がserial_idを送る前にxml fileを送信していなくてはならない | |
170 xml_id = serial_id * 10; | |
171 callback_arg *copy_arg = (callback_arg *)carg->manager->allocate(sizeof(callback_arg)); | |
172 *copy_arg = *carg; | |
173 copy_arg->sid = serial_id; | |
174 psx_callback_in(carg->tid, xml_id, callback_get_xml, (void *)copy_arg); | |
175 | |
176 /* dataは'\0'で終わっている事を期待 (writerで保証する) */ | |
177 free(taple); | |
178 | |
179 // arg は使い回すらしい。 | |
180 psx_callback_in(carg->tid, carg->read_id, callbacker, arg); | |
181 } | |
182 | |
183 static const char *linda = "localhost"; | |
184 | |
185 static void | |
186 linda_init(TaskManager *manager, SceneGraphRoot *sgroot, client_t *clist, SceneGraphPtr node) | |
187 { | |
188 init_linda(); | |
189 callback_arg *carg = (callback_arg *)manager->allocate(sizeof(callback_arg)); | |
190 | |
191 carg->tid = open_linda_java(linda, PORT); | |
192 carg->sgroot = sgroot; | |
193 carg->sgroot->tid = carg->tid; | |
194 carg->read_id = SERIAL_REGIST_TUPLE_NO; | |
195 carg->node = node; | |
196 carg->manager = manager; | |
197 carg->clist = clist; | |
198 psx_callback_in(carg->tid, carg->read_id, callbacker, carg); | |
199 } | |
200 | |
201 MainLoopPtr | |
202 dynamic_create::init(Viewer *sgroot, int screen_w, int screen_h) | |
203 { | |
204 client_t *clist; | |
205 clist = (client_t *)sgroot->manager->allocate(sizeof(client_t)); | |
206 | |
207 client_list_init(sgroot->manager, clist); | |
208 | |
209 SceneGraphPtr parent; | |
210 parent = sgroot->createSceneGraph(); | |
211 parent->set_move_collision(earth_move, earth_collision); | |
212 linda_init(sgroot->manager, sgroot->sgroot, clist, parent); | |
213 | |
214 // SceneGraphRoot に、使用する SceneGraph を設定する | |
215 // このとき、ユーザーが記述した SceneGraph の root を渡す。 | |
216 sgroot->setSceneData(parent); | |
217 return sgroot; | |
218 } | |
219 | |
220 extern Application * | |
221 application() { | |
222 return new dynamic_create(); | |
223 } | |
224 | |
225 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; | |
226 | |
227 extern int init(TaskManager *manager, int argc, char *argv[]); | |
228 extern void task_initialize(); | |
229 static void TMend(TaskManager *manager); | |
230 | |
231 int | |
232 TMmain(TaskManager *manager, int argc, char *argv[]) | |
233 { | |
234 task_initialize(); | |
235 manager->set_TMend(TMend); | |
236 | |
237 for(int i=0;i<argc;i++) { | |
238 if (strcmp(argv[i],"-linda") == 0 && i+1<=argc) { | |
239 linda = argv[i+1]; | |
240 } | |
241 } | |
242 | |
243 return init(manager, argc, argv); | |
244 } | |
245 | |
246 void | |
247 TMend(TaskManager *manager) | |
248 { | |
249 printf("test_nogl end\n"); | |
250 } | |
251 | |
252 /* end */ |