Ejemplo n.º 1
0
 public void setType(Class type) {
   if (type == null || type == Object.class) this.type = null;
   else if (isEmpty()) this.type = type;
   else {
     type = Data.commonType(type, getCommonType());
     if (type != null) this.type = type;
   }
 }
Ejemplo n.º 2
0
 public Class getCommonType() {
   Class t = null;
   Iterator i = iterator();
   while (i.hasNext()) {
     Object e = i.next();
     if (e == null) continue;
     else if (t == null) t = e.getClass();
     else {
       t = Data.commonType(t, e.getClass());
       if (t == null || t == Object.class) return null;
     }
   }
   return t;
 }
  public static void main(String[] args) throws IOException {

    int n;
    String aux[] = new String[32];
    int linie, coloana;
    Data.setACC(10);
    Data.setBRK(10);
    Data.setMaxSPD(100);
    Data.setgetSteeringCar(0.5);
    try {
      // create socket
      socket = new Socket("127.0.0.1", 6666);
      // we attempt to bypass nagle's algorithm
      socket.setTcpNoDelay(true);

      // initialize our communication class
      comm = new NetworkCommunication(socket);
      comm.initStreams();

      // send initial packet, aka the team's name
      comm.writeInt(H_TEAM_NAME);
      comm.writeInt(1);
      comm.writeString(args[0]);
      // comm.writeString("void");
    } catch (UnknownHostException e) {
      System.out.println("could not connect to server");
      System.exit(1);
    } catch (IOException e) {
      System.out.println("No I/O");
      System.exit(1);
    }
    System.out.println("Sent team name....entering main loop");

    while (true) {
      try {
        int header = comm.readInt();
        switch (header) {
          case H_MAP_DIM: // 2
            {
              Data.setMapWidth(comm.readInt());
              Data.setMapHeigh(comm.readInt());
              Data.Map = new int[Data.getMapHeigh()][Data.getMapWidth()];

              int nr_data_recieve = Data.getMapHeigh() * Data.getMapWidth() / 32 + 1;
              for (int i = 0; i < nr_data_recieve; i++) {
                n = comm.readInt();
                for (int j = 0; j < 32; j++) {
                  if ((n & (1 << j)) == 0) aux[31 - j] = "0";
                  else aux[31 - j] = "1";
                }
                for (int k = 0; k < 32; k++) {
                  linie = (32 * i + k) / Data.getMapWidth();
                  coloana = (32 * i + k) - (linie * Data.getMapWidth());
                  if (Data.getMapHeigh() > linie && Data.getMapWidth() > coloana)
                    Data.Map[linie][coloana] = Integer.parseInt(aux[k]);
                }
              }

              System.out.println("Received map dimentions packet");
            }
            ;
            break;

          case H_INITIAL_INFO: // 3
            {
              Data.setStartPointX(comm.readInt());
              Data.setStartPointY(comm.readInt());
              Data.setWidth_meters(comm.readInt());
              Data.setHeight_meters(comm.readInt());
              Data.setDirection(comm.readInt());
              Data.setNumberOfLaps(comm.readInt());
              Data.setMaximumLapTime(comm.readInt());
              Data.setCarAngle(comm.readDouble());

              // trimite-m la server car configuration
              comm.writeInt(H_CAR_CONFIG);
              comm.writeInt(Data.getACC());
              comm.writeInt(Data.getBRK());
              comm.writeInt(Data.getMaxSPD());
              comm.writeDouble(Data.getSteeringCar());

              System.out.println("Received initial information packet");
            }
            ;
            break;

          case H_CAR_CONFIRM: // 4
            {
              Data.setACC(comm.readInt());
              Data.setBRK(comm.readInt());
              Data.setMaxSPD(comm.readInt());
              Data.setgetSteeringCar(comm.readDouble());
              System.out.println("Received car confirm packet");

              // System.out.println(Data.getACC()+" "+Data.getBRK()+" "+Data.getSteeringCar());

              // pornim masina :))
              Engine.StartPosition(100, 0, Data.getCarAngle());

              comm.writeInt(H_DRIVE_INFO);
              comm.writeDouble(Data.getAccelerationPercentage());
              comm.writeDouble(Data.getBrakePercentage());
              comm.writeDouble(Data.getWantedAngle());
              comm.writeInt(0); // drop queue

              System.out.println("Send driving info1");
            }
            ;
            break;

          case H_POS_CONFIRM: // 6
            {
              Data.setCurrent_X_Meters(comm.readDouble());
              Data.setCurrent_Y_Meters(comm.readDouble());
              Data.setCurrentSpeed(comm.readDouble());
              Data.setCurrentAngleInRadians(comm.readDouble());
              Data.setCurrentDirection(comm.readInt());
              System.out.println("Received position confirmation packet");
              // System.out.println(Data.getCurrent_X_Meters()+"-"+Data.getCurrent_Y_Meters()+"pozitia masinii");
              // System.out.println(Data.getCurrentSpeed()+"-"+Data.getCurrentDirection()+"-"+Engine.FromRadiusToAngle(Data.getCurrentAngleInRadians()));

              Engine.evalPosition();

              comm.writeInt(H_DRIVE_INFO);
              comm.writeDouble(Data.getAccelerationPercentage());
              comm.writeDouble(Data.getBrakePercentage());
              comm.writeDouble(Data.getWantedAngle());
              comm.writeInt(0); // drop queue

              // System.out.println("Send driving info2");

            }
            ;
            break;

          case H_END_RACE:
            {
              System.out.println("Received end race packet");
            }
            ;
            break;

          default:
            {
              System.out.println("Unknown packet");
              // System.out.println(header);
            }
        }
      } catch (IOException e) {
        System.out.println("No I/O");
        System.exit(1);
      }
    }
  }
