protected boolean checkConnectionId(String client_address, long id) {
    try {
      random_mon.enter();

      Long key = new Long(id);

      connectionData data = (connectionData) connection_id_map.get(key);

      if (data == null) {

        // System.out.println( "TRTrackerServerProcessorUDP: rejected:" + id + ", data not found" );

        return (false);

      } else {

        if (SystemTime.getMonotonousTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

          return (false);
        }
      }

      boolean ok = data.getAddress().equals(client_address);

      // System.out.println( "TRTrackerServerProcessorUDP: tested:" + id + "/" + client_address + "
      // -> " + ok );

      return (ok);

    } finally {

      random_mon.exit();
    }
  }
  /** Stop the server. */
  public void stop() {
    try {
      this_mon.enter();

      if (server_channel != null) {
        try {
          server_channel.close();
          server_channel = null;
        } catch (Throwable t) {
          Debug.out(t);
        }
      }
    } finally {

      this_mon.exit();
    }
  }
  /** Start the server and begin accepting incoming connections. */
  public void start() {
    try {
      this_mon.enter();

      if (!isRunning()) {
        try {
          server_channel = ServerSocketChannel.open();

          server_channel.socket().setReuseAddress(true);
          if (receive_buffer_size > 0)
            server_channel.socket().setReceiveBufferSize(receive_buffer_size);

          server_channel.socket().bind(bind_address, 1024);

          if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "TCP incoming server socket " + bind_address));

          AEThread accept_thread =
              new AEThread("VServerSelector:port" + bind_address.getPort()) {
                public void runSupport() {
                  accept_loop();
                }
              };
          accept_thread.setDaemon(true);
          accept_thread.start();
        } catch (Throwable t) {
          Debug.out(t);
          Logger.log(
              new LogAlert(
                  LogAlert.UNREPEATABLE,
                  "ERROR, unable to bind TCP incoming server socket to " + bind_address.getPort(),
                  t));
        }

        last_accept_time = SystemTime.getCurrentTime(); // init to now
      }
    } finally {

      this_mon.exit();
    }
  }
Пример #4
0
  protected void activityFinished(boolean download, String item_name, Object relatedObject) {
    final String sound_enabler;
    final String sound_file;
    final String default_sound = "org/gudy/azureus2/ui/icons/downloadFinished.wav";

    final String speech_enabler;
    final String speech_text;

    final String popup_enabler;
    final String popup_def_text;

    if (download) {
      sound_enabler = "Play Download Finished";
      sound_file = "Play Download Finished File";

      speech_enabler = "Play Download Finished Announcement";
      speech_text = "Play Download Finished Announcement Text";

      popup_enabler = "Popup Download Finished";
      popup_def_text = "popup.download.finished";

    } else {
      sound_enabler = "Play File Finished";
      sound_file = "Play File Finished File";

      speech_enabler = "Play File Finished Announcement";
      speech_text = "Play File Finished Announcement Text";

      popup_enabler = "Popup File Finished";
      popup_def_text = "popup.file.finished";
    }

    try {
      this_mon.enter();

      if (COConfigurationManager.getBooleanParameter(popup_enabler)) {
        String popup_text = MessageText.getString(popup_def_text, new String[] {item_name});
        Logger.log(new LogAlert(relatedObject, true, LogAlert.AT_INFORMATION, popup_text));
      }

      if (Constants.isOSX) { // OS X cannot concurrently use SWT and AWT
        new AEThread("DownloadSound") {
          public void runSupport() {
            try {
              if (COConfigurationManager.getBooleanParameter(speech_enabler))
                Runtime.getRuntime()
                    .exec(
                        new String[] {
                          "say", COConfigurationManager.getStringParameter(speech_text)
                        }); // Speech Synthesis services

              if (COConfigurationManager.getBooleanParameter(sound_enabler))
                Runtime.getRuntime()
                    .exec(
                        new String[] {
                          "osascript", "-e", "beep"
                        }); // Beep alert type is in accordance with System Preferences

              Thread.sleep(2500);
            } catch (Throwable e) {
            }
          }
        }.start();
      } else if (COConfigurationManager.getBooleanParameter(sound_enabler, false)) {

        String file = COConfigurationManager.getStringParameter(sound_file);

        file = file.trim();

        // turn "<default>" into blank

        if (file.startsWith("<")) {

          file = "";
        }

        if (audio_clip == null || !file.equals(audio_resource)) {

          audio_clip = null;

          // try explicit file

          if (file.length() != 0) {

            File f = new File(file);

            try {

              if (f.exists()) {

                URL file_url = f.toURL();

                audio_clip = Applet.newAudioClip(file_url);
              }

            } catch (Throwable e) {

              Debug.printStackTrace(e);

            } finally {

              if (audio_clip == null) {
                Logger.log(
                    new LogAlert(
                        relatedObject,
                        LogAlert.UNREPEATABLE,
                        LogAlert.AT_ERROR,
                        "Failed to load audio file '" + file + "'"));
              }
            }
          }

          // either non-explicit or explicit missing

          if (audio_clip == null) {

            audio_clip =
                Applet.newAudioClip(UserAlerts.class.getClassLoader().getResource(default_sound));
          }

          audio_resource = file;
        }

        if (audio_clip != null) {

          new AEThread("DownloadSound") {
            public void runSupport() {
              try {
                audio_clip.play();

                Thread.sleep(2500);

              } catch (Throwable e) {

              }
            }
          }.start();
        }
      }
    } catch (Throwable e) {

      Debug.printStackTrace(e);

    } finally {

      this_mon.exit();
    }
  }
  protected long allocateConnectionId(String client_address) {
    try {
      random_mon.enter();

      long id = random.nextLong();

      Long new_key = new Long(id);

      connectionData new_data = new connectionData(client_address, id);

      // check for timeouts

      if (new_data.getTime() - last_timeout_check > 500) {

        last_timeout_check = new_data.getTime();

        Iterator<Long> it = connection_id_map.keySet().iterator();

        while (it.hasNext()) {

          Long key = it.next();

          connectionData data = connection_id_map.get(key);

          if (new_data.getTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

            // System.out.println( "TRTrackerServerProcessorUDP: connection id timeout" );

            it.remove();

            List<connectionData> cds = connection_ip_map.get(client_address);

            if (cds != null) {

              Iterator<connectionData> it2 = cds.iterator();

              while (it2.hasNext()) {

                if (it2.next().getID() == key) {

                  it2.remove();

                  break;
                }
              }

              if (cds.size() == 0) {

                connection_ip_map.remove(client_address);
              }
            }

          } else {
            // insertion order into map is time based - LinkedHashMap returns keys in same order

            break;
          }
        }
      }

      List<connectionData> cds = connection_ip_map.get(client_address);

      if (cds == null) {

        cds = new ArrayList<connectionData>();

        connection_ip_map.put(client_address, cds);
      }

      cds.add(new_data);

      if (cds.size() > 512) {

        connectionData dead = cds.remove(0);

        connection_id_map.remove(dead.getID());
      }

      connection_id_map.put(new_key, new_data);

      // System.out.println( "TRTrackerServerProcessorUDP: allocated:" + id + ", connection id map
      // size = " + connection_id_map.size());

      return (id);

    } finally {

      random_mon.exit();
    }
  }
  protected Map getVersionCheckInfoSupport(
      String reason, boolean only_if_cached, boolean force, boolean v6) {
    try {
      synchronized (listeners) {
        if (REASON_UPDATE_CHECK_START.equals(reason)) {
          startCheckRan = true;
        }
        for (VersionCheckClientListener l : listeners) {
          l.versionCheckStarted(reason);
        }
      }
    } catch (Throwable t) {
      Debug.out(t);
    }
    if (v6) {

      if (enable_v6) {

        try {
          check_mon.enter();

          long time_diff = SystemTime.getCurrentTime() - last_check_time_v6;

          force = force || time_diff > CACHE_PERIOD || time_diff < 0;

          if (last_check_data_v6 == null || last_check_data_v6.size() == 0 || force) {
            // if we've never checked before then we go ahead even if the "only_if_cached"
            // flag is set as its had not chance of being cached yet!
            if (only_if_cached && last_check_data_v6 != null) {
              return (new HashMap());
            }
            try {
              last_check_data_v6 =
                  performVersionCheck(constructVersionCheckMessage(reason), true, true, true);

              if (last_check_data_v6 != null && last_check_data_v6.size() > 0) {

                COConfigurationManager.setParameter("versioncheck.cache.v6", last_check_data_v6);
              }
            } catch (SocketException t) {
              // internet is broken
              // Debug.out(t.getClass().getName() + ": " + t.getMessage());
            } catch (UnknownHostException t) {
              // dns is broken
              // Debug.out(t.getClass().getName() + ": " + t.getMessage());
            } catch (Throwable t) {
              Debug.out(t);
              last_check_data_v6 = new HashMap();
            }
          } else {
            Logger.log(
                new LogEvent(
                    LOGID,
                    "VersionCheckClient is using "
                        + "cached version check info. Using "
                        + last_check_data_v6.size()
                        + " reply keys."));
          }
        } finally {
          check_mon.exit();
        }
      }

      if (last_check_data_v6 == null) last_check_data_v6 = new HashMap();

      return last_check_data_v6;

    } else {

      try {
        check_mon.enter();

        long time_diff = SystemTime.getCurrentTime() - last_check_time_v4;

        force = force || time_diff > CACHE_PERIOD || time_diff < 0;

        if (last_check_data_v4 == null || last_check_data_v4.size() == 0 || force) {
          // if we've never checked before then we go ahead even if the "only_if_cached"
          // flag is set as its had not chance of being cached yet!
          if (only_if_cached && last_check_data_v4 != null) {
            return (new HashMap());
          }
          try {
            last_check_data_v4 =
                performVersionCheck(constructVersionCheckMessage(reason), true, true, false);

            if (last_check_data_v4 != null && last_check_data_v4.size() > 0) {

              COConfigurationManager.setParameter("versioncheck.cache.v4", last_check_data_v4);
            }

            // clear down any plugin-specific data that has successfully been sent to the version
            // server

            try {
              if (AzureusCoreFactory.isCoreAvailable()) {

                // installed plugin IDs
                PluginInterface[] plugins =
                    AzureusCoreFactory.getSingleton().getPluginManager().getPluginInterfaces();

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

                  PluginInterface plugin = plugins[i];

                  Map data =
                      plugin
                          .getPluginconfig()
                          .getPluginMapParameter("plugin.versionserver.data", null);

                  if (data != null) {

                    plugin
                        .getPluginconfig()
                        .setPluginMapParameter("plugin.versionserver.data", new HashMap());
                  }
                }
              }
            } catch (Throwable e) {
            }
          } catch (UnknownHostException t) {
            // no internet
            Debug.outNoStack(
                "VersionCheckClient - " + t.getClass().getName() + ": " + t.getMessage());
          } catch (IOException t) {
            // General connection problem.
            Debug.outNoStack(
                "VersionCheckClient - " + t.getClass().getName() + ": " + t.getMessage());
          } catch (Throwable t) {
            Debug.out(t);
            last_check_data_v4 = new HashMap();
          }
        } else {
          if (Logger.isEnabled())
            Logger.log(
                new LogEvent(
                    LOGID,
                    "VersionCheckClient is using "
                        + "cached version check info. Using "
                        + last_check_data_v4.size()
                        + " reply keys."));
        }
      } finally {
        check_mon.exit();
      }

      if (last_check_data_v4 == null) last_check_data_v4 = new HashMap();

      last_feature_flag_cache_time = 0;

      return last_check_data_v4;
    }
  }