Mercurial > hg > Game > Cerium
annotate Renderer/Test/send_linda.cc @ 610:d38d9896dfa4 draft
change htonl to xdr_float (checked working on mac)
author | kazz@kazzone.st.ie.u-ryukyu.ac.jp |
---|---|
date | Sun, 08 Nov 2009 03:30:06 +0900 |
parents | 50c877027e0f |
children | aefb5ee6fcd0 |
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
d38d9896dfa4
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> |
d38d9896dfa4
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
d38d9896dfa4
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 |
d38d9896dfa4
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
d38d9896dfa4
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) { |
d38d9896dfa4
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] |
d38d9896dfa4
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; |
d38d9896dfa4
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; |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
30 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
31 // XDRの準備 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
32 XDR xdrs; |
d38d9896dfa4
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]; |
d38d9896dfa4
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); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
35 |
d38d9896dfa4
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 ++) { |
d38d9896dfa4
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]); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
38 } |
d38d9896dfa4
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 ++) { |
d38d9896dfa4
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]); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
41 } |
d38d9896dfa4
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); |
d38d9896dfa4
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); |
d38d9896dfa4
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 | |
49 root_move(SceneGraphPtr node, int w, int h) | |
50 { | |
51 Pad *pad = sgroot->getController(); | |
588 | 52 int flag = 0; |
566 | 53 if (pad->right.isHold() || pad->left.isHold()) { |
586 | 54 if (pad->right.isHold()) { |
55 node->xyz[0] += 5.0f; | |
588 | 56 flag = 1; |
586 | 57 } else if (pad->left.isHold()) { |
58 node->xyz[0] -= 5.0f; | |
588 | 59 flag = 1; |
586 | 60 } |
566 | 61 } |
62 | |
63 if (pad->down.isHold() || pad->up.isHold() ) { | |
586 | 64 if (pad->down.isHold()) { |
65 node->xyz[1] += 5.0f; | |
588 | 66 flag = 1; |
586 | 67 } else if (pad->up.isHold()) { |
68 node->xyz[1] -= 5.0f; | |
588 | 69 flag = 1; |
586 | 70 } |
566 | 71 } |
72 | |
588 | 73 /* |
566 | 74 ここでキー入力を向こうに送る |
586 | 75 */ |
588 | 76 if (flag) { |
77 send_position(node); | |
78 } | |
566 | 79 } |
80 | |
81 void | |
82 root_collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree) | |
83 { | |
84 } | |
85 | |
86 void | |
87 move(SceneGraphPtr node, int w, int h) | |
88 { | |
89 } | |
90 | |
91 void | |
92 collision(SceneGraphPtr node, int w, int h, SceneGraphPtr tree) | |
93 { | |
94 } | |
95 | |
96 void * | |
97 file_map(const char *filename, int *size) { | |
98 int fd; | |
99 void *addr; | |
100 struct stat sb; | |
101 | |
102 if ((fd = open(filename, O_RDONLY)) == -1) { | |
586 | 103 fprintf(stderr, "Can't open %s\n", filename); |
104 perror(NULL); | |
566 | 105 } |
106 if (fstat(fd, &sb) == -1) { | |
586 | 107 fprintf(stderr, "Can't fstat %s\n", filename); |
108 perror(NULL); | |
566 | 109 } |
110 *size = sb.st_size; | |
111 addr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); | |
112 if (addr == MAP_FAILED) { | |
586 | 113 perror("mmap error\n"); |
114 exit(EXIT_FAILURE); | |
566 | 115 } |
116 close(fd); | |
117 | |
118 return addr; | |
119 } | |
120 | |
121 | |
122 | |
123 int get_serial_id(int fd) { | |
124 char *data; | |
125 int serial; | |
126 int seq; | |
127 | |
128 seq = psx_in(fd, 65535); | |
587 | 129 do { |
130 psx_sync_n(); | |
131 data = (char *)psx_reply(seq); | |
132 } while (data == 0); | |
566 | 133 serial = atoi(data + LINDA_HEADER_SIZE); |
134 psx_free(data); | |
593
75c78f8fba24
sending and moving of multi xml work (linda)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
592
diff
changeset
|
135 printf("serial id = %d\n", serial); |
566 | 136 return serial; |
137 } | |
138 | |
139 | |
140 void | |
141 send_xml(int tspace, int xml_id, void *addr, int size) { | |
142 psx_out(tspace, xml_id, (unsigned char *)addr, size); | |
143 psx_sync_n(); | |
144 } | |
145 | |
146 static char *xml; | |
577 | 147 static char *linda = HOSTNAME; |
566 | 148 |
149 MainLoopPtr | |
586 | 150 send_linda::init(Viewer *sgr, int screen_w, int screen_h) |
566 | 151 { |
152 void *addr; | |
153 int size; | |
154 int tspace; | |
155 int serial; | |
156 int xml_id; | |
157 | |
158 SceneGraphPtr sgp; | |
159 SceneGraphPtr root; | |
587 | 160 |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
161 // ここら辺長ったるいから、関数で分けるべきか... |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
162 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
163 // root オブジェクト作成 |
587 | 164 root = sgroot->createSceneGraph(); |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
165 // root_moveはコントローラーの入力で動き、座標をLinda Serverにout |
566 | 166 root->set_move_collision(root_move, root_collision); |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
167 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
168 // XMLをメモリにmapして、オブジェクト生成 |
586 | 169 addr = file_map(xml, &size); |
587 | 170 sgp = sgroot->createSceneGraph(); |
171 sgroot->createFromXMLmemory(sgroot->tmanager, sgp, (char *)addr, size); | |
566 | 172 sgp->set_move_collision(move, collision); |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
173 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
174 // rootに接続 |
566 | 175 root->addChild(sgp); |
586 | 176 |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
177 // Linda Serverに接続 |
587 | 178 tspace = open_linda_java(linda, PORT_NUM); |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
179 // rootにLindaのfdを持たせる |
588 | 180 root->propertyptr = sgroot->tmanager->allocate(sizeof(int)); |
181 root->property_size = sizeof(int); | |
182 *(int *)root->propertyptr = tspace; | |
610
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
183 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
184 // このclientのserial_idを取得 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
185 serial = get_serial_id(tspace); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
186 root->id = serial; |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
187 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
188 // ここから先の処理は、裏で何か動かせないかを考える |
d38d9896dfa4
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
d38d9896dfa4
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
d38d9896dfa4
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
d38d9896dfa4
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
d38d9896dfa4
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
369537cb45ad
send position work!!!(linda)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
591
diff
changeset
|
198 |
610
d38d9896dfa4
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足したところに座標データを送る |
d38d9896dfa4
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
d38d9896dfa4
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}; |
d38d9896dfa4
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]; |
d38d9896dfa4
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++) { |
d38d9896dfa4
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; |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
206 // } |
d38d9896dfa4
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); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
208 |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
209 // 初期化のin() |
d38d9896dfa4
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の終了を確認する分岐が最初にあるため |
d38d9896dfa4
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); |
d38d9896dfa4
change htonl to xdr_float (checked working on mac)
kazz@kazzone.st.ie.u-ryukyu.ac.jp
parents:
601
diff
changeset
|
212 sgroot->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 |