Ejemplo n.º 4
0
  /**
   * Process an HTML get or post.
   *
   * @exception ServletException From inherited class.
   * @exception IOException From inherited class.
   */
  public void scanOutXML(
      PrintWriter out,
      String strDirectory,
      String strFilename,
      String[] strPlus,
      String[] strMinus,
      boolean bExcludeParams,
      boolean bAnalyzeParams)
      throws IOException {
    File dir = new File(strDirectory + '/' + strFilename);
    if (dir.isDirectory()) return;

    try {
      FileReader is = new FileReader(strDirectory + '/' + strFilename);
      BufferedReader r = new BufferedReader(is);
      String string = null;
      Hashtable ht = new Hashtable();
      Set setstrExtensions = new HashSet();
      int iCount = 0;
      int iBytes = 0;
      while ((string = r.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(string, " \"", false);
        Data data = new Data();
        int iTokenCount = 0;
        while (st.hasMoreTokens()) {
          iTokenCount++;
          string = st.nextToken();
          if (iTokenCount == IP) data.m_IP = string;
          if (iTokenCount == URL) {
            if (bExcludeParams)
              if (string.indexOf('?') != -1) string = string.substring(0, string.indexOf('?'));
            if (bAnalyzeParams)
              if (string.indexOf('?') != -1) string = string.substring(string.indexOf('?') + 1);
            data.m_URL = string;
          }
          if (iTokenCount == PROTOCOL)
            if (!string.startsWith("HTTP")) {
              data.m_URL += " " + string;
              iTokenCount--;
            }
          if (iTokenCount == BYTES) data.m_iBytes = Integer.parseInt(string);
        }
        if (!this.filterURL(data.m_URL, strPlus, strMinus, setstrExtensions)) continue;
        iCount++;
        iBytes += data.m_iBytes;
        if (ht.get(data.m_URL) == null) ht.put(data.m_URL, data);
        else {
          int iThisBytes = data.m_iBytes;
          data = (Data) ht.get(data.m_URL);
          data.m_iCount++;
          data.m_iBytes += iThisBytes;
        }
      }
      Comparator comparator = new Test();
      TreeMap tm = new TreeMap(comparator);
      Iterator iterator = ht.values().iterator();
      while (iterator.hasNext()) {
        Data data = (Data) iterator.next();
        tm.put(new Integer(data.m_iCount), data);
      }
      out.println("<file>");
      this.printXML(out, "directory", strDirectory);
      this.printXML(out, "name", strFilename);
      iterator = tm.values().iterator();
      while (iterator.hasNext()) {
        out.println("<data>");
        Data data = (Data) iterator.next();
        this.printXML(out, "url", data.m_URL);
        this.printXML(out, "count", Integer.toString(data.m_iCount));
        out.println("</data>");
      }
      this.printXML(out, "hits", Integer.toString(iCount));
      this.printXML(out, "bytes", Integer.toString(iBytes));
      this.printXML(out, "unique", Integer.toString(tm.size()));

      iterator = setstrExtensions.iterator();
      out.println("<extensions>");
      while (iterator.hasNext()) {
        this.printXML(out, "extension", (String) iterator.next());
      }
      out.println("</extensions>");

      out.println("</file>");
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }