Mercurial > hg > Members > kono > Cerium
annotate Renderer/Test/send_linda.cc @ 758:e615e552efa5
change Test/create_task
author | hiroki |
---|---|
date | Sat, 30 Jan 2010 19:33:16 +0900 |
parents | b7376415fa5f |
children | 2432c7fe291c |
rev | line source |
---|---|
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> | |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
10 #include <rpc/types.h> |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
11 #include <rpc/xdr.h> |
566 | 12 #include "SceneGraphRoot.h" |
13 #include "lindaapi.h" | |
14 #include "send_linda.h" | |
15 | |
16 #define HOSTNAME "localhost" | |
17 #define PORT_NUM 10000 | |
18 #define LISTEN_PORT 1 | |
19 #define MULTI_NUM 10 | |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
20 #define SEND_DATA_SIZE sizeof(float) * 6 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
21 |
588 | 22 void |
23 send_position(SceneGraphPtr node) | |
24 { | |
25 char *data; | |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
26 if ((data = (char *)psx_reply(node->seq)) == NULL) { |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
27 //float send_data[6]; // xyz[3] and angle[3] |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
28 int tapleid = node->id * 10 + 1; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
29 int fd = *(int*)node->propertyptr; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
30 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
31 // XDRの準備 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
32 XDR xdrs; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
33 char send_data[SEND_DATA_SIZE]; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
34 xdrmem_create(&xdrs, send_data, SEND_DATA_SIZE, XDR_ENCODE); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
35 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
36 for (int i = 0; i < 3; i ++) { |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
37 xdr_float(&xdrs, &node->xyz[i]); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
38 } |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
39 for (int i = 0; i < 3; i ++) { |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
40 xdr_float(&xdrs, &node->angle[i]); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
41 } |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
42 node->seq = psx_in(fd, tapleid); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
43 psx_out(fd, tapleid, (unsigned char *)send_data, SEND_DATA_SIZE); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
44 psx_free(data); |
588 | 45 } |
46 } | |
566 | 47 |
48 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
49 root_move(SceneGraphPtr node, void *sgroot_, int w, int h) |
566 | 50 { |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
51 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; |
566 | 52 Pad *pad = sgroot->getController(); |
588 | 53 int flag = 0; |
566 | 54 if (pad->right.isHold() || pad->left.isHold()) { |
586 | 55 if (pad->right.isHold()) { |
56 node->xyz[0] += 5.0f; | |
588 | 57 flag = 1; |
586 | 58 } else if (pad->left.isHold()) { |
59 node->xyz[0] -= 5.0f; | |
588 | 60 flag = 1; |
586 | 61 } |
566 | 62 } |
63 | |
64 if (pad->down.isHold() || pad->up.isHold() ) { | |
586 | 65 if (pad->down.isHold()) { |
66 node->xyz[1] += 5.0f; | |
588 | 67 flag = 1; |
586 | 68 } else if (pad->up.isHold()) { |
69 node->xyz[1] -= 5.0f; | |
588 | 70 flag = 1; |
586 | 71 } |
566 | 72 } |
73 | |
588 | 74 /* |
566 | 75 ここでキー入力を向こうに送る |
586 | 76 */ |
588 | 77 if (flag) { |
78 send_position(node); | |
79 } | |
566 | 80 } |
81 | |
82 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
83 root_collision(SceneGraphPtr node, void *sgroot_, int w, int h, SceneGraphPtr tree) |
566 | 84 { |
85 } | |
86 | |
87 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
88 move(SceneGraphPtr node, void *sgroot_, int w, int h) |
566 | 89 { |
90 } | |
91 | |
92 void | |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
93 collision(SceneGraphPtr node, void *sgroot_, int w, int h, SceneGraphPtr tree) |
566 | 94 { |
95 } | |
96 | |
97 void * | |
98 file_map(const char *filename, int *size) { | |
99 int fd; | |
100 void *addr; | |
101 struct stat sb; | |
102 | |
103 if ((fd = open(filename, O_RDONLY)) == -1) { | |
586 | 104 fprintf(stderr, "Can't open %s\n", filename); |
105 perror(NULL); | |
566 | 106 } |
107 if (fstat(fd, &sb) == -1) { | |
586 | 108 fprintf(stderr, "Can't fstat %s\n", filename); |
109 perror(NULL); | |
566 | 110 } |
111 *size = sb.st_size; | |
112 addr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); | |
113 if (addr == MAP_FAILED) { | |
586 | 114 perror("mmap error\n"); |
115 exit(EXIT_FAILURE); | |
566 | 116 } |
117 close(fd); | |
118 | |
119 return addr; | |
120 } | |
121 | |
122 | |
123 | |
124 int get_serial_id(int fd) { | |
125 char *data; | |
126 int serial; | |
127 int seq; | |
128 | |
129 seq = psx_in(fd, 65535); | |
587 | 130 do { |
131 psx_sync_n(); | |
132 data = (char *)psx_reply(seq); | |
133 } while (data == 0); | |
566 | 134 serial = atoi(data + LINDA_HEADER_SIZE); |
135 psx_free(data); | |
593
6f741ab60749
sending and moving of multi xml work (linda)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
592
diff
changeset
|
136 printf("serial id = %d\n", serial); |
566 | 137 return serial; |
138 } | |
139 | |
140 | |
141 void | |
142 send_xml(int tspace, int xml_id, void *addr, int size) { | |
143 psx_out(tspace, xml_id, (unsigned char *)addr, size); | |
144 psx_sync_n(); | |
145 } | |
146 | |
147 static char *xml; | |
655 | 148 static const char *linda = HOSTNAME; |
566 | 149 |
150 MainLoopPtr | |
586 | 151 send_linda::init(Viewer *sgr, int screen_w, int screen_h) |
566 | 152 { |
153 void *addr; | |
154 int size; | |
155 int tspace; | |
156 int serial; | |
157 int xml_id; | |
158 | |
587 | 159 |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
160 // ここら辺長ったるいから、関数で分けるべきか... |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
161 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
162 // root オブジェクト作成 |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
163 SceneGraphPtr root = sgr->createSceneGraph(); |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
164 // root_moveはコントローラーの入力で動き、座標をLinda Serverにout |
566 | 165 root->set_move_collision(root_move, root_collision); |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
166 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
167 // XMLをメモリにmapして、オブジェクト生成 |
586 | 168 addr = file_map(xml, &size); |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
169 SceneGraphPtr sgp = sgr->createSceneGraph(); |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
170 sgr->createFromXMLmemory(sgp, (char *)addr, size); |
566 | 171 sgp->set_move_collision(move, collision); |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
172 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
173 // rootに接続 |
566 | 174 root->addChild(sgp); |
586 | 175 |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
176 // Linda Serverに接続 |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
177 tspace = open_linda_java(linda, PORT_NUM); |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
178 // rootにLindaのfdを持たせる |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
179 root->propertyptr = (void*)malloc(sizeof(int)); |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
180 int *p = (int*)root->propertyptr; |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
181 root->property_size = sizeof(int); |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
182 *p = tspace; |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
183 |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
184 // このclientのserial_idを取得 |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
185 serial = get_serial_id(tspace); |
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
186 root->id = serial; |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
187 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
188 // ここから先の処理は、裏で何か動かせないかを考える |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
189 // とりあえず、関数に分けようか |
588 | 190 |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
191 // serial_idを十倍したところにXMLを送信 |
566 | 192 xml_id = serial * 10; |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
193 send_xml(tspace, xml_id, addr, size); // ここのpsx_sync_n()は仕方ない |
566 | 194 |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
195 // XMLの送信が終了してから、serial_idをLindaに送信する |
587 | 196 int client_id = htonl(serial); |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
197 psx_out(tspace, LISTEN_PORT, (unsigned char *)&client_id, sizeof(int)); |
592
56e63fc45105
send position work!!!(linda)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
591
diff
changeset
|
198 |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
199 // serial_idを十倍して1足したところに座標データを送る |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
200 // 初期化のout() |
588 | 201 int pos_id = serial * 10 + 1; |
610
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
202 char pos_data[SEND_DATA_SIZE] = {0}; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
203 // float pos_data[6]; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
204 // for (int i = 0; i < 6; i++) { |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
205 // pos_data[i] = 0.0f; |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
206 // } |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
207 psx_out(tspace, pos_id, (unsigned char *)pos_data, SEND_DATA_SIZE); |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
208 |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
209 // 初期化のin() |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
210 // send_position()でinの終了を確認する分岐が最初にあるため |
529188ae604d
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
211 root->seq = psx_in(tspace, pos_id); |
671
f42b303044f7
fix several Renderer/Test with (void*)sgroot.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
655
diff
changeset
|
212 sgr->setSceneData(root); |
588 | 213 |
586 | 214 return sgr; |
566 | 215 } |
216 | |
217 extern Application * | |
218 application() { | |
219 return new send_linda(); | |
220 } | |
221 | |
222 | |
223 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; | |
224 | |
225 extern int init(TaskManager *manager, int argc, char *argv[]); | |
226 extern void task_initialize(); | |
227 static void TMend(TaskManager *manager); | |
228 | |
229 int | |
230 TMmain(TaskManager *manager, int argc, char *argv[]) | |
231 { | |
232 task_initialize(); | |
233 manager->set_TMend(TMend); | |
234 | |
235 for(int i=0;i<argc;i++) { | |
588 | 236 if (strcmp(argv[i],"-xml") == 0 && i+1<=argc) { |
237 xml = argv[i+1]; | |
238 } else if (strcmp(argv[i],"-linda") == 0 && i+1<=argc) { | |
239 linda = argv[i+1]; | |
240 } | |
566 | 241 } |
242 | |
577 | 243 if (xml==0) { |
244 printf("-xml xml-file is required\n"); | |
245 exit(0); | |
246 } | |
247 | |
566 | 248 return init(manager, argc, argv); |
249 | |
250 } | |
251 | |
252 void | |
253 TMend(TaskManager *manager) | |
254 { | |
255 printf("test_nogl end\n"); | |
256 } | |
257 | |
258 /* end */ | |
259 |