001    package org.realobject;
002    
003    import java.awt.geom.*;
004    import java.net.*;
005    import org.util.xml.io.*;
006    import org.util.xml.element.*;
007    
008    public class RCRSVirtualSoccerEcoBe extends VirtualEcoBe {
009    
010            private Point2D.Double location_;
011            private InetSocketAddress remote_address_;
012            private long last_received_time_ = -1;
013            private long timeout_length_ = 1000;
014            private DatagramPacket packet_;
015            private String team_name_;
016            private String command_type_;
017            private double[] command_args_;
018            private long command_time_;
019            
020            public   RCRSVirtualSoccerEcoBe(int id) {
021                    super(id);
022            }
023            
024            public InetSocketAddress getRemoteAddress() {
025                    return remote_address_;
026            }
027    
028            public void setTeamName(String team_name) {
029                    team_name_ = team_name;
030            }
031            public String getTeamName() {
032                    return team_name_;
033            }
034    
035            public void setClient(InetSocketAddress remote_address) throws Exception {
036    System.out.println("set client: "+remote_address);
037    //if(remote_address_ != null) throw new Exception("this ecobe is already connected: "+remote_address);
038    if(remote_address_ != null) System.out.println("chenged new connection: "+remote_address);
039                    last_received_time_ = System.currentTimeMillis();
040                    packet_ = new DatagramPacket(new byte[0], 0, remote_address);
041                    remote_address_ = remote_address;
042            }
043            
044            public boolean isConnected() {
045                    if(last_received_time_ == -1) return false;
046                    long now = System.currentTimeMillis();
047                    return (now-last_received_time_)<timeout_length_;
048            }
049    
050            public void update(DatagramPacket packet) {
051    //System.out.println("update");
052                    last_received_time_ = System.currentTimeMillis();
053                    remote_address_ = (InetSocketAddress)packet.getSocketAddress();
054    //              System.out.println("now parse packet");
055                    String message = new String(packet.getData(), packet.getOffset(), packet.getLength());
056                    TagElement tag = null;
057                    try{
058                            tag = XMLIO.read(new StringBuffer(message));
059                            if(tag.getKey().equals("connect")) return;
060                            TagElement command = tag.getTagChildren()[0];
061                            if(command.getKey().equals("kick")) {
062                                    setCommand("kick", getDouble(command, "angle"), getDouble(command, "force"));
063                            } else if(command.getKey().equals("wheel_velocities")){
064                                    setCommand("wheel_velocities", getDouble(command, "left"), getDouble(command, "right"));
065                            }
066                    }catch(Exception e){
067                            e.printStackTrace();
068                            System.err.println("cannot parse: "+tag);
069                    }
070            }
071            public String getCommandType() {
072                    return command_type_;
073            }
074            public double[] getCommandArgs() {
075                    return command_args_;
076            }
077            public void setCommand(String type, double... args) {
078                    command_time_ = System.currentTimeMillis();
079                    command_type_ = type;
080                    command_args_ = args;
081            }
082            public double getDouble(TagElement tag, String key) {
083                    return Double.parseDouble(tag.getChildValue(key));
084            }
085            public String createPacket() {
086                    return null;
087            }
088            public DatagramPacket createPacket(String message) {
089                    byte[] send_buf = message.getBytes();
090                    packet_.setData(send_buf);
091                    return packet_;
092            }
093    }