Exemple #1
0
  /**
   * Creates a new, empty SessionDescription. The session is set as follows:
   *
   * <p>v=0
   *
   * <p>o=this.createOrigin ("user", InetAddress.getLocalHost().toString());
   *
   * <p>s=-
   *
   * <p>t=0 0
   *
   * @throws SdpException SdpException, - if there is a problem constructing the SessionDescription.
   * @return a new, empty SessionDescription.
   */
  public SessionDescription createSessionDescription() throws SdpException {
    SessionDescriptionImpl sessionDescriptionImpl = new SessionDescriptionImpl();

    ProtoVersionField ProtoVersionField = new ProtoVersionField();
    ProtoVersionField.setVersion(0);
    sessionDescriptionImpl.setVersion(ProtoVersionField);

    OriginField originImpl = null;
    try {
      originImpl =
          (OriginField) this.createOrigin("user", InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    sessionDescriptionImpl.setOrigin(originImpl);

    SessionNameField sessionNameImpl = new SessionNameField();
    sessionNameImpl.setValue("-");
    sessionDescriptionImpl.setSessionName(sessionNameImpl);

    TimeDescriptionImpl timeDescriptionImpl = new TimeDescriptionImpl();
    TimeField timeImpl = new TimeField();
    timeImpl.setZero();
    timeDescriptionImpl.setTime(timeImpl);
    Vector times = new Vector();
    times.addElement(timeDescriptionImpl);
    sessionDescriptionImpl.setTimeDescriptions(times);

    sessionDescriptionsList.addElement(sessionDescriptionImpl);
    return sessionDescriptionImpl;
  }
Exemple #2
0
  // 获得IP地址
  public static String getIpAddr(HttpServletRequest request) {
    String ipAddress = null;
    ipAddress = request.getHeader("x-forwarded-for");
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if (ipAddress.equals("127.0.0.1")) {
        // 根据网卡取本机配置的IP
        InetAddress inet = null;
        try {
          inet = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
          e.printStackTrace();
        }
        ipAddress = inet.getHostAddress();
      }
    }

    // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
      // = 15
      if (ipAddress.indexOf(",") > 0) {
        ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
      }
    }
    return ipAddress;
  }
Exemple #3
0
  private Descrittore contatta_server(String nome) {

    // variabili per l'RMI
    RMIServerInt serv = null; // server
    Descrittore descr_rit = null; // descrittore ritornato

    if (nome == null) {
      System.out.println("## contatta_server di Download ha ricevuto parametro null ! ");
      return null;
    }

    System.out.println("@ provo a contattare il server RMI ");
    // ################  RMI ################
    if (System.getSecurityManager() == null) {
      System.setSecurityManager(new SecurityManager());
    }

    Object o = null;
    try {
      // o = Naming.lookup("rmi://192.168.0.10:1099/srmi");
      Registry registry = LocateRegistry.getRegistry(server.getHostAddress());
      o = registry.lookup("srmi");

    } catch (RemoteException e) {
      System.out.println("## Problemi nell'RMI di Download - contatta_server di " + nome);
      e.printStackTrace();
    } catch (NotBoundException e) {
      System.out.println("## Problemi nell'RMI di Download - contatta_server di " + nome);
      e.printStackTrace();
    }

    if (o == null) {
      System.out.println(
          "## l'RMI di Download - contatta_server di " + nome + " ha ritornato l'oggetto o null");
      return null;
    }

    serv = (RMIServerInt) o;

    try {
      descr_rit = serv.lookup(nome, InetAddress.getLocalHost());
    } catch (RemoteException e) {
      e.printStackTrace();
      System.out.println("## Problemi con Lookup di " + nome);
      return null;

    } catch (UnknownHostException e) {
      e.printStackTrace();
      System.out.println("## Problemi con Lookup di " + nome);
      return null;
    }

    return descr_rit;
  }
