/**
   * Fetch participants of TaskUpdate. Is called on creation of a new instance of TaskUpdate.
   *
   * @param repoUid the specified repository's identifier.
   * @return new Object[] with length of 2. The first item is a vector of Links that content is
   *     containing ComputerRemotes that are online and participating at the specified repository.
   *     <p>The second entry contains a link between the local computer and the specified
   *     repository.
   */
  private Object[] fetchTaskupdateParticipants(String repoUid) {

    Vector<Link> vec_parti = new Vector<Link>();
    Link ownRepoLink = null;

    for (int j = 0; j < combination.size(); j++) {

      final Link secondLnk = getCombination().get(j);

      if (secondLnk.getRepository().getUid().equals(repoUid)) {

        // inform the specified computer about the
        // changes
        final Computer currentComputer = secondLnk.getComputer();
        if (!currentComputer.matches(ComputerLocal.this)
            && currentComputer instanceof ComputerRemote) {

          vec_parti.addElement(secondLnk);
        } else if (currentComputer.getUid().equals(getUid())) {
          ownRepoLink = secondLnk;
        }
      }
    }

    return new Object[] {vec_parti, ownRepoLink};
  }
    /**
     * @author Julius Huelsmann
     * @version %I%, %U%
     * @since 1.0
     */
    public void run() {

      while (!isInterrupted()) {

        if (taskList.isEmpty() && getCombination().isEmpty()) {
          try {
            Thread.sleep(2000);
          } catch (InterruptedException e) {
            interrupt();
          }
        }

        //
        //
        // Check if the connection is established to all the RemoteComputers
        // that are listed as online
        final double currentTime = System.currentTimeMillis();
        for (int i = 0; i < computer.size(); i++) {
          final double dueTime = computer.get(i).getDueContact();
          final double connectionTime = computer.get(i).getContactionThreshold();
          if (computer.get(i).isConnected()) {
            if (currentTime > connectionTime) {
              computer.get(i).setConnected(false);
              Staticio.unskipPort(computer.get(i).getPort());

              setChanged();
              notifyObservers(new Object[] {View.ID_UPDATE_COMPUTER, computer});
            } else if (currentTime > dueTime) {
              computer.get(i).sayHi(ComputerLocal.this);
              System.out.println("sayin hi");

            } else {
              System.out.println("hier" + (currentTime - computer.get(i).getLastContact()));
            }
          }
        }

        final int taskListSize = taskList.size();
        final int totalNumber = taskList.size() + getCombination().size();

        final String identifier =
            "GitThreadRun" + new Random().nextDouble() + " " + System.currentTimeMillis();

        taskLock.add(identifier);
        if (!taskLock.get(0).equals(identifier)) {

          View.print(
              "DEBUG: taskLock.get(0) equalt nicht identifier!"
                  + taskLock.get(0)
                  + ".."
                  + identifier);
          try {
            Thread.sleep(200);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          continue;
        }

        for (int i = 0; i < taskList.size(); i++) {
          setPercentage(1.0 * i / totalNumber);
          if (taskList.get(i).isReadyPerformOperation()) {
            removeTask(i);
            i--;
          }
        }

        setChanged();
        notifyObservers(new Object[] {View.ID_UPDATE_TASKS, taskList, taskListfinished});

        for (int i = 0; i < getCombination().size(); i++) {

          setPercentage(1.0 * (taskListSize + i) / totalNumber);
          //
          // if the current combination contains a repository which
          // is contained by the local Computer check whether changes
          // occurred. If that is the case, send information message
          // to all participants of the repository.
          final Link currentLink = getCombination().get(i);
          final Repository repo = currentLink.getRepository();
          final double timestamp = System.currentTimeMillis();
          if (currentLink.getComputer().getUid().equals(getUid())) {

            final String repoPath = currentLink.getPath();
            final String commitMsg = "auto" + timestamp;
            if (Utils.callScript(
                Constants.UNIX_SRC_GIT_COMMIT,
                Constants.UNIX_DEST_GIT_COMMIT.getAbsolutePath(),
                repoPath + " " + commitMsg,
                true)) {

              //
              // Inform the user about the changes
              View.print("git changes occurred at " + repoPath);

            } else {

              continue;
            }

            // Find all the computers connected ot the
            final Vector<Link> vec_parti =
                (Vector<Link>) fetchTaskupdateParticipants(repo.getUid())[0];

            // important: initialize TaskUpdate before sending
            // signal.
            TaskUpdate tu = new TaskUpdate(vec_parti, currentLink, ComputerLocal.this);
            if (!tu.add(
                TaskUpdate.generateInformationString(
                    repo.getUid(), ComputerLocal.this.getUid(), "" + timestamp))) {

              View.print(
                  "Error: new created task does not accept "
                      + "information on the local computer.");
            }
            taskList.add(tu);

            //
            // Inform every client that is online to update the repository.
            for (int j = 0; j < vec_parti.size(); j++) {

              ((ComputerRemote) vec_parti.get(j).getComputer())
                  .repositoryUpdated(getUid(), repo.getUid(), timestamp);
            }
          }
        }

        removeTasklock(identifier);
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          View.print("interrupted");
          interrupt();
        }
      }
    }
  /**
   * @param xrepository
   * @param xpath
   * @return
   * @author Julius Huelsmann
   * @version %I%, %U%
   * @since 1.0
   */
  public String becomeRepositorymember(final Repository xrepository, final String xpath) {

    if (combination.size() == 0) {
      return "ERROR: no combination found.";
    }

    Link linkSelected = null;
    int index = 0;
    do {
      linkSelected = combination.get(index);
      index++;
    } while ((!linkSelected.getRepository().matches(xrepository)
            || linkSelected.getComputer() instanceof ComputerLocal
            || !((ComputerRemote) linkSelected.getComputer()).isConnected())
        && index < combination.size());

    if (linkSelected == null
        || !linkSelected.getRepository().matches(xrepository)
        || linkSelected.getComputer() instanceof ComputerLocal
        || !((ComputerRemote) linkSelected.getComputer()).isConnected()) {
      return "ERROR: No link between a computer that is online and the "
          + "specified repository found. Try again later.";
    }

    /*
     *  $1  the repository's root directory,
     *  $2  the repository's name.
     *  $3  the remote repository's url and path
     */
    final String computerUrl = ((ComputerRemote) linkSelected.getComputer()).getAddress();
    // TODO: hard-coded git user. change to git eitherway
    final String parameter =
        xpath
            + " "
            + linkSelected.getRepository().getName()
            + " juli@"
            + computerUrl
            + ":"
            + linkSelected.getPath();

    //
    // create directory to which is cloned
    new File(xpath).mkdirs();

    //
    //
    final boolean b =
        Utils.callScript(
            Constants.UNIX_SRC_GIT_CLONE,
            Constants.UNIX_DEST_GIT_CLONE.getAbsolutePath(),
            parameter,
            true);

    if (b) {

      //
      // Add new link to the vector of links and inform the remote computers
      // the changes.
      combination.add(new Link(xpath, this, xrepository));
      broadcastInformation();

      return "Successfully cloned repository from remote computer\n"
          + "remote ID  :\t"
          + linkSelected.getComputer().getUid()
          + "\n"
          + "remote IP  :\t"
          + computerUrl
          + "\n"
          + "remote path:\t"
          + linkSelected.getPath()
          + "\n"
          + "Destination:\t"
          + xpath
          + "."
          + "\n"
          + "Callparam. :\t"
          + parameter;

    } else {
      return "Failed to clone repository from remote computer\n"
          + "remote ID  :\t"
          + linkSelected.getComputer().getUid()
          + "\n"
          + "remote IP  :\t"
          + computerUrl
          + "\n"
          + "remote path:\t"
          + linkSelected.getPath()
          + "\n"
          + "Destination:\t"
          + xpath
          + "."
          + "\n"
          + "Callparam. :\t"
          + parameter;
    }
  }