pinghandler.java
来自「Java p2p程序设计2002年版」· Java 代码 · 共 82 行
JAVA
82 行
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.*;/** * A concrete implementation of AbstractPingHandler that listens for only * inbound ping requests. */public class PingHandler extends AbstractPingHandler implements Runnable { // PingListeners Vector listeners = new Vector(); public PingHandler(InputPipe pipe) { super(pipe); } /** * 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 { //ping.println2("got ping msg "+msg); buffer = msg.getBytes("ping"); String message = new String(buffer); // Parse values out of ping message StringTokenizer st = new StringTokenizer(message,":"); String name = st.nextToken(); String timestr = (String)st.nextToken(); long ostamp = new Long(timestr).longValue(); PingEvent pr = new PingEvent(this,name,ostamp,timestamp); // notify listeners notifyListeners(pr); } catch (Exception e) { e.printStackTrace(); pipe.close(); } } /** * Notify interested listeners that a PingEvent has occured and send the * ping event. */ private void notifyListeners(PingEvent event) { for(int i=0;i<listeners.size();i++) ((PingListener)listeners.elementAt(i)).pingEvent(event); } public void addPingListener(PingListener ppml) { listeners.addElement(ppml); } public void removePingListener(PingListener ppml) { listeners.removeElement(ppml); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?