/**
   * 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;
    }
  }
  /**
   * Create new instance of TaskUpdate from received message
   *
   * @param xmessageRest
   * @return
   * @author Julius Huelsmann
   * @version %I%, %U%
   * @since 1.0
   */
  public final synchronized boolean newTaskUpdate(final String xmessageRest) {

    //
    // Enable task-lock.
    final String identifier =
        "newTaskUpdate" + new Random().nextDouble() + " " + System.currentTimeMillis();
    taskLock.add(identifier);

    //
    // In case the task is not locked, continue. Otherwise go to else clause
    // that waits for a few milliseconds and recalls the function afterwards.
    if (taskLock.get(0).equals(identifier)) {

      //
      // Check if task already exists that is working with the same repository
      // and add string to the task. May occur here due to task-lock-delay.
      if (forewardTaskMessage(xmessageRest)) {
        return true;
      }

      //
      // Here it is clear that the task does not exist and that a new task has
      // to be created. Thus save the current time as an identifier of the
      // update time.
      double timestamp = System.currentTimeMillis();
      final String[] extractedInfo = TaskUpdate.fetchInformation(xmessageRest);
      final String repoUid = extractedInfo[0];

      //
      // Fetch participants of the new task for creating the task.
      Object[] o = fetchTaskupdateParticipants(repoUid);
      final Vector<Link> vec_parti = (Vector<Link>) o[0];
      final Link ownRepoLink = (Link) o[1];

      if (ownRepoLink != null && !vec_parti.isEmpty()) {

        final String repoPath = ownRepoLink.getPath();
        final String commitMsg = "auto" + timestamp;
        if (!Utils.callScript(
            Constants.UNIX_SRC_GIT_COMMIT,
            Constants.UNIX_DEST_GIT_COMMIT.getAbsolutePath(),
            repoPath + " " + commitMsg,
            true)) {
          timestamp = TaskUpdate.VALUE_NO_UPDATE;
        } else {
          View.print(
              getClass().getSimpleName()
                  + " Error. local computer "
                  + "out of sync?"
                  + ownRepoLink
                  + vec_parti.isEmpty());
        }
        final TaskUpdate tu = new TaskUpdate(vec_parti, ownRepoLink, this);
        taskList.add(tu);
        if (!tu.add(xmessageRest)) {
          View.print("ERROR in COMPUTERLOCAL COULD NOT ADD " + xmessageRest);

          final String newId = taskLock.remove(0);
          if (!newId.equals(identifier)) {
            View.print("Fatal error in newTaksupdate 1");
          }
          return false;
        }
        final String myself =
            Task.COMMUNIICATION_CHANGES
                + ownRepoLink.getRepository().getUid()
                + Utils.invalidCharLevel1
                + getUid()
                + Utils.invalidCharLevel1
                + timestamp;
        if (!tu.add(myself)) {

          View.print("ERROR in COMPUTERLOCAL COULD NOT ADD " + xmessageRest);

          final String newId = taskLock.remove(0);
          if (!newId.equals(identifier)) {
            View.print("Fatal error in newTaksupdate 2");
          }
          return false;
        }

        final String newId = taskLock.remove(0);
        if (!newId.equals(identifier)) {
          View.print("Fatal error in newTaksupdate 3");
        }
        return true;
      }

      // update participants

      final String newId = taskLock.remove(0);
      if (!newId.equals(identifier)) {
        View.print("Fatal error in newTaksupdate 4");
      }
      return false;
    } else {
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      return newTaskUpdate(xmessageRest);
    }
  }