comparison 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
comparison
equal deleted inserted replaced
609:9700abc5dd91 610:d38d9896dfa4
5 #include <sys/types.h> 5 #include <sys/types.h>
6 #include <sys/mman.h> 6 #include <sys/mman.h>
7 #include <sys/stat.h> 7 #include <sys/stat.h>
8 #include <unistd.h> 8 #include <unistd.h>
9 #include <arpa/inet.h> 9 #include <arpa/inet.h>
10 #include <rpc/types.h>
11 #include <rpc/xdr.h>
10 #include "SceneGraphRoot.h" 12 #include "SceneGraphRoot.h"
11 #include "lindaapi.h" 13 #include "lindaapi.h"
12 #include "send_linda.h" 14 #include "send_linda.h"
13 15
14 #define HOSTNAME "localhost" 16 #define HOSTNAME "localhost"
15 #define PORT_NUM 10000 17 #define PORT_NUM 10000
16 #define LISTEN_PORT 1 18 #define LISTEN_PORT 1
17 #define MULTI_NUM 10 19 #define MULTI_NUM 10
18 #define SEND_DATA_SIZE 24 //sizeof(float) * 6; 20 #define SEND_DATA_SIZE sizeof(float) * 6
21
19 void 22 void
20 send_position(SceneGraphPtr node) 23 send_position(SceneGraphPtr node)
21 { 24 {
22 char *data; 25 char *data;
23 float send_data[6]; // xyz[3] and angle[3] 26 if ((data = (char *)psx_reply(node->seq)) == NULL) {
24 int tapleid = node->id * 10 + 1; 27 //float send_data[6]; // xyz[3] and angle[3]
25 int fd = *(int*)node->propertyptr; 28 int tapleid = node->id * 10 + 1;
26 int seq; 29 int fd = *(int*)node->propertyptr;
27 int i; 30
28 31 // XDRの準備
29 for (i = 0; i < 3; i ++) { 32 XDR xdrs;
30 send_data[i] = htonl(node->xyz[i]); 33 char send_data[SEND_DATA_SIZE];
31 send_data[i+3] = htonl(node->angle[i]); 34 xdrmem_create(&xdrs, send_data, SEND_DATA_SIZE, XDR_ENCODE);
35
36 for (int i = 0; i < 3; i ++) {
37 xdr_float(&xdrs, &node->xyz[i]);
38 }
39 for (int i = 0; i < 3; i ++) {
40 xdr_float(&xdrs, &node->angle[i]);
41 }
42 node->seq = psx_in(fd, tapleid);
43 psx_out(fd, tapleid, (unsigned char *)send_data, SEND_DATA_SIZE);
44 psx_free(data);
32 } 45 }
33 seq = psx_in(fd, tapleid);
34 do {
35 psx_sync_n();
36 data = (char *)psx_reply(seq);
37 } while (data == NULL);
38 // hton_float(&send_data[0]); hton_float(&send_data[1]);
39 psx_out(fd, tapleid, (unsigned char *)send_data, SEND_DATA_SIZE);
40 psx_sync_n();
41 psx_free(data);
42 } 46 }
43 47
44 void 48 void
45 root_move(SceneGraphPtr node, int w, int h) 49 root_move(SceneGraphPtr node, int w, int h)
46 { 50 {
67 } 71 }
68 72
69 /* 73 /*
70 ここでキー入力を向こうに送る 74 ここでキー入力を向こうに送る
71 */ 75 */
72
73 if (flag) { 76 if (flag) {
74 send_position(node); 77 send_position(node);
75 } 78 }
76 } 79 }
77 80
138 send_xml(int tspace, int xml_id, void *addr, int size) { 141 send_xml(int tspace, int xml_id, void *addr, int size) {
139 psx_out(tspace, xml_id, (unsigned char *)addr, size); 142 psx_out(tspace, xml_id, (unsigned char *)addr, size);
140 psx_sync_n(); 143 psx_sync_n();
141 } 144 }
142 145
143 /*
144 void
145 mainLoop(int tid, int write_id, int fd)
146 {
147 void *addr;
148 struct stat sb;
149
150 if (fstat(fd, &sb) == -1) {
151 perror("fstat");
152 exit(1);
153 }
154 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
155 if (addr==NULL) {
156 perror("mmap");
157 exit(1);
158 }
159
160 printf("file size=%d\n", sb.st_size);
161
162 psx_out(tid, write_id, addr, sb.st_size);
163 psx_sync_n();
164
165 return;
166 }
167 */
168
169 static char *xml; 146 static char *xml;
170 static char *linda = HOSTNAME; 147 static char *linda = HOSTNAME;
171 148
172 MainLoopPtr 149 MainLoopPtr
173 send_linda::init(Viewer *sgr, int screen_w, int screen_h) 150 send_linda::init(Viewer *sgr, int screen_w, int screen_h)
179 int xml_id; 156 int xml_id;
180 157
181 SceneGraphPtr sgp; 158 SceneGraphPtr sgp;
182 SceneGraphPtr root; 159 SceneGraphPtr root;
183 160
161 // ここら辺長ったるいから、関数で分けるべきか...
162
163 // root オブジェクト作成
184 root = sgroot->createSceneGraph(); 164 root = sgroot->createSceneGraph();
165 // root_moveはコントローラーの入力で動き、座標をLinda Serverにout
185 root->set_move_collision(root_move, root_collision); 166 root->set_move_collision(root_move, root_collision);
186 167
168 // XMLをメモリにmapして、オブジェクト生成
187 addr = file_map(xml, &size); 169 addr = file_map(xml, &size);
188 sgp = sgroot->createSceneGraph(); 170 sgp = sgroot->createSceneGraph();
189 sgroot->createFromXMLmemory(sgroot->tmanager, sgp, (char *)addr, size); 171 sgroot->createFromXMLmemory(sgroot->tmanager, sgp, (char *)addr, size);
190 sgp->set_move_collision(move, collision); 172 sgp->set_move_collision(move, collision);
191 173
174 // rootに接続
192 root->addChild(sgp); 175 root->addChild(sgp);
193 176
177 // Linda Serverに接続
194 tspace = open_linda_java(linda, PORT_NUM); 178 tspace = open_linda_java(linda, PORT_NUM);
179 // rootにLindaのfdを持たせる
195 root->propertyptr = sgroot->tmanager->allocate(sizeof(int)); 180 root->propertyptr = sgroot->tmanager->allocate(sizeof(int));
196 root->property_size = sizeof(int); 181 root->property_size = sizeof(int);
197 *(int *)root->propertyptr = tspace; 182 *(int *)root->propertyptr = tspace;
183
184 // このclientのserial_idを取得
185 serial = get_serial_id(tspace);
186 root->id = serial;
187
188 // ここから先の処理は、裏で何か動かせないかを考える
189 // とりあえず、関数に分けようか
198 190
199 serial = get_serial_id(tspace); 191 // serial_idを十倍したところにXMLを送信
200 root->id = serial;
201 xml_id = serial * 10; 192 xml_id = serial * 10;
202 send_xml(tspace, xml_id, addr, size); 193 send_xml(tspace, xml_id, addr, size); // ここのpsx_sync_n()は仕方ない
203 194
195 // XMLの送信が終了してから、serial_idをLindaに送信する
204 int client_id = htonl(serial); 196 int client_id = htonl(serial);
205 197 psx_out(tspace, LISTEN_PORT, (unsigned char *)&client_id, sizeof(int));
206 send_xml(tspace, LISTEN_PORT, (unsigned char *)&client_id, sizeof(int)); 198
199 // serial_idを十倍して1足したところに座標データを送る
200 // 初期化のout()
201 int pos_id = serial * 10 + 1;
202 char pos_data[SEND_DATA_SIZE] = {0};
203 // float pos_data[6];
204 // for (int i = 0; i < 6; i++) {
205 // pos_data[i] = 0.0f;
206 // }
207 psx_out(tspace, pos_id, (unsigned char *)pos_data, SEND_DATA_SIZE);
208
209 // 初期化のin()
210 // send_position()でinの終了を確認する分岐が最初にあるため
211 root->seq = psx_in(tspace, pos_id);
212 sgroot->setSceneData(root);
207 213
208 int pos_id = serial * 10 + 1;
209 char pos_data[SEND_DATA_SIZE];
210 for (int i = 0; i < 6; i++) {
211 ((float *)pos_data)[i] = htonl(0.0f);
212 }
213 send_xml(tspace, pos_id, (unsigned char *)pos_data, SEND_DATA_SIZE);
214
215 sgroot->setSceneData(root);
216 return sgr; 214 return sgr;
217 } 215 }
218 216
219 extern Application * 217 extern Application *
220 application() { 218 application() {