public static StreamConnection getConnectionForRequest(String url) {
    final ConnectionFactory connectionFactory = new ConnectionFactory();

    // Remove any transports that are not currently available.
    if (isTransportManaged == false) {
      for (int i = 0; i < availableTransportTypes.length; i++) {
        int transport = availableTransportTypes[i];
        if (!TransportInfo.isTransportTypeAvailable(transport)
            || !TransportInfo.hasSufficientCoverage(transport)) {
          Arrays.removeAt(availableTransportTypes, i);
        }
      }
      isTransportManaged = true;
    }

    connectionFactory.setPreferredTransportTypes(availableTransportTypes);

    final ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(url);
    StreamConnection connection = null;
    if (connectionDescriptor != null) {
      // connection suceeded
      int transportUsed = connectionDescriptor.getTransportDescriptor().getTransportType();
      System.out.println("Transport type used: " + Integer.toString(transportUsed));
      connection = (StreamConnection) connectionDescriptor.getConnection();
    }
    return connection;
  }
  public HTTPConnManager(
      final int type,
      final String http,
      BigVector _postData,
      final int handler,
      final HTTPAnswerListener _listener) {
    listener = _listener;
    postData = _postData;
    // Create a preference-ordered list of transports.
    int[] _intTransports = {
      TransportInfo.TRANSPORT_TCP_WIFI,
      TransportInfo.TRANSPORT_BIS_B,
      TransportInfo.TRANSPORT_MDS,
      TransportInfo.TRANSPORT_WAP2,
      TransportInfo.TRANSPORT_TCP_CELLULAR
    };

    // Remove any transports that are not currently available.
    for (int i = 0; i < _intTransports.length; i++) {
      int transport = _intTransports[i];
      if (!TransportInfo.isTransportTypeAvailable(transport)
          || !TransportInfo.hasSufficientCoverage(transport)) {
        Arrays.removeAt(_intTransports, i);
      }
    }

    // Set ConnectionFactory options.
    if (_intTransports.length > 0) {
      _factory.setPreferredTransportTypes(_intTransports);
    }

    _factory.setAttemptsLimit(5);

    // Open a connection on a new thread.
    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                DebugStorage.getInstance().Log(0, "HTTPConnManager: open connection to " + http);
                ConnectionDescriptor cd = _factory.getConnection(http);
                if (cd != null) {
                  DebugStorage.getInstance().Log(0, "HTTPConnManager: connection created.");
                  Connection c = cd.getConnection();
                  new HTTPOut(type, postData, c, handler, listener);
                } else {
                  DebugStorage.getInstance()
                      .Log(0, "HTTPConnManager: UNABLE TO CREATE CONNECTION!");
                }
              }
            });
    t.start();
  }
Beispiel #3
0
  /** Reads the remote service (http lookup) and send updated data to JMS bus */
  @Scheduled(fixedRate = 1000)
  private void onTick() {

    // Get transport positions from the source
    String positions = getTransportPositions();

    // Connectivity OK, let's go with agent processing
    if (positions == null || positions.isEmpty()) {
      logger.warn("No transport positions readed!");
      return;
    }

    String[] lines = positions.split(LINE_SEPARATOR);

    if (lines.length == 0) {
      logger.warn("Could not split different lines from positions result string");
      return;
    }

    String[] attr;
    float latitude, longitude;
    int orientation;

    for (int i = 0; i < lines.length; i++) {

      attr = lines[i].split(FIELD_SEPARATOR);

      if (attr.length < 5) {
        logger.warn("Could not split different fields with ';' from line result: {}", lines[i]);
        continue;
      }

      latitude = 0f;
      longitude = 0f;
      orientation = 0;
      String id = EMPTY_IDENTIFIER;
      String line = EMPTY_IDENTIFIER;

      try {
        latitude = Float.valueOf(attr[3]);
        longitude = Float.valueOf(attr[2]);
        line = attr[1];
        id = attr[0];
        orientation = Integer.valueOf(attr[4]);
      } catch (Exception e) {
        logger.warn(
            "Could not parse result string for position data: {}. Exception: {}", positions, e);
        continue;
      }

      int mod = orientation % 45;
      if (mod < 23) orientation -= mod;
      else orientation += (45 - mod);

      if (orientation == 360) orientation = 0;

      boolean changed = false;

      TransportInfo transportInfo;
      if (transports.containsKey(id)) {
        transportInfo = transports.get(id);

        if (transportInfo.isOrientationDiferent(orientation)
            || transportInfo.isPositionDiferent(latitude, longitude)
            || transportInfo.isLineDiferent(line)) changed = true;

      } else {
        transportInfo = new TransportInfo();
        transportInfo.setId(id);
        transportInfo.setLine(line);
        transports.put(id, transportInfo);
        changed = true;
      }

      if (changed) {
        transportInfo.setLatitude(latitude);
        transportInfo.setLongitude(longitude);
        transportInfo.setOrientation(orientation);
        transportInfo.setLastupdate(System.currentTimeMillis());
        if (!transportInfo.isLineDiferent(line)) {
          line = EMPTY_IDENTIFIER;
        } else {
          transportInfo.setLine(line);
        }

        // Send JMS Message
        sendBusPosition(line, id, latitude, longitude, orientation);
      }
    }

    Vector<String> idsToRemove = new Vector<String>();
    for (TransportInfo info : transports.values()) {
      if ((System.currentTimeMillis() - info.getLastupdate()) > UPDATE_TIMEOUT) {
        idsToRemove.add(info.getId());
      }
    }

    for (String id : idsToRemove) {
      transports.remove(id);
    }
  }