pingreplyhandler.java
来自「Java p2p程序设计2002年版」· Java 代码 · 共 81 行
JAVA
81 行
package net.jxta.impl.shell.bin.ping;import java.io.*;import net.jxta.exception.*;import net.jxta.discovery.*;import net.jxta.endpoint.*;import net.jxta.pipe.*;import net.jxta.peergroup.*;import net.jxta.document.*;import net.jxta.id.*;import net.jxta.protocol.*;import java.util.*;/** * This handler class listens on a supplied pipe for ping reply messages * and notifies its listeners when one does. */public class PingReplyHandler extends AbstractPingHandler implements Runnable { Vector listeners = new Vector(); public PingReplyHandler(InputPipe pipe) { super(pipe); } /** * Notify listeners of a ping reply event. * @param event The PingReplyEvent */ protected void notifyListeners(PingReplyEvent event) { for(int i=0;i<listeners.size();i++) ((PingReplyListener)listeners.elementAt(i)).pingReply(event); } public void addPingReplyListener(PingReplyListener ppml) { listeners.addElement(ppml); } public void removePingReplyListener(PingReplyListener ppml) { listeners.removeElement(ppml); } /** * This key method is abstractly declared in AbstractPingHandler which * delegates to subclasses here. * @param msg The Message object received on the pipe * @param timestamp The timestamp the message was received on the pipe */ protected void deliverMessage(Message msg, long timestamp) { Enumeration enum = msg.getNames(); if ((enum == null) || (!enum.hasMoreElements())) { // Empty message return; } byte[] buffer = null; try { buffer = msg.getBytes("reply"); String message = new String(buffer); StringTokenizer st = new StringTokenizer(message,":"); // The peer's name String peername = st.nextToken(); // Time it took for original ping request to reach peer. long time = new Long(st.nextToken()).longValue(); // Time the ping was received on the peer long tstamp = new Long(st.nextToken()).longValue(); PingReplyEvent pr = new PingReplyEvent(this,time,tstamp,timestamp,peername); notifyListeners(pr); } catch (Exception e) { // No user information } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?