📄 abstractpinghandler.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -