private String createUniqueFilename(String path) { String random = "" + System.nanoTime(); // just use the last 16 numbers if (random.length() > 16) random = random.substring(random.length() - 16); random += "" + new Random(System.currentTimeMillis()).nextInt(10000); return path + "\\DELETE_ME_" + random; }
public void handleStreamEvent(StreamEvent event) { if (event.eventType == StreamEvent.Type.STREAM_PREPARED) { SessionInfo session = ((StreamEvent.SessionPreparedEvent) event).session; sessionsByHost.put(session.peer, session); } else if (event.eventType == StreamEvent.Type.FILE_PROGRESS) { ProgressInfo progressInfo = ((StreamEvent.ProgressEvent) event).progress; // update progress Set<ProgressInfo> progresses = progressByHost.get(progressInfo.peer); if (progresses == null) { progresses = Sets.newSetFromMap(new ConcurrentHashMap<ProgressInfo, Boolean>()); progressByHost.put(progressInfo.peer, progresses); } if (progresses.contains(progressInfo)) progresses.remove(progressInfo); progresses.add(progressInfo); StringBuilder sb = new StringBuilder(); sb.append("\rprogress: "); long totalProgress = 0; long totalSize = 0; for (Map.Entry<InetAddress, Set<ProgressInfo>> entry : progressByHost.entrySet()) { SessionInfo session = sessionsByHost.get(entry.getKey()); long size = session.getTotalSizeToSend(); long current = 0; int completed = 0; for (ProgressInfo progress : entry.getValue()) { if (progress.currentBytes == progress.totalBytes) completed++; current += progress.currentBytes; } totalProgress += current; totalSize += size; sb.append("[").append(entry.getKey()); sb.append(" ").append(completed).append("/").append(session.getTotalFilesToSend()); sb.append(" (").append(size == 0 ? 100L : current * 100L / size).append("%)] "); } long time = System.nanoTime(); long deltaTime = Math.max(1L, TimeUnit.NANOSECONDS.toMillis(time - lastTime)); lastTime = time; long deltaProgress = totalProgress - lastProgress; lastProgress = totalProgress; sb.append("[total: ") .append(totalSize == 0 ? 100L : totalProgress * 100L / totalSize) .append("% - "); sb.append(mbPerSec(deltaProgress, deltaTime)).append("MB/s"); sb.append(" (avg: ") .append(mbPerSec(totalProgress, TimeUnit.NANOSECONDS.toMillis(time - start))) .append("MB/s)]"); System.out.print(sb.toString()); } }
/* * BE CAREFUL -- Don't introduce "Distributed Concurrency Bugs" * e.g. you have to make sure the filename is unique. * 1. create a remote file * 2. copy the token/auth stuff into it * 3. add the correct args to the remote commandline * Put the file in the same directory that nadmin lives in (lib) */ private void setupAuthTokenFile(List<String> cmd, List<String> stdin) throws WindowsException { WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(dcomInfo.getCredentials()); authTokenFilePath = dcomInfo.getNadminParentPath() + "\\token_" + System.nanoTime() + new Random().nextInt(1000); authTokenFilePath = createUniqueFilename(dcomInfo.getNadminParentPath()); authTokenFile = new WindowsRemoteFile(wrfs, authTokenFilePath); authTokenFile.copyFrom(stdin); cmd.add(AsadminInput.CLI_INPUT_OPTION); cmd.add(authTokenFilePath); }
public void run() { if (host == null) { try { host = InetAddress.getByName(hostname); } catch (UnknownHostException e) { System.err.println(hostname + ": Host name lookup failure"); return; } } final String hostSig = hostname + " (" + host + ")"; try { socket = new DatagramSocket(); socket.setSoTimeout(TIMEOUT); } catch (SocketException e) { e.printStackTrace(); return; } int seq = 1; while (true) { String sendString = generateSendString(seq); byte[] sendData = sendString.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, host, port); long sendTime = System.nanoTime(); long ping; try { socket.send(sendPacket); } catch (IOException e) { System.err.println("In sending ping request to " + hostSig + ": " + e); } byte[] receiveData = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); try { socket.receive(receivePacket); ping = System.nanoTime() - sendTime; System.out.println( receiveData.length + " bytes from " + hostSig + ": ping_seq=" + seq + " time=" + ping + "ns"); } catch (SocketTimeoutException e) { System.out.println("Timeout from " + hostSig); } catch (IOException e) { System.err.println("In receiving ping response from " + hostSig + ": " + e); } ++seq; --packets; if (packets == 0) break; try { Thread.sleep(DELAY); } catch (InterruptedException e) { System.err.println("Grr, my beauty sleep!"); } } }
protected long getTimestamp() { return server.timeService() != null ? server.timeService().timestamp() : System.nanoTime(); }
public static void main(String args[]) throws InterruptedException, IOException { int i, j; String serverInetAddress = "localhost"; String server1AddressString = "10.10.1.1"; InetAddress server1Address = InetAddress.getByName(server1AddressString); String server2AddressString = "10.10.2.2"; InetAddress server2Address = InetAddress.getByName(server2AddressString); String server3AddressString = "10.10.3.2"; InetAddress server3Address = InetAddress.getByName(server3AddressString); String server4AddressString = "localhost"; InetAddress server4Address = InetAddress.getByName(server4AddressString); DatagramSocket skt; { skt = new DatagramSocket(PORT_NUMBER_CLIENT); // socket used to listen and write InetAddress host = InetAddress.getByName(serverInetAddress); int serversocket = S1.PORT_NUMBER_SERVER; String msg = "Send file size"; byte[] b = msg.getBytes(); // dummy assignments - not used anywhere int filesize = 1; DatagramPacket reply, request; reply = new DatagramPacket(b, b.length, host, serversocket); request = new DatagramPacket(b, b.length, host, serversocket); for (i = 1; i <= 3; i++) { // defining a packet called request with parameters b(msg in bytes), b.length, host Internet // address and socket number if (i == 1) { host = server1Address; } else if (i == 2) { host = server2Address; } else if (i == 3) { host = server3Address; } request = new DatagramPacket(b, b.length, host, serversocket); // System.out.println("request sent from client to server"); Thread.sleep(S1.PAUSE_DURATION); // for error checks // Sending the packet- for getting the file size skt.send(request); // getting reply from // server........................................................................................ byte[] buffer = new byte [S1.PACKET_SIZE]; // apparently the size of data packet at the receiving side needs // to be bigger than the size of incoming datapacket reply = new DatagramPacket(buffer, buffer.length); // receiving packet from server - contatining filesize skt.receive(reply); // System.out.println("Response Received from server"); // System.out.println("on Client: - filesize= "+new String(reply.getData())); filesize = Integer.parseInt(new String(reply.getData()).trim()); // System.out.println("on Client: - filesize= "+filesize); Thread.sleep(S1.PAUSE_DURATION); } // here the client know the size of the file // Find the number of times it must make iterations - dividing filesize by packet_size // Request that many packets from server String[] buffer_string = new String[BUFFER_SIZE_CLIENT]; float delay[] = new float[filesize / S1.PACKET_SIZE]; System.out.println(filesize); System.out.println(S1.PACKET_SIZE); System.out.println(filesize / S1.PACKET_SIZE); Thread.sleep(2000); byte[] buffer = new byte[S1.PACKET_SIZE]; for (i = 0; i < filesize / S1.PACKET_SIZE; i++) { if (i % 100 != 0) { // System.out.print(" "+i); } else { System.out.println(" " + i); } msg = String.valueOf(i); b = msg.getBytes(); if (i % 3 == 0) { host = server1Address; } else if (i % 3 == 1) { host = server2Address; } else if (i % 3 == 2) { host = server3Address; } request = new DatagramPacket(b, b.length, host, serversocket); skt.send(request); delay[i] = System.nanoTime(); Thread.sleep(10); skt.receive(reply); delay[i] = System.nanoTime() - delay[i]; delay[i] = delay[i] / (1000000); /* if(empty_index<BUFFER_SIZE_CLIENT) { buffer_string[empty_index]=new String(reply.getData()); empty_index++; } else { for(j=0;j<BUFFER_SIZE_CLIENT-1;j++) { buffer_string[j]=buffer_string[j+1]; } buffer_string[BUFFER_SIZE_CLIENT-1]=new String(reply.getData()); }*/ // display_buffer(buffer_string); } Arrays.sort(delay); float delay2[] = new float[filesize / S1.PACKET_SIZE]; for (i = 0; i < delay2.length; i++) { delay2[i] = delay[delay.length - i - 1]; } // delay2 stores the array in descending values float[] Sk = new float[filesize / S1.PACKET_SIZE]; Sk[0] = (float) 0.0; for (i = 1; i < filesize / S1.PACKET_SIZE; i++) { for (j = 1; j <= i; j++) { Sk[i] = Sk[i] + delay2[j]; } Sk[i] = Sk[i] / (10 * i); } make_output(Sk); System.out.format( "Sk at 2=%f\n,10=%f\n,20=%f\n,100=%f\n and 30000=%f\n ", Sk[1], Sk[9], Sk[19], Sk[99], Sk[29999]); // display_buffer(buffer_string); skt.close(); } }
public ProgressIndicator() { start = lastTime = System.nanoTime(); }
public void run() { long lastTime = System.nanoTime(); double unprocessed = 0; int frames = 0; long lastTimer1 = System.currentTimeMillis(); try { init(); } catch (Exception e) { e.printStackTrace(); return; } // if (!isMultiplayer) { // createLevel(); // } int toTick = 0; long lastRenderTime = System.nanoTime(); int min = 999999999; int max = 0; while (running) { if (!this.hasFocus()) { keys.release(); } double nsPerTick = 1000000000.0 / framerate; boolean shouldRender = false; while (unprocessed >= 1) { toTick++; unprocessed -= 1; } int tickCount = toTick; if (toTick > 0 && toTick < 3) { tickCount = 1; } if (toTick > 20) { toTick = 20; } for (int i = 0; i < tickCount; i++) { toTick--; // long before = System.nanoTime(); tick(); // long after = System.nanoTime(); // System.out.println("Tick time took " + (after - before) * // 100.0 / nsPerTick + "% of the max time"); shouldRender = true; } // shouldRender = true; BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); continue; } if (shouldRender) { frames++; Graphics g = bs.getDrawGraphics(); Random lastRandom = TurnSynchronizer.synchedRandom; TurnSynchronizer.synchedRandom = null; render(g); TurnSynchronizer.synchedRandom = lastRandom; long renderTime = System.nanoTime(); int timePassed = (int) (renderTime - lastRenderTime); if (timePassed < min) { min = timePassed; } if (timePassed > max) { max = timePassed; } lastRenderTime = renderTime; } long now = System.nanoTime(); unprocessed += (now - lastTime) / nsPerTick; lastTime = now; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if (shouldRender) { if (bs != null) { bs.show(); } } if (System.currentTimeMillis() - lastTimer1 > 1000) { lastTimer1 += 1000; fps = frames; frames = 0; } } }
public void executePlay() { println("play"); isPlaying = true; startTime = System.nanoTime(); }
public void update() { if (isPlaying) { // only plays sound for dialog if (sMan.eventHappened(getPosInSeconds())) { // put sound code here!!! sMan.peekNextEvent().execute(getPosInSeconds()); println(sMan.popEvent()); } // find if previous dialog should still be playing, and if so, play it if (sMan.getPreviousPoppedDialog(getPosInSeconds()) != null) { sMan.getPreviousPoppedDialog(getPosInSeconds()).execute(getPosInSeconds()); // println(sMan.getPreviousPoppedDialog(getPosInSeconds())); } } if (overSlide()) { over = true; } else { over = false; } if (mousePressed && over) { locked = true; } if (!mousePressed) { locked = false; } if (locked) { newspos = constrain(mouseX - sheight / 2, sposMin, sposMax); } if (abs(newspos - spos) > 1 || isPlaying) { // if playback has collided with the end of the timeline, stop playing if (isPlaying) { if (getPosInSeconds() >= totalTime) { executePause(); } } if (isPlaying) { long estimatedTime = System.nanoTime() - startTime; if (estimatedTime / 1000000000 >= 1) { // if one second has passed, make a new start time (reset the second timer) startTime = System.nanoTime(); // println("before spos is " + spos + " time is " + getPosInSeconds()); spos = incrementSposInSeconds(); println( "spos is " + spos + " time is " + getPosInSeconds() + " frame is " + getPosInFrames()); } } else { spos = spos + (newspos - spos) / loose; } } // if the timeline is NOT playing, create the queue for the events to unfold in order if (!isPlaying) { sMan.createQueue(getPosInSeconds()); } // println(getPosInSeconds()); }