001 package org.server; 002 003 import java.net.*; 004 import java.io.*; 005 import java.util.*; 006 import java.awt.geom.*; 007 008 import org.realobject.*; 009 import org.server.*; 010 011 import org.util.xml.io.*; 012 import org.util.xml.element.*; 013 import org.server.motion.*; 014 /** 015 * 016 */ 017 public class DummyServer { 018 019 private static String config_file_name_ = "data/config-DummyServer.xml"; 020 021 private ArrayList<RCRSVirtualSoccerEcoBe> ecobe_list_ = new ArrayList<RCRSVirtualSoccerEcoBe>(); 022 private ArrayList<Mark> mark_list_ = new ArrayList<Mark>(); 023 private ArrayList<RCRSVirtualSoccerEcoBe> blue_team_member_list_ = new ArrayList<RCRSVirtualSoccerEcoBe>(); 024 private ArrayList<RCRSVirtualSoccerEcoBe> yellow_team_member_list_ = new ArrayList<RCRSVirtualSoccerEcoBe>(); 025 private ArrayList<DummyServerListener> dslistener_list_ = new ArrayList<DummyServerListener>(); 026 private Ball ball_; 027 // private Point2D.Double ball_location_; 028 // private double ball_radius_ = 7; 029 private MotionSimulator motion_simulator_; 030 031 private HashMap<String, Point2D.Double> flag_location_map_ = new HashMap<String, Point2D.Double>(); 032 private Rectangle2D flag_area_; 033 private double time_ =0; 034 035 public static void main(String[] args) throws Exception { 036 037 if(args.length==1) 038 config_file_name_ = args[0]; 039 040 System.out.println("Now starting DummyServer."); 041 System.out.println("config file: " + config_file_name_); 042 XMLConfigManager config = new XMLConfigManager(config_file_name_); 043 DummyServer server = new DummyServer(config); 044 045 new DummyServerGUI(server); 046 047 server.start(); 048 049 } 050 051 public void setMotionSimulator(MotionSimulator motion_simulator) { 052 motion_simulator_ = motion_simulator; 053 if(motion_simulator_!=null) 054 motion_simulator_.init(flag_location_map_, ecobe_list_, ball_); 055 } 056 public MotionSimulator getMotionSimulator() { 057 return motion_simulator_; 058 } 059 060 public ArrayList<RCRSVirtualSoccerEcoBe> getEcoBeList() { 061 return ecobe_list_; 062 } 063 public ArrayList<RCRSVirtualSoccerEcoBe> getBlueTeamMemberList() { 064 return blue_team_member_list_; 065 } 066 public ArrayList<RCRSVirtualSoccerEcoBe> getYellowTeamMemberList() { 067 return yellow_team_member_list_; 068 } 069 public HashMap<String, Point2D.Double> getFlags() { 070 return flag_location_map_; 071 } 072 public Rectangle2D getArea() { 073 return flag_area_; 074 } 075 076 public DummyServer(XMLConfigManager config) throws Exception { 077 init(config); 078 } 079 080 public Point2D.Double getBallLocation() { 081 return ball_.getLocation(); 082 } 083 084 public double getBallRadius() { 085 return ball_.getRadius(); 086 } 087 088 private XMLConfigManager config_; 089 private DatagramPacket blue_packet_; 090 private DatagramSocket blue_socket_; 091 private byte[] blue_receive_buf_; 092 private DatagramPacket yellow_packet_; 093 private DatagramSocket yellow_socket_; 094 private byte[] yellow_receive_buf_; 095 096 public void init(XMLConfigManager config) throws Exception { 097 config_ = config; 098 flag_location_map_.put("top_left_pole", new Point2D.Double(0, 200)); 099 flag_location_map_.put("bottom_left_pole", new Point2D.Double(0, 300)); 100 flag_location_map_.put("bottom_right_pole", new Point2D.Double(1000, 200)); 101 flag_location_map_.put("top_right_pole", new Point2D.Double(1000, 300)); 102 103 flag_location_map_.put("top_left_corner", new Point2D.Double(0, 0)); 104 flag_location_map_.put("bottom_left_corner", new Point2D.Double(0, 500)); 105 flag_location_map_.put("top_right_corner", new Point2D.Double(1000, 0)); 106 flag_location_map_.put("bottom_right_corner", new Point2D.Double(1000, 500)); 107 108 flag_location_map_.put("top_left_goal", new Point2D.Double(00, 200)); 109 flag_location_map_.put("bottom_left_goal", new Point2D.Double(00, 300)); 110 flag_location_map_.put("top_right_goal", new Point2D.Double(1000, 200)); 111 flag_location_map_.put("bottom_right_goal", new Point2D.Double(1000, 300)); 112 113 flag_location_map_.put("bottom_left_small_area", new Point2D.Double(70, 200)); 114 flag_location_map_.put("top_left_small_area", new Point2D.Double(70, 300)); 115 flag_location_map_.put("top_right_small_area", new Point2D.Double(930, 200)); 116 flag_location_map_.put("bottom_right_small_area", new Point2D.Double(930, 300)); 117 118 flag_location_map_.put("middle_center", new Point2D.Double(500, 250)); 119 flag_location_map_.put("top_center", new Point2D.Double(500, 0)); 120 flag_location_map_.put("bottom_center", new Point2D.Double(500, 500)); 121 ball_ = new Ball(new Point2D.Double(100, 100), 15, 10); 122 123 for(Iterator<Point2D.Double> it = flag_location_map_.values().iterator(); it.hasNext();) { 124 Point2D.Double p = it.next(); 125 if(flag_area_==null) 126 flag_area_ = new Rectangle2D.Double(p.getX(), p.getY(), 0, 0); 127 else 128 flag_area_.add(p); 129 } 130 131 } 132 133 public void addDummyServerListener(DummyServerListener listener) { 134 dslistener_list_.add(listener); 135 } 136 137 public void start() throws Exception { 138 init(); 139 140 new Thread(new Runnable(){public void run(){ 141 startProcess(); 142 }}).start(); 143 144 // blue team packet receive thread 145 new Thread(new Runnable(){public void run(){try{ 146 while(true) { 147 blue_packet_.setData(blue_receive_buf_); 148 blue_socket_.receive(blue_packet_); 149 InetSocketAddress address = (InetSocketAddress)blue_packet_.getSocketAddress(); 150 RCRSVirtualSoccerEcoBe ecobe = findEcoBe(address); 151 if(ecobe != null) { 152 ecobe.update(blue_packet_); 153 } else { 154 try{ 155 tryToCreateNewBlueConnection(blue_packet_); 156 }catch(Exception e) { 157 e.printStackTrace(); 158 } 159 } 160 } 161 }catch(Exception e){e.printStackTrace();}}}).start(); 162 163 // yellow team packet receive thread 164 new Thread(new Runnable(){public void run(){try{ 165 while(true) { 166 yellow_packet_.setData(yellow_receive_buf_); 167 yellow_socket_.receive(yellow_packet_); 168 InetSocketAddress address = (InetSocketAddress)yellow_packet_.getSocketAddress(); 169 RCRSVirtualSoccerEcoBe ecobe = findEcoBe(address); 170 if(ecobe != null) { 171 ecobe.update(yellow_packet_); 172 } else { 173 try{ 174 tryToCreateNewYellowConnection(yellow_packet_); 175 }catch(Exception e) { 176 e.printStackTrace(); 177 } 178 } 179 } 180 }catch(Exception e){e.printStackTrace();}}}).start(); 181 182 // new Thread(new Runnable(){public void run(){ 183 // }}).start(); 184 185 } 186 187 188 189 int step_length_ = 1000/15; //[ms] 190 int counter = 0; 191 int skip_counter_=0; 192 public void startProcess() { 193 /* 194 String sampledata = "<WorldData><time>5.25131</time><agent_id>0</agent_id><nickname>player1</nickname><status>found</status><max_agent>2</max_agent><playMode>play on</playMode><score><yellow>0</yellow><blue>0</blue></score><wheel_velocities><right>0</right><left>0</left></wheel_velocities><ball><dist>117</dist><angle>39</angle></ball><teamMate><id>1</id><nickname>player2</nickname><status>found</status><dist>89</dist><angle>48</angle><orientation>107</orientation></teamMate><teamMate><id>2</id><nickname>player3</nickname><status>found</status><dist>79</dist><angle>-21</angle><orientation>103</orientation></teamMate><teamMate><id>3</id><nickname>player4</nickname><status>found</status><dist>74</dist><angle>-57</angle><orientation>273</orientation></teamMate><teamMate><id>4</id><nickname>player0</nickname><status>found</status><dist>70</dist><angle>66</angle><orientation>258</orientation></teamMate><opponent><id>5</id><nickname>player7</nickname><status>found</status><dist>167</dist><angle>46</angle><orientation>161</orientation></opponent><opponent><id>6</id><nickname>player9</nickname><status>found</status><dist>56</dist><angle>3</angle><orientation>249</orientation></opponent><opponent><id>7</id><nickname>player5</nickname><status>found</status><dist>287</dist><angle>58</angle><orientation>357</orientation></opponent><opponent><id>8</id><nickname>player8</nickname><status>found</status><dist>53</dist><angle>0</angle><orientation>328</orientation></opponent><opponent><id>9</id><nickname>player6</nickname><status>found</status><dist>128</dist><angle>-34</angle><orientation>6</orientation></opponent><flag><id>top_left_pole</id><dist>152</dist><angle>-59</angle></flag><flag><id>bottom_left_pole</id><dist>116</dist><angle>-83</angle></flag><flag><id>top_right_pole</id><dist>310</dist><angle>57</angle></flag><flag><id>bottom_right_pole</id><dist>294</dist><angle>69</angle></flag><flag><id>top_left_corner</id><dist>236</dist><angle>-40</angle></flag><flag><id>bottom_left_corner</id><dist>126</dist><angle>-133</angle></flag><flag><id>top_right_corner</id><dist>359</dist><angle>41</angle></flag><flag><id>bottom_right_corner</id><dist>298</dist><angle>89</angle></flag><flag><id>top_left_goal</id><dist>148</dist><angle>-8</angle></flag><flag><id>bottom_left_goal</id><dist>14</dist><angle>85</angle></flag><flag><id>top_right_goal</id><dist>224</dist><angle>36</angle></flag><flag><id>bottom_right_goal</id><dist>169</dist><angle>78</angle></flag><flag><id>top_left_small_area</id><dist>83</dist><angle>-74</angle></flag><flag><id>bottom_left_small_area</id><dist>128</dist><angle>-48</angle></flag><flag><id>top_right_small_area</id><dist>259</dist><angle>68</angle></flag><flag><id>bottom_right_small_area</id><dist>277</dist><angle>55</angle></flag><flag><id>middle_center</id><dist>117</dist><angle>39</angle></flag><flag><id>top_center</id><dist>229</dist><angle>11</angle></flag><flag><id>bottom_center</id><dist>112</dist><angle>112</angle></flag></WorldData>"; 195 */ 196 //double sin = Math.sin((double)counter*0.01); 197 //double cos = Math.sin((double)counter*0.01); 198 199 while(true) { 200 int rad = (counter+=5)%360-180; 201 long last_time = System.currentTimeMillis(); 202 203 final String sampledata2 = "<WorldData><time>"+time_+"</time><agent_id>0</agent_id><nickname>player1</nickname><status>found</status><max_agent>5</max_agent><playMode>play on</playMode><score><yellow>0</yellow><blue>0</blue></score><wheel_velocities><right>0</right><left>0</left></wheel_velocities><ball><dist>117</dist><angle>"+rad+"</angle></ball><teamMate><id>1</id><nickname>player2</nickname><status>found</status><dist>89</dist><angle>48</angle><orientation>107</orientation></teamMate><teamMate><id>2</id><nickname>player3</nickname><status>found</status><dist>79</dist><angle>-21</angle><orientation>103</orientation></teamMate><teamMate><id>3</id><nickname>player4</nickname><status>found</status><dist>74</dist><angle>-57</angle><orientation>273</orientation></teamMate><teamMate><id>4</id><nickname>player0</nickname><status>found</status><dist>70</dist><angle>66</angle><orientation>258</orientation></teamMate><opponent><id>5</id><nickname>player7</nickname><status>found</status><dist>167</dist><angle>46</angle><orientation>161</orientation></opponent><opponent><id>6</id><nickname>player9</nickname><status>found</status><dist>56</dist><angle>3</angle><orientation>249</orientation></opponent><opponent><id>7</id><nickname>player5</nickname><status>found</status><dist>287</dist><angle>58</angle><orientation>357</orientation></opponent><opponent><id>8</id><nickname>player8</nickname><status>found</status><dist>53</dist><angle>0</angle><orientation>328</orientation></opponent><opponent><id>9</id><nickname>player6</nickname><status>found</status><dist>128</dist><angle>-34</angle><orientation>6</orientation></opponent><flag><id>top_left_pole</id><dist>152</dist><angle>-59</angle></flag><flag><id>bottom_left_pole</id><dist>116</dist><angle>-83</angle></flag><flag><id>top_right_pole</id><dist>310</dist><angle>57</angle></flag><flag><id>bottom_right_pole</id><dist>294</dist><angle>69</angle></flag><flag><id>top_left_corner</id><dist>236</dist><angle>-40</angle></flag><flag><id>bottom_left_corner</id><dist>126</dist><angle>-133</angle></flag><flag><id>top_right_corner</id><dist>359</dist><angle>41</angle></flag><flag><id>bottom_right_corner</id><dist>298</dist><angle>89</angle></flag><flag><id>top_left_goal</id><dist>148</dist><angle>-8</angle></flag><flag><id>bottom_left_goal</id><dist>14</dist><angle>85</angle></flag><flag><id>top_right_goal</id><dist>224</dist><angle>36</angle></flag><flag><id>bottom_right_goal</id><dist>169</dist><angle>78</angle></flag><flag><id>top_left_small_area</id><dist>83</dist><angle>-74</angle></flag><flag><id>bottom_left_small_area</id><dist>128</dist><angle>-48</angle></flag><flag><id>top_right_small_area</id><dist>259</dist><angle>68</angle></flag><flag><id>bottom_right_small_area</id><dist>277</dist><angle>55</angle></flag><flag><id>middle_center</id><dist>117</dist><angle>39</angle></flag><flag><id>top_center</id><dist>229</dist><angle>11</angle></flag><flag><id>bottom_center</id><dist>112</dist><angle>112</angle></flag></WorldData>"; 204 205 //System.out.println(sampledata2); 206 207 //log("send message: "); 208 for(int i=0;i<blue_team_member_list_.size();i++) { 209 RCRSVirtualSoccerEcoBe ecobe = blue_team_member_list_.get(i); 210 String sampledata = createMessage(ecobe); 211 try{ 212 if(ecobe.isConnected()) { 213 blue_socket_.send(ecobe.createPacket(sampledata)); 214 }else{ 215 // System.out.println("skip: "+skip_counter_++); 216 } 217 }catch(Exception e) { 218 e.printStackTrace(); 219 } 220 } 221 for(int i=0;i<yellow_team_member_list_.size();i++) { 222 RCRSVirtualSoccerEcoBe ecobe = yellow_team_member_list_.get(i); 223 String sampledata = createMessage(ecobe); 224 try{ 225 if(ecobe.isConnected()) { 226 yellow_socket_.send(ecobe.createPacket(sampledata)); 227 }else{ 228 // System.out.println("skip: "+skip_counter_++); 229 } 230 }catch(Exception e) { 231 e.printStackTrace(); 232 } 233 } 234 235 if(motion_simulator_!=null) 236 motion_simulator_.simulateStep(1); 237 238 for(int j=0;j<dslistener_list_.size();j++) 239 try{ 240 dslistener_list_.get(j).update(); 241 }catch(Exception e){e.printStackTrace();} 242 243 long time = System.currentTimeMillis(); 244 int wait = step_length_ - (int)(time-last_time); 245 // System.out.println("wait: "+wait); 246 if(wait<0) 247 System.out.println("cannot keep step length: "+step_length_+"<"+(last_time-time)); 248 else 249 try{Thread.sleep(wait);}catch(Exception e) {} 250 251 time_ += ((double)step_length_)/1000; 252 } 253 } 254 /* 255 public void createClient(DatagramPacket packet) throws Exception { 256 257 InetSocketAddress address = (InetSocketAddress)packet.getSocketAddress(); 258 for(int i=0;i<dslistener_list_.size();i++) 259 dslistener_list_.get(i).connectionDetected((address)); 260 String message = new String(packet.getData(), 0, packet.getLength()); 261 TagElement tag = XMLIO.read(new StringBuffer(message)); 262 if(!tag.getChildValue("type").equals("Client")) 263 throw new Exception("unsupported type: "+tag.getChildValue("type")); 264 if(!tag.getChildValue("protocol_version").equals("1.0")) 265 throw new Exception("unsupported protocol_version: "+tag.getChildValue("protocol_version")); 266 String nickname = tag.getChildValue("nickname"); 267 int rc_id = Integer.parseInt(tag.getChildValue("rc_id")); 268 int vt_id = Integer.parseInt(tag.getChildValue("vt_id")); 269 270 EcoBe ecobe = findEcoBe(rc_id); 271 Mark mark = findMark(vt_id); 272 273 send(new_client.createPacket("<connect>true</connect>")); 274 275 SoccerClient new_client = new SoccerClient(address, ecobe); 276 client_list_.add(new_client); 277 278 send(new_client.createPacket("<connect><type>Client</type><protocol_version>1.0</protocol_version><movement_mode>wheel_velocities</movement_mode></connect>")); 279 280 for(int i=0;i<dslistener_list_.size();i++) 281 dslistener_list_.get(i).failedToConnect(address, "???"); 282 283 log("Connected new client: "+new_client); 284 } 285 */ 286 public void tryToCreateNewYellowConnection(DatagramPacket packet) throws Exception { 287 InetSocketAddress address = (InetSocketAddress)packet.getSocketAddress(); 288 RCRSVirtualSoccerEcoBe ecobe = null; 289 for(int i=0;i<yellow_team_member_list_.size()&&ecobe==null;i++) { 290 if(!yellow_team_member_list_.get(i).isConnected()) 291 ecobe = yellow_team_member_list_.get(i); 292 } 293 if(ecobe == null) throw new Exception("yellow team is already full"); 294 ecobe.setClient(address); 295 ecobe.update(packet); 296 yellow_socket_.send(ecobe.createPacket("<connect>true</connect>")); 297 yellow_socket_.send(ecobe.createPacket("<connect><type>Client</type><protocol_version>1.0</protocol_version><movement_mode>wheel_velocities</movement_mode></connect>")); 298 log("Connected new client(yellow): "+ecobe); 299 } 300 301 public void tryToCreateNewBlueConnection(DatagramPacket packet) throws Exception { 302 InetSocketAddress address = (InetSocketAddress)packet.getSocketAddress(); 303 RCRSVirtualSoccerEcoBe ecobe = null; 304 for(int i=0;i<blue_team_member_list_.size()&&ecobe==null;i++) { 305 if(!blue_team_member_list_.get(i).isConnected()) 306 ecobe = blue_team_member_list_.get(i); 307 } 308 if(ecobe == null) throw new Exception("blue team is already full"); 309 ecobe.setClient(address); 310 ecobe.update(packet); 311 blue_socket_.send(ecobe.createPacket("<connect>true</connect>")); 312 blue_socket_.send(ecobe.createPacket("<connect><type>Client</type><protocol_version>1.0</protocol_version><movement_mode>wheel_velocities</movement_mode></connect>")); 313 log("Connected new client(blue): "+ecobe); 314 } 315 /* 316 public void tryToCreateNewConnection(DatagramPacket packet) throws Exception { 317 318 InetSocketAddress address = (InetSocketAddress)packet.getSocketAddress(); 319 int port = address.getPort(); 320 321 // System.out.println(port); 322 for(int i=0;i<dslistener_list_.size();i++) 323 dslistener_list_.get(i).connectionDetected((address)); 324 String message = new String(packet.getData(), 0, packet.getLength()); 325 TagElement tag = XMLIO.read(new StringBuffer(message)); 326 if(!tag.getChildValue("type").equals("Client")) 327 throw new Exception("unsupported type: "+tag.getChildValue("type")); 328 if(!tag.getChildValue("protocol_version").equals("1.0")) 329 throw new Exception("unsupported protocol_version: "+tag.getChildValue("protocol_version")); 330 String nickname = tag.getChildValue("nickname"); 331 int rc_id = Integer.parseInt(tag.getChildValue("rc_id")); 332 int vt_id = Integer.parseInt(tag.getChildValue("vt_id")); 333 334 RCRSVirtualSoccerEcoBe ecobe = findEcoBe(rc_id); 335 // Mark mark = findMark(vt_id); 336 337 ecobe.setClient(address); 338 ecobe.update(packet); 339 340 341 342 send(ecobe.createPacket("<connect>true</connect>")); 343 send(ecobe.createPacket("<connect><type>Client</type><protocol_version>1.0</protocol_version><movement_mode>wheel_velocities</movement_mode></connect>")); 344 345 346 347 348 // for(int i=0;i<dslistener_list_.size();i++) 349 // dslistener_list_.get(i).failedToConnect(address, "???"); 350 351 log("Connected new client: "+ecobe); 352 } 353 */ 354 public RCRSVirtualSoccerEcoBe findEcoBe(InetSocketAddress address) { 355 for(RCRSVirtualSoccerEcoBe ecobe : ecobe_list_) { 356 //System.out.println("available address: "+address); 357 if(address.equals(ecobe.getRemoteAddress())) { 358 // System.out.println("find address: "+address); 359 return ecobe; 360 } 361 } 362 System.out.println("cannnot find address: "+address); 363 return null; 364 } 365 public RCRSVirtualSoccerEcoBe findEcoBe(int id) throws Exception { 366 for(int i=0;i<ecobe_list_.size();i++) 367 if(ecobe_list_.get(i).getID() == id) 368 return ecobe_list_.get(i); 369 throw new Exception("cannot find EcoBe: "+id); 370 } 371 public Mark findMark(int id) throws Exception { 372 for(int i=0;i<mark_list_.size();i++) 373 if(mark_list_.get(i).getID() == id) 374 return mark_list_.get(i); 375 throw new Exception("cannot find Mark: "+id); 376 } 377 378 /* 379 public void start() throws Exception { 380 381 init(); 382 383 String message = null; 384 TagElement response = null; 385 try{ 386 message = receive(); 387 response = createResponseMessage(true); 388 389 send(response); 390 391 send("<connect><type>Client</type><protocol_version>1.0</protocol_version><movement_mode>wheel_velocities</movement_mode></connect>"); 392 message = receive(); 393 // 394 String sampledata = "<WorldData><time>5.25131</time><agent_id>0</agent_id><nickname>player1</nickname><status>found</status><max_agent>2</max_agent><playMode>play on</playMode><score><yellow>0</yellow><blue>0</blue></score><wheel_velocities><right>0</right><left>0</left></wheel_velocities><ball><dist>117</dist><angle>39</angle></ball><teamMate><id>1</id><nickname>player2</nickname><status>found</status><dist>89</dist><angle>48</angle><orientation>107</orientation></teamMate><teamMate><id>2</id><nickname>player3</nickname><status>found</status><dist>79</dist><angle>-21</angle><orientation>103</orientation></teamMate><teamMate><id>3</id><nickname>player4</nickname><status>found</status><dist>74</dist><angle>-57</angle><orientation>273</orientation></teamMate><teamMate><id>4</id><nickname>player0</nickname><status>found</status><dist>70</dist><angle>66</angle><orientation>258</orientation></teamMate><opponent><id>5</id><nickname>player7</nickname><status>found</status><dist>167</dist><angle>46</angle><orientation>161</orientation></opponent><opponent><id>6</id><nickname>player9</nickname><status>found</status><dist>56</dist><angle>3</angle><orientation>249</orientation></opponent><opponent><id>7</id><nickname>player5</nickname><status>found</status><dist>287</dist><angle>58</angle><orientation>357</orientation></opponent><opponent><id>8</id><nickname>player8</nickname><status>found</status><dist>53</dist><angle>0</angle><orientation>328</orientation></opponent><opponent><id>9</id><nickname>player6</nickname><status>found</status><dist>128</dist><angle>-34</angle><orientation>6</orientation></opponent><flag><id>top_left_pole</id><dist>152</dist><angle>-59</angle></flag><flag><id>bottom_left_pole</id><dist>116</dist><angle>-83</angle></flag><flag><id>top_right_pole</id><dist>310</dist><angle>57</angle></flag><flag><id>bottom_right_pole</id><dist>294</dist><angle>69</angle></flag><flag><id>top_left_corner</id><dist>236</dist><angle>-40</angle></flag><flag><id>bottom_left_corner</id><dist>126</dist><angle>-133</angle></flag><flag><id>top_right_corner</id><dist>359</dist><angle>41</angle></flag><flag><id>bottom_right_corner</id><dist>298</dist><angle>89</angle></flag><flag><id>top_left_goal</id><dist>148</dist><angle>-8</angle></flag><flag><id>bottom_left_goal</id><dist>14</dist><angle>85</angle></flag><flag><id>top_right_goal</id><dist>224</dist><angle>36</angle></flag><flag><id>bottom_right_goal</id><dist>169</dist><angle>78</angle></flag><flag><id>top_left_small_area</id><dist>83</dist><angle>-74</angle></flag><flag><id>bottom_left_small_area</id><dist>128</dist><angle>-48</angle></flag><flag><id>top_right_small_area</id><dist>259</dist><angle>68</angle></flag><flag><id>bottom_right_small_area</id><dist>277</dist><angle>55</angle></flag><flag><id>middle_center</id><dist>117</dist><angle>39</angle></flag><flag><id>top_center</id><dist>229</dist><angle>11</angle></flag><flag><id>bottom_center</id><dist>112</dist><angle>112</angle></flag></WorldData>"; 395 396 for(int i=0;i<10;i++) { 397 send(sampledata); 398 message = receive(); 399 // System.out.println("received command:"+message); 400 } 401 }catch(Exception e){ 402 response = createResponseMessage(false); 403 } 404 405 //alert("Dummy Server received message: \n"+message); 406 } 407 */ 408 409 public void init() throws Exception { 410 initEcoBe(); 411 initSocket(); 412 } 413 414 public void initEcoBe() throws Exception { 415 ecobe_list_.clear(); 416 mark_list_.clear(); 417 yellow_team_member_list_.clear(); 418 blue_team_member_list_.clear(); 419 420 TagElement[] ecobe_tag = config_.getTag("team_yellow/ecobe_list").getTagChildren("ecobe"); 421 for(int i=0;i<ecobe_tag.length;i++) { 422 int vt_id = Integer.parseInt(ecobe_tag[i].getChildValue("vt_id")); 423 int rc_id = Integer.parseInt(ecobe_tag[i].getChildValue("rc_id")); 424 Mark mark = new Mark(vt_id); 425 RCRSVirtualSoccerEcoBe ecobe = new RCRSVirtualSoccerEcoBe(rc_id); 426 ecobe.setTeamName("yellow"); 427 ecobe.putMark(mark); 428 mark_list_.add(mark); 429 ecobe_list_.add(ecobe); 430 yellow_team_member_list_.add(ecobe); 431 } 432 433 ecobe_tag = config_.getTag("team_blue/ecobe_list").getTagChildren("ecobe"); 434 for(int i=0;i<ecobe_tag.length;i++) { 435 int vt_id = Integer.parseInt(ecobe_tag[i].getChildValue("vt_id")); 436 int rc_id = Integer.parseInt(ecobe_tag[i].getChildValue("rc_id")); 437 Mark mark = new Mark(vt_id); 438 RCRSVirtualSoccerEcoBe ecobe = new RCRSVirtualSoccerEcoBe(rc_id); 439 ecobe.setTeamName("blue"); 440 ecobe.putMark(mark); 441 mark_list_.add(mark); 442 ecobe_list_.add(ecobe); 443 blue_team_member_list_.add(ecobe); 444 } 445 // javax.swing.JOptionPane.showMessageDialog(null, yellow_team_member_list_.size()+","+blue_team_member_list_.size()); 446 } 447 448 private void log(String message) { 449 for(int i=0;i<dslistener_list_.size();i++) 450 dslistener_list_.get(i).log(message); 451 } 452 453 454 public TagElement createFirstMessage() throws Exception { 455 TagElement message = new TagElement("connect"); 456 message.setChildren( 457 new TagElement("type", config_.getValue("connection/type", "Client")), 458 new TagElement("protocol_version", config_.getValue("connection/protocol_version", "1.0")), 459 new TagElement("nickname", config_.getValue("connection/nickname", "no_name")), 460 new TagElement("rc_id", config_.getValue("connection/rc_id", "0")), 461 new TagElement("vt_id", config_.getValue("connection/vt_id", "0")) 462 ); 463 return message; 464 } 465 466 public void initSocket() throws Exception { 467 int timeout = config_.getInt("protocol/timeout", 1000); 468 int message_max_length = config_.getInt("protocol/message_max_length", 1024*4); 469 int blue_port = config_.getInt("team_blue/port", 3310); 470 int yellow_port = config_.getInt("yellow_blue/port", 3311); 471 System.out.println("Server port blue: "+blue_port); 472 System.out.println("Server port yellow: "+yellow_port); 473 System.out.println("message max length: "+message_max_length); 474 System.out.println("timeout: "+timeout); 475 476 blue_packet_ = new DatagramPacket(new byte[0], 0); 477 blue_socket_ = new DatagramSocket(blue_port); 478 blue_socket_.setSoTimeout(timeout); 479 blue_receive_buf_ = new byte[message_max_length]; 480 481 yellow_packet_ = new DatagramPacket(new byte[0], 0); 482 yellow_socket_ = new DatagramSocket(yellow_port); 483 yellow_socket_.setSoTimeout(timeout); 484 yellow_receive_buf_ = new byte[message_max_length]; 485 /* 486 int blue_port = config_.getInt("server/port", 3310); 487 System.out.println("Server port: "+blue_port); 488 System.out.println("timeout: "+timeout); 489 blue_packet_ = new DatagramPacket(new byte[0], 0); 490 blue_socket_ = new DatagramSocket(port); 491 blue_socket_.setSoTimeout(timeout); 492 blue_receive_buf_ = new byte[config_.getInt("server/message_max_length", 1024*4)]; 493 */ 494 } 495 496 public void send(TagElement tag, DatagramPacket packet_, DatagramSocket socket_) throws Exception { 497 send(tag.toString(), packet_, socket_); 498 } 499 public void send(String message, DatagramPacket packet_, DatagramSocket socket_) throws Exception { 500 byte[] send_buf = message.getBytes(); 501 packet_.setData(send_buf); 502 socket_.send(blue_packet_); 503 System.out.println("send:" + message); 504 } 505 /* 506 public void send(String message) throws Exception { 507 byte[] send_buf = message.getBytes(); 508 blue_packet_.setData(send_buf); 509 blue_socket_.send(blue_packet_); 510 System.out.println("send:" + message); 511 } 512 */ 513 /* 514 public void send(DatagramPacket packet) throws Exception { 515 blue_socket_.send(packet); 516 } 517 */ 518 /* 519 public void send(String message, DatagramPacket received_packet, DatagramSocket socket_) throws Exception { 520 byte[] send_buf = message.getBytes(); 521 received_packet.setData(send_buf); 522 socket_.send(received_packet); 523 System.out.println("send:" + message); 524 } 525 */ 526 public TagElement createResponseMessage(boolean success) throws Exception { 527 TagElement message = new TagElement("connect", (success ? "true" : "false")); 528 return message; 529 } 530 531 public String receive() throws Exception { 532 // System.out.println("wait receiving..."); 533 blue_packet_.setData(blue_receive_buf_); 534 blue_socket_.receive(blue_packet_); 535 536 String response = new String(blue_packet_.getData(), 0, blue_packet_.getLength()); 537 System.out.println("receive:" + response); 538 return response; 539 } 540 541 public String createMessage(RCRSVirtualSoccerEcoBe ecobe) { 542 int member_length = 4; // member_length = {1(me)+teammate_length} + {opponent_length} 543 int opponent_length = member_length/2; 544 int teammate_length = opponent_length-1; 545 546 StringBuffer sb = new StringBuffer(); 547 sb.append("<WorldData>"); 548 sb.append(createKeyValueTag("time", String.valueOf(time_))); 549 sb.append(createKeyValueTag("agent_id", ecobe.getID())); 550 sb.append(createKeyValueTag("nickname", "player1")); 551 sb.append(createKeyValueTag("status", "found")); 552 sb.append(createKeyValueTag("max_agent", ecobe_list_.size())); 553 sb.append(createKeyValueTag("playMode", "play on")); 554 sb.append("<score>"); 555 sb.append(createKeyValueTag("yellow", "0")); 556 sb.append(createKeyValueTag("blue", "0")); 557 sb.append("</score>"); 558 sb.append("<wheel_velocities>"); 559 sb.append(createKeyValueTag("right", "0")); 560 sb.append(createKeyValueTag("left", "0")); 561 sb.append("</wheel_velocities>"); 562 sb.append(createLocationTag("ball", ecobe, ball_.getLocation().getX(), ball_.getLocation().getY())); 563 564 565 /* 566 int now_teammate_count = 0; 567 for(;now_teammate_count<blue_team_member_list_.size();now_teammate_count++) { 568 if(blue_team_member_list_.get(now_teammate_count)==ecobe) 569 sb.append(createMemberTag("teamMate", blue_team_member_list_.get(now_teammate_count))); 570 } 571 for(int i=0;i<yellow_team_member_list_.size();i++) 572 sb.append(createMemberTag("opponent", yellow_team_member_list_.get(i))); 573 574 */ 575 ArrayList<RCRSVirtualSoccerEcoBe> opponent_list, teammate_list; 576 if(ecobe.getTeamName().equals("blue")) { 577 teammate_list = blue_team_member_list_; 578 opponent_list = yellow_team_member_list_; 579 } else { 580 opponent_list = blue_team_member_list_; 581 teammate_list = yellow_team_member_list_; 582 } 583 584 for(int i=0; i<teammate_list.size(); i++) { 585 RCRSVirtualSoccerEcoBe target = teammate_list.get(i); 586 if(target != ecobe) { 587 sb.append(createMemberTag("teamMate", target, ecobe)); 588 } 589 } 590 for(int i=0; i<opponent_list.size(); i++) { 591 RCRSVirtualSoccerEcoBe target = opponent_list.get(i); 592 sb.append(createMemberTag("opponent", target, ecobe)); 593 } 594 595 596 for(Iterator<String> it = flag_location_map_.keySet().iterator(); it.hasNext(); ) { 597 String key = it.next(); 598 sb.append(createFlagTag(key, ecobe, flag_location_map_.get(key))); 599 } 600 601 sb.append("</WorldData>"); 602 return sb.toString(); 603 } 604 605 public String createLocationTag(String key, EcoBe ecobe, double x, double y) { 606 double rx = x - ecobe.getLocation().getX(); 607 double ry = y - ecobe.getLocation().getY(); 608 double ecobe_angle = ecobe.getAngle(); 609 int angle = (int)((Math.atan2(ry, rx)-ecobe_angle)*180.0/Math.PI); 610 int dist = (int)(Math.sqrt(rx*rx + ry*ry)); 611 return "<"+key+">" 612 + "<dist>"+dist+"</dist>" 613 + "<angle>"+angle+"</angle>" 614 + "</"+key+">"; 615 616 } 617 public String createFlagTag(String key, EcoBe ecobe, Point2D.Double p) { 618 // EcoBe ecobe = client.getEcoBe(); 619 double x = p.getX() - ecobe.getLocation().getX(); 620 double y = p.getY() - ecobe.getLocation().getY(); 621 double ecobe_angle = ecobe.getAngle(); 622 int angle = (int)((Math.atan2(y, x)-ecobe_angle)*180/Math.PI); 623 int dist = (int)(Math.sqrt(x*x + y*y)); 624 return "<flag>" 625 + createKeyValueTag("id", key) 626 + createKeyValueTag("dist", dist) 627 + createKeyValueTag("angle", angle) 628 + "</flag>"; 629 } 630 public String createKeyValueTag(String key, double value) { 631 return createKeyValueTag(key, String.valueOf(value)); 632 } 633 public String createKeyValueTag(String key, int value) { 634 return createKeyValueTag(key, String.valueOf(value)); 635 } 636 public String createKeyValueTag(String key, String value) { 637 return "<"+key+">"+value+"</"+key+">"; 638 } 639 640 public String createMemberTag(String team, RCRSVirtualSoccerEcoBe ecobe, RCRSVirtualSoccerEcoBe me) { 641 double x = ecobe.getLocation().getX() - me.getLocation().getX(); 642 double y = ecobe.getLocation().getY() - me.getLocation().getY(); 643 double ecobe_angle = ecobe.getAngle(); 644 double me_angle = me.getAngle(); 645 int dist = (int)(Math.sqrt(x*x + y*y)); 646 int orientation = (int)(ecobe_angle-me_angle); 647 int angle = (int)((Math.atan2(y, x)-me_angle)*180.0/Math.PI); 648 //System.out.println("member tag: angle:"+angle+"; dist:"+dist); 649 return "<"+team+">" 650 + createKeyValueTag("id", ecobe.getID()) 651 + createKeyValueTag("nickname", ecobe.getNickName()) 652 + createKeyValueTag("status", "found") 653 + createKeyValueTag("dist", dist) 654 + createKeyValueTag("angle", angle) 655 + createKeyValueTag("orientation", orientation) 656 +"</"+team+">"; 657 } 658 /* 659 public String createMemberTag(String team, SoccerClient client) { 660 EcoBe ecobe = client.getEcoBe(); 661 return "<"+team+">" 662 + createKeyValueTag("id", ecobe.getID()) 663 + createKeyValueTag("nickname", ecobe.getNickName()) 664 + createKeyValueTag("status", "found") 665 + createKeyValueTag("dist", (int)(Math.random()*100)) 666 + createKeyValueTag("angle", (int)(Math.random()*100)) 667 + createKeyValueTag("orientation", (int)(Math.random()*100)) 668 +"</"+team+">"; 669 } 670 */ 671 public static void alert(Object message) { 672 javax.swing.JOptionPane.showMessageDialog(null, message); 673 } 674 675 }