for (int i1 = 0; i1 < n1; i1++) { for (int i2 = 0; i2 < n2; i2++) { ... for (int iN = 0; iN < nN; iN++) { myFunction(i1, .., iN); } }}
public class MyTask extends JPPFTask { // initialization on the client side public MyTask(int i1, .., int iN) { ... } // execution on the ndoe side public void run() { Object o = myAtomicFunction(i1, .., iN); setResult(o); } private Object myAtomicFunction(int i1, .., int iN) { ... }}
public class MyTask extends JPPFTask { // initialization on the client side public MyTask(int i1Start, int i1End, int n2, ..., int nN) { ... } // execution on the ndoe side public void run() { Object[] myResults = new Object[i1End - i1Start + 1]; for (int i1 = i1Start; i1 <= i1End; i1++) { myResults[index - i1Start] = myFunction(index, i2, .., iN); } setResult(myResults); } private Object myAtomicFunction(int i1) { for (int i2 = 0; i2 < n2; i2++) { ... for (int iN = 0; iN < nN; iN++) { Object o = myAtomicFunction(i1, i2, .., iN); ... } } ... } private Object myAtomicFunction(int i1, .., int iN) { ... }}
char dictionary[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
String password = "ZZZZ"; String temp; char dictionary[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; for (int a = 0; a < dictionary.length; a++) { for (int b = 0; b < dictionary.length; b++) { for (int c = 0; c < dictionary.length; c++) { for (int d = 0; d < dictionary.length; d++) { temp = String.valueOf(dictionary[a]) + dictionary[b] + dictionary[c] + dictionary[d]; if (password.equals(temp)) { System.out.println(temp); } } } } }NetBeans prints out:run:ZZZZBUILD SUCCESSFUL (total time: 3 seconds)
In constructor, task gets "atmoment" string and "password" string. Password String is "ZZZZZZ". You'll see in Application runner code what is atmoment String.public void run() { String temp; char dictionary[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; String zero = atmoment.substring(0, 1); String one = atmoment.substring(1, 2); for (int a = 0; a < dictionary.length; a++) { for (int b = 0; b < dictionary.length; b++) { for (int c = 0; c < dictionary.length; c++) { for (int d = 0; d < dictionary.length; d++) { temp = zero + one + dictionary[a] + dictionary[b] + dictionary[c] + dictionary[d]; if (password.equals(temp)) { setResult(temp); } } } } } }
public JPPFJob createJob() throws Exception { // create a JPPF job String password = "ZZZZZZ"; char dictionary[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; JPPFJob job = new JPPFJob(); // give this job a readable unique id that we can use to monitor and manage it. job.setName("Brute-Force"); for (int i = 0; i < dictionary.length; i++){ for(int j = 0; j < dictionary.length ; j++){ atmoment = String.valueOf(dictionary[i]) + String.valueOf(dictionary[j]) + "0" + "0" + "0" + "0"; job.addTask(new TemplateJPPFTask(password, atmoment)); } } // start the job in suspended mode //job.getSLA().setSuspended(true); return job; }
long time = System.nanoTime();String password = "ZZZZ";char[] pwdChars = password.toCharArray();char[] temp = new char[pwdChars.length];for (int a = 0; a < dictionary.length; a++) { temp[0] = dictionary[a]; for (int b = 0; b < dictionary.length; b++) { temp[1] = dictionary[b]; for (int c = 0; c < dictionary.length; c++) { temp[2] = dictionary[c]; for (int d = 0; d < dictionary.length; d++) { temp[3] = dictionary[d]; count++; if (Arrays.equals(temp, pwdChars)) { time = System.nanoTime() - time; output("found in " + StringUtils.toStringDuration(time/1000000L)); return; } } } }}
public static void loopOptimized(final char[] password) { char[] temp = new char[password.length]; int[] indices = new int[password.length]; boolean b = loopOptimized(0, password.length-1, indices, temp, password);}public static boolean loopOptimized(final int currentDepth, final int maxDepth, final int[] indices, final char[] temp, final char[] pwdChars) { if (currentDepth < maxDepth) { for (indices[currentDepth] = 0; indices[currentDepth] < dictionary.length; indices[currentDepth]++) { temp[currentDepth] = dictionary[indices[currentDepth]]; if (loopOptimized(currentDepth + 1, maxDepth, indices, temp, pwdChars)) return true; } } else { for (indices[currentDepth] = 0; indices[currentDepth] < dictionary.length; indices[currentDepth]++) { temp[currentDepth] = dictionary[indices[currentDepth]]; if (Arrays.equals(temp, pwdChars)) return true; } } return false;}
public class ParallelLoopTask extends JPPFTask { private transient char[] dictionary; // retrieved from the DataProvider final int idx1; final int idx2; boolean found = false; public ParallelLoopTask(final int idx1, final int idx2) { this.idx1 = idx1; this.idx2 = idx2; } @Override public void run() { try { char[] pwd = (char[]) getDataProvider().getValue("password"); dictionary = (char[]) getDataProvider().getValue("dictionary"); int[] indices = new int[pwd.length]; indices[0] = idx1; indices[1] = idx2; char[] temp = new char[pwd.length]; temp[0] = dictionary[idx1]; temp[1] = dictionary[idx2]; found = loop(2, pwd.length-1, indices, temp, pwd); if (found) setResult(temp); } catch (Exception e) { setException(e); } } public boolean loop(final int currentDepth, final int maxDepth, final int[] indices, final char[] temp, final char[] pwd) { if (currentDepth < maxDepth) { for (indices[currentDepth] = 0; indices[currentDepth] < dictionary.length; indices[currentDepth]++) { temp[currentDepth] = dictionary[indices[currentDepth]]; if (loop(currentDepth + 1, maxDepth, indices, temp, pwd)) return true; } } else { for (indices[currentDepth] = 0; indices[currentDepth] < dictionary.length; indices[currentDepth]++) { temp[currentDepth] = dictionary[indices[currentDepth]]; if (Arrays.equals(temp, pwd)) return true; } } return false; }}
private static void submitJob(final char[] pwd) throws Exception { final JPPFJob job = new JPPFJob(); int length = dictionary.length; for (int i=0; i<length; i++) { for (int j=0; j<length; j++) { job.addTask(new ParallelLoopTask(i, j)); } } DataProvider dp = new MemoryMapDataProvider(); dp.setValue("dictionary", dictionary); dp.setValue("password", pwd); job.setDataProvider(dp); List<JPPFTask> results = jppfClient.submit(job);}
job.setResultListener(new JPPFResultCollector(job) { @Override public synchronized void resultsReceived(final TaskResultEvent event) { super.resultsReceived(event); if (event.getTaskList() != null) { for (JPPFTask task: event.getTaskList()) { if (task.getResult() != null) { cancelJob(job.getUuid()); output("found password: " + new String((char[]) task.getResult())); break; } } } }});
private static void cancelJob(final String jobUuid) { Runnable r = new Runnable() { @Override public void run() { try { jppfClient.cancelJob(jobUuid); } catch (Exception e) { e.printStackTrace(); } } }; new Thread(r).start();}
@Overridepublic void onCancel() { cancelFlag = true;}
private void cancelJob(final String jobUuid) throws Exception { Runnable r = new Runnable() { @Override public void run() { try { JMXNodeConnectionWrapper jmx = new JMXNodeConnectionWrapper(); jmx.connect(); jmx.cancelJob(jobUuid, false); jmx.close(); } catch(Exception e) { e.printStackTrace(); } } }; new Thread(r).start();}
public void run() { ... if (found) { setResult(temp); String uuid = (String) getDataProvider().getValue("uuid"); cancelJob(uuid); } ...}