/** * openConnections: open the connection to the master. has retry mechanism * * @return: true if the connection is built, false if failed */ public boolean openConnections() { _log("Try to connect to Master..."); int i = 0; while (true) { boolean isConnected = connect(); if (!isConnected) { i++; if (i == DropboxConstants.MAX_TRY) { break; } _log("Cannot connect to Master, retry " + i); try { Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS); } catch (InterruptedException e) { if (!_server.noException()) { _elog(e.toString()); } if (_server.debugMode()) { e.printStackTrace(); } _log("Retry connection is interrupted"); break; } } else { _log("Success!"); return true; } } _log("Failed"); return false; }
public static void main(String[] args) { long head = 0; num_thread = Integer.parseInt(args[0]); long total = Long.parseLong(args[1]); section = total / ((long) num_thread); sectionHead = new long[num_thread]; long start = System.currentTimeMillis(); Thread[] threads = new Thread[num_thread]; for (int i = 0; i < num_thread; i++) { threads[i] = new Thread(new CoinFlip(i)); threads[i].start(); } for (int j = 0; j < num_thread; j++) { try { threads[j].join(); head = head + sectionHead[j]; } catch (InterruptedException e) { System.out.println( "Thread interrupted. Exception: " + e.toString() + " Message: " + e.getMessage()); return; } } long end = System.currentTimeMillis(); System.out.println(head + " heads in " + args[1] + " coin tosses"); System.out.println("Elapsed time: " + ((end - start)) + "ms"); }
/** listen: start to listen if there is any clients connected */ public void listen() { // Firstly spawn a new thread and then the main thread // also start listening _userNet = new DropboxFileServerUserNet(_server, _userOut, _userIn, _userSock); _userNet.start(); /* Use main thread, cannot be stopped */ while (_sock != null && !_sock.isClosed()) { try { String line = NetComm.receive(_in); _dlog(line); parse(line); } catch (Exception e) { if (!_server.noException()) { _elog(e.toString()); } break; // Break the loop } } /* Clear */ // Close main thread clear(); // Cancel the listening thread and retry _server.cancelListeningThread(); /* Also cancel all of the syncers */ _server.cancelSyncers(); // Retry after try { _log( "The connection to master is broken," + " reset everything and retry connection after " + DropboxConstants.TRY_CONNECT_MILLIS + " milliseconds"); Thread.sleep(DropboxConstants.TRY_CONNECT_MILLIS); } catch (InterruptedException e) { if (!_server.noException()) { _elog(e.toString()); } if (_server.debugMode()) { e.printStackTrace(); } } _server.run(); clear(); _log(_threadName + " is stopped"); }
// the method which actually copies the caches locally and unjars/unzips them // and does chmod for the files private static Path localizeCache( Configuration conf, URI cache, long confFileStamp, CacheStatus cacheStatus, boolean isArchive) throws IOException { FileSystem fs = getFileSystem(cache, conf); FileSystem localFs = FileSystem.getLocal(conf); Path parchive = null; if (isArchive) { parchive = new Path( cacheStatus.localizedLoadPath, new Path(cacheStatus.localizedLoadPath.getName())); } else { parchive = cacheStatus.localizedLoadPath; } if (!localFs.mkdirs(parchive.getParent())) { throw new IOException( "Mkdirs failed to create directory " + cacheStatus.localizedLoadPath.toString()); } String cacheId = cache.getPath(); fs.copyToLocalFile(new Path(cacheId), parchive); if (isArchive) { String tmpArchive = parchive.toString().toLowerCase(); File srcFile = new File(parchive.toString()); File destDir = new File(parchive.getParent().toString()); if (tmpArchive.endsWith(".jar")) { RunJar.unJar(srcFile, destDir); } else if (tmpArchive.endsWith(".zip")) { FileUtil.unZip(srcFile, destDir); } else if (isTarFile(tmpArchive)) { FileUtil.unTar(srcFile, destDir); } // else will not do anyhting // and copy the file into the dir as it is } long cacheSize = FileUtil.getDU(new File(parchive.getParent().toString())); cacheStatus.size = cacheSize; addCacheInfoUpdate(cacheStatus); // do chmod here try { // Setting recursive permission to grant everyone read and execute Path localDir = new Path(cacheStatus.localizedBaseDir, cacheStatus.uniqueParentDir); LOG.info("Doing chmod on localdir :" + localDir); FileUtil.chmod(localDir.toString(), "ugo+rx", true); } catch (InterruptedException e) { LOG.warn("Exception in chmod" + e.toString()); } // update cacheStatus to reflect the newly cached file cacheStatus.mtime = getTimestamp(conf, cache); return cacheStatus.localizedLoadPath; }
public String handle(String str, boolean forceDump) throws IOException { try { if (str != null && str.length() > 0) { String d = terminal.pipe(str); for (byte b : d.getBytes()) { in.write(b); } in.flush(); } } catch (IOException e) { closed = true; throw e; } try { return terminal.dump(10, forceDump); } catch (InterruptedException e) { throw new InterruptedIOException(e.toString()); } }
/** * A Test to guarantee that the Dancing UI bug will not rear its ugly head again. Basically, add a * component listener to the leftComponent of _docSplitPane and make certain its size does not * change while closing an OpenDefinitionsDocument outside the event thread. */ public void testDancingUIFileClosed() throws IOException { /** * Maybe this sequence of calls should be incorporated into one function createTestDir(), which * would get the username and create the temporary directory. Only sticky part is deciding where * to put it, in FileOps maybe? */ String user = System.getProperty("user.name"); _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); File forceOpenClass1_file = new File(_tempDir, "ForceOpenClass1.java"); String forceOpenClass1_string = "public class ForceOpenClass1 {\n" + " ForceOpenClass2 class2;\n" + " ForceOpenClass3 class3;\n\n" + " public ForceOpenClass1() {\n" + " class2 = new ForceOpenClass2();\n" + " class3 = new ForceOpenClass3();\n" + " }\n" + "}"; IOUtil.writeStringToFile(forceOpenClass1_file, forceOpenClass1_string); forceOpenClass1_file.deleteOnExit(); final ComponentAdapter listener = new ComponentAdapter() { public void componentResized(ComponentEvent event) { _testFailed = true; fail("testDancingUI: Open Documents List danced!"); } }; final SingleDisplayModelFileClosedListener closeListener = new SingleDisplayModelFileClosedListener(); _closeDone = false; Utilities.invokeAndWait( new Runnable() { public void run() { // _frame.setVisible(true); _frame.pack(); _frame.addComponentListenerToOpenDocumentsList(listener); _frame.open( new FileOpenSelector() { public File[] getFiles() { File[] return_me = new File[1]; return_me[0] = new File(_tempDir, "ForceOpenClass1.java"); return return_me; } }); _frame.getModel().addListener(closeListener); } }); Utilities.clearEventQueue(); /* Asynchronously close the file */ Utilities.invokeLater( new Runnable() { public void run() { _frame.getCloseButton().doClick(); } }); _log.log("Waiting for file closing"); synchronized (_closeLock) { try { while (!_closeDone) _closeLock.wait(); } catch (InterruptedException e) { fail(e.toString()); } } if (!IOUtil.deleteRecursively(_tempDir)) { System.err.println( "Couldn't fully delete directory " + _tempDir.getAbsolutePath() + "\nDo it by hand.\n"); } _log.log("testDancingUIFileClosed completed"); }
/** * A Test to guarantee that the Dancing UI bug will not rear its ugly head again. Basically, add a * component listener to the leftComponent of _docSplitPane and make certain its size does not * change while compiling a class which depends on another class. */ public void testDancingUIFileOpened() throws IOException { // System.out.println("DEBUG: Entering messed up test"); /** * Maybe this sequence of calls should be incorporated into one function createTestDir(), which * would get the username and create the temporary directory. Only sticky part is deciding where * to put it, in FileOps maybe? */ _log.log("Starting testingDancingUIFileOpened"); final GlobalModel _model = _frame.getModel(); String user = System.getProperty("user.name"); _tempDir = IOUtil.createAndMarkTempDirectory("DrJava-test-" + user, ""); File forceOpenClass1_file = new File(_tempDir, "ForceOpenClass1.java"); String forceOpenClass1_string = "public class ForceOpenClass1 {\n" + " ForceOpenClass2 class2;\n" + " ForceOpenClass3 class3;\n\n" + " public ForceOpenClass1() {\n" + " class2 = new ForceOpenClass2();\n" + " class3 = new ForceOpenClass3();\n" + " }\n" + "}"; File forceOpenClass2_file = new File(_tempDir, "ForceOpenClass2.java"); String forceOpenClass2_string = "public class ForceOpenClass2 {\n" + " inx x = 4;\n" + "}"; File forceOpenClass3_file = new File(_tempDir, "ForceOpenClass3.java"); String forceOpenClass3_string = "public class ForceOpenClass3 {\n" + " String s = \"asf\";\n" + "}"; IOUtil.writeStringToFile(forceOpenClass1_file, forceOpenClass1_string); IOUtil.writeStringToFile(forceOpenClass2_file, forceOpenClass2_string); IOUtil.writeStringToFile(forceOpenClass3_file, forceOpenClass3_string); forceOpenClass1_file.deleteOnExit(); forceOpenClass2_file.deleteOnExit(); forceOpenClass3_file.deleteOnExit(); _log.log("DancingUIFileOpened Set Up"); // _frame.setVisible(true); // set up listeners and signal flags final ComponentAdapter listener = new ComponentAdapter() { public void componentResized(ComponentEvent event) { _testFailed = true; fail("testDancingUI: Open Documents List danced!"); } }; final SingleDisplayModelFileOpenedListener openListener = new SingleDisplayModelFileOpenedListener(); final SingleDisplayModelCompileListener compileListener = new SingleDisplayModelCompileListener(); _openDone = false; Utilities.invokeAndWait( new Runnable() { public void run() { // _frame.setVisible(true); _frame.pack(); _frame.addComponentListenerToOpenDocumentsList(listener); } }); Utilities.clearEventQueue(); _model.addListener(openListener); _log.log("opening file"); Utilities.invokeLater( new Runnable() { public void run() { _frame.open( new FileOpenSelector() { public File[] getFiles() { File[] return_me = new File[1]; return_me[0] = new File(_tempDir, "ForceOpenClass1.java"); return return_me; } }); } }); Utilities.clearEventQueue(); /* wait until file has been open and active document changed. */ synchronized (_openLock) { try { while (!_openDone) _openLock.wait(); } catch (InterruptedException e) { fail(e.toString()); } } _model.removeListener(openListener); _log.log("File opened"); _compileDone = false; _model.addListener(compileListener); // save and compile the new file asynchronously Utilities.invokeLater( new Runnable() { public void run() { _log.log("saving all files"); _frame._saveAll(); _log.log("invoking compileAll action"); _frame.getCompileAllButton().doClick(); } }); Utilities.clearEventQueue(); synchronized (_compileLock) { try { while (!_compileDone) _compileLock.wait(); } catch (InterruptedException e) { fail(e.toString()); } } _log.log("File saved and compiled"); if (!IOUtil.deleteRecursively(_tempDir)) System.err.println( "Couldn't fully delete directory " + _tempDir.getAbsolutePath() + "\nDo it by hand.\n"); _log.log("testDancingUIFileOpened completed"); }