Exemple #4
0
 public void Connect() {
   try {
     System.out.println("ok");
     socket99 = new Socket(serverIP, 9999);
     socket66 = new Socket(serverIP, 6666);
     socket88 = new Socket(serverIP, 8888);
     isConnected = true;
   } catch (UnknownHostException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  public static void main(String[] args) throws EOFException {

    queue = new ArrayList<Traffic>();

    ObjectInputStream OISforTG = null;
    ObjectOutputStream OOSforLot = null;

    System.out.print("login?");
    sc = new Scanner(System.in);
    String input = sc.nextLine();
    if (input.equalsIgnoreCase("y") || input.equalsIgnoreCase("yes")) {
      // System.out.println(ConnectionData.ipTG+" "+ConnectionData.portForTG);
      try {
        // socketForManager = new Socket(ConnectionData.ipMani, ConnectionData.portForManager);
        socketForTG = new Socket(ConnectionData.ipTG, ConnectionData.portForTG);
        socketForTime = new Socket(ConnectionData.ipTG, ConnectionData.portForTime);
        socketForParkingLot = new Socket(ConnectionData.ipLot, ConnectionData.portForLot);
        System.out.println("Connected");
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }

      receiverForManager RFM = new receiverForManager();
      Thread ThreadRFM = new Thread(RFM);
      ThreadRFM.start();

      trafficReceiver trafficRec = new trafficReceiver();
      Thread ThreadTraffic = new Thread(trafficRec);
      ThreadTraffic.start();

      try {
        OOSforLot = new ObjectOutputStream(socketForParkingLot.getOutputStream());
      } catch (IOException e) {
        e.printStackTrace();
      } catch (NullPointerException npe) {
        npe.printStackTrace();
      }

      timeReader TR = new timeReader();
      Thread thread = new Thread(TR);
      thread.start();

      carReturned CR = new carReturned();
      Thread thread1 = new Thread(CR);
      thread1.start();
      // System.out.println("reach1");
      while (true) {
        try {
          if (Token > 0 && queue.size() > 0) {
            Token--;
            System.out.println(Token);
            OOSforLot.writeObject(queue.get(0));
          } else if (Token == 0 && queue.size() > 0) {
            String carOrig = queue.get(0).getGeneratedTime();
            Integer carOrigInt[] = utils.timeConverter(carOrig);
            int Orig = (carOrigInt[0] * 3600) + (carOrigInt[1] * 60);
            int Curren = (currentTime[0] * 3600) + (currentTime[1] * 60);

            if ((Curren - Orig) > 600) {
              // need to something...
              //
              //
              //
              //
            } else {
              System.out.println("Token: " + Token + " queue Size: " + queue.size());
              continue;
            }
          }

        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  /**
   * connect: connect to the master
   *
   * @return: true for success and false for failure
   */
  protected boolean connect() {

    boolean connected = true;
    try {
      _sock = new Socket(_serverIP, _serverPort);
      _out = new PrintWriter(_sock.getOutputStream(), true);
      _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
      String reply = sendAll();
      if (!reply.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        /* Tokenize */
        StringTokenizer st = new StringTokenizer(reply);
        if (st.nextToken().equals(ProtocolConstants.PACK_STR_ERRMES_HEAD)) {
          _elog(st.nextToken());
          return false;
        }
      }

      _userSock = new Socket(_serverIP, _serverPort);
      _userOut = new PrintWriter(_userSock.getOutputStream(), true);
      _userIn = new BufferedReader(new InputStreamReader(_userSock.getInputStream()));
      String replyUser = sendUserID();
      if (!replyUser.equals(ProtocolConstants.PACK_STR_CONFIRM_HEAD)) {
        throw new Exception("Not confirmed");
      }
    } catch (UnknownHostException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (IOException e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    } catch (Exception e) {
      if (!_server.noException()) {
        _elog(e.toString());
      }
      if (_server.debugMode()) {
        e.printStackTrace();
      }
      connected = false;
    }
    if (!connected) {
      return connected;
    }

    if (!connected) {
      /* Remove the pair of socket */
      _sock = null;
      _in = null;
      _out = null;

      _userSock = null;
      _userOut = null;
      _userIn = null;
    }
    return connected;
  }
Exemple #7
0
  private ArrayList<InetAddress> contatta_Tracker(Descrittore descr) {

    InetAddress server = null;
    ObjectInputStream data_in;
    ObjectOutputStream data_out;
    InputStream in;
    OutputStream out;
    ArrayList<InetAddress> lista = null;
    /* socket per la comunicazione TCP */
    SSLSocketFactory factory = null;
    SSLSocket socket = null;

    // ###### preparo l'indirizzo
    try {
      server = InetAddress.getByName("192.168.0.10");
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    }

    // ###### creo il socket

    if (descr == null) {
      System.out.println("## contatta_tracker di Download ha ricevuto parametro null ! ");
      return null;
    }

    System.out.println(
        "@ provo a contattare via TCP il Tracker a "
            + server.toString()
            + " porta "
            + descr.getTcp());

    try {
      factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
      socket = (SSLSocket) factory.createSocket(server, descr.getTcp());
    } catch (IOException e1) {
      System.out.println("# eccezione nella creazione socket verso il Tracker TCP");
      e1.printStackTrace();
    }

    System.out.println("@ socket creato ");

    if (factory == null || socket == null) {
      System.out.println("## la connessione ssl di Download ha ritornato null ! ");
      return null;
    }

    try {
      System.out.println("@ out_stream ");
      out = socket.getOutputStream();
      System.out.println("@ out ");
      data_out = new ObjectOutputStream(out);
      System.out.println("@ in_stream ");
      in = socket.getInputStream();
      System.out.println("@ in ");
      data_in = new ObjectInputStream(in);

      System.out.println("@ Downloader:  streams ok  ");

      data_out.writeObject(descr); // TODO Perchè ?
      data_out.flush();

      System.out.println("@ descrittore mandato  ");

      lista = (ArrayList<InetAddress>) data_in.readObject();
      if (lista == null)
        System.out.println(
            "@ ritornata lista vuota dal tracker riguardo :  "
                + descr.nome); // TODO sarebbe un caso plausibile
      else System.out.println("@ lista ricevuta :  " + lista.toString());

    } catch (IOException e) {
      System.out.println("# eccezione I/O nella creazione socket verso il Tracker TCP");
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      System.out.println("# eccezione C N F nella creazione socket verso il Tracker TCP");
      e.printStackTrace();
    }

    return lista;
  }