/** * @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(); } } }
/** * 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); } }