abstractpinghandler.java

来自「Java p2p程序设计2002年版」· Java 代码 · 共 76 行

JAVA
76
字号
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 abstract class factors out the common behavior of all ping handler types. * Namely, creating a thread, listening on a particular pipe and dispatching * the pipe Message and timestamp to an abstract method. */public abstract class AbstractPingHandler implements Runnable {    InputPipe pipe;    Thread thread ;        public AbstractPingHandler(InputPipe pipe) {        this.pipe = pipe;    }        public void abort() {        thread.interrupt();    }        /**     * Spawn a thread and wait for a ping message to arrive.     */    public void waitForMessage() {        thread = new Thread(this);        thread.start();    }        public void run() {            Message msg = null;            long timestamp;                        while(true) {                try {                    msg = pipe.waitForMessage();                                        // Arrival timestamp                                  timestamp = new Date().getTime();                    if (msg == null) {                        if (Thread.interrupted()) {                            pipe.close();                            return;                        }                    }                } catch (Exception e) {                    e.printStackTrace();                    pipe.close();                    return;                }                deliverMessage(msg,timestamp);            }    }            /**     * 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 abstract void deliverMessage(Message msg, long timestamp);    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?