📄 ping.java
字号:
/* * @(#)$Id: Ping.java,v 1.6 2004/07/02 23:59:22 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package util.network.ping;import java.net.InetAddress;import java.net.InetSocketAddress;import services.LocalNode;import services.network.Payload;import services.network.udp.UDPClient;import util.network.serialization.SerializationManager;import util.timer.timeouts.TimeoutManager;import util.timer.timeouts.TimeoutManagerClient;/** * Class Ping * */public class Ping implements UDPClient, TimeoutManagerClient { private int sequenceNumber; private int serverPort; private TimeoutManager timeoutManager; /** * Constructor Ping * * @param serverPort */ public Ping(int serverPort) { this.serverPort = serverPort; sequenceNumber = 0; timeoutManager = new TimeoutManager(); SerializationManager.registerClass("util.network.ping.EchoMessage"); LocalNode.myUDPMessenger.listen(new Integer(serverPort), this); } /** * Method ping * * @param destination * @param timeout * @param additionalData * @param client */ public void ping(InetSocketAddress destination, double timeout, Payload additionalData, PingClient client) { double currentTime = LocalNode.myTimer.getCurrentTime(); int curSequenceNumber = sequenceNumber++; EchoMessage message = EchoMessage.allocate(serverPort, curSequenceNumber, currentTime, additionalData); PingEntry entry = PingEntry.allocate(destination, client); timeoutManager.addAbsoluteTimeout(entry, new Integer(curSequenceNumber), currentTime + timeout, this); LocalNode.myUDPMessenger.send(destination, message); } private void processEchoMessage(InetAddress source, EchoMessage message) { message.convertToReply(); LocalNode.myUDPMessenger.send( new InetSocketAddress(source, message.getIdentifier()), message); } private void processEchoReply(EchoMessage message) { double currentTime = LocalNode.myTimer.getCurrentTime(); PingEntry entry = (PingEntry) timeoutManager.removeTimeout( new Integer(message.getSequenceNumber())); // If entry is null, then the ping already expired if (entry != null) { entry.getClient().handlePingResponse(entry.getDestination(), currentTime - message.getSendTime(), true); } } /** * Method handleUDPNetwork * * @param source * @param data */ public void handleUDPNetwork(InetSocketAddress source, Payload data) { if (data instanceof EchoMessage) { EchoMessage message = (EchoMessage) data; if (message.getType() == EchoMessage.ECHO_MESSAGE) { processEchoMessage(source.getAddress(), message); } else { processEchoReply(message); } } } /** * Method handleTimeout * * @param item * @param currentTime */ public void handleTimeout(Object item, double currentTime) { PingEntry entry = (PingEntry) item; entry.getClient().handlePingResponse(entry.getDestination(), 0, false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -