📄 abstractprocessor.java
字号:
msg.add(len);
String strMime = new String(raw, "UTF-8");
String toFlush = msg.toString() + "\r\n" + strMime;
if (Debug.printMime)
System.out.println("=> " + strMime);
flushMessage(toFlush);
}
/**
* 处理通告消息
*/
public void processNotifyMessage(IncomingMessage msg, BufferedReader in) throws Exception {
if (msg.getHeader().equals("MSG")) {
// System.out.println("equals msg");
int readlen = 0;
int len = msg.getInt(2);
byte[] b = new byte[len];
MimeMessage mime = new MimeMessage();
while (readlen < len) {
String line = in.readLine();
byte[] lb = line.getBytes("UTF-8");
if (lb.length == 0)
break;
System.arraycopy(lb, 0, b, readlen, lb.length);
b[readlen + lb.length] = '\r';
b[readlen + lb.length + 1] = '\n';
readlen += lb.length + 2 /*CRLF*/;
}
String s = new String(b, 0, readlen, "UTF-8");
mime.parse(s);
filterMimeMessage(mime);
if (Debug.printMime) {
System.out.println(s);
}
}
}
/**
* 向服务器发\uFFFD\uFFFD验证请求
* @param msg OutgoingMessage
* @param hash String
* @throws IOException
*/
public synchronized void sendCHLResponse(OutgoingMessage msg, String hash) throws
IOException {
flushMessage(msg.toString() + "\r\n" + hash);
}
/**
*标记消息跟踪id
*/
public void markTransactionId(OutgoingMessage msg) {
msg.setTransactionId(this.trId);
}
/**
* 当前跟踪标记id
* */
public int getCurrentTransactionId() {
return this.trId;
}
/**
* 注册回调函数
*/
protected void registerCallback(Integer trId, Callback cb) {
callbackMap.put(trId, cb);
}
/**
* 消息处理
*/
public void processMessage(IncomingMessage msg) throws Exception {
}
/**
* All Mime Messages ('MSG' header) filter in this method.
*/
protected void filterMimeMessage(MimeMessage msg) {
}
/**
* \uFFFD\uFFFD\uFFFD\uFFFD Callback \uFFFD\u033F\uFFFD\uFFFD\uFFFD trId\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD ~\uFFFD\uFFFD\uFFFDa\uFFFD\uFFFD \uFFFD\u01F8\uFFFD
* \uFFFD\uFFFD \uFFFD\u07BC\u04B5带 \uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\u033B\uFFFD callback map\uFFFD\uFFFD x\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\u02B5\uFFFD\uFFFD\uFFFD
* \uFFFD\uFFFDf\uFFFD\u03FF\uFFFD\uFFFD\uFFFD \uFFFD\u0474\uFFFD.
*/
public void removeInfiniteTransactionId(int trId) {
callbackMap.remove(new Integer(trId));
}
/**
* Thread 进程
*/
public final void run() {
isLive = true;
try {
//初始化
init();
//Poll
while (isLive && msn.isLogged) {
sleep(pollIntervalTime);
sentPollMessage();
}
}
catch (Throwable e) {
processError(e);
}
// finally {
// System.out.println("finally");
// cleanUp();
// if (in != null) {
// try {
// in.close();
// }
// catch (Exception e) {}
// in = null;
// }
// }
}
// public final void run() {
// isLive = true;
//
// try {
// init();
//
// while (isLive) {
// System.gc();
// String line = in.readLine();
//
// if (Debug.printInput)
// System.out.println(name + "<=" + line);
//
// if (line == null) {
// if (msn.isLogged == false)
// break;
// sleep(pollIntervalTime);
// sentPollMessage();
// continue;
// }
//
// IncomingMessage msg = null;
// try{
// msg = IncomingMessage.getInstance(line);
// }
// catch(Exception e){}
// if (msg == null)continue;
//
// // Bodyless = falase
// if (!msg.isNotify()) {
// Integer trId = new Integer(msg.getTransactionId());
// Callback cb = (Callback) callbackMap.get(trId);
// if (cb != null) {
// if (!cb.isInfinite())
// callbackMap.remove(trId);
// dispatchCallbackEvent(cb.getProcessType(), msg);
// }
// else
// processMessage(msg);
// }
// else {
// processNotifyMessage(msg);
// }
//
// }
// }
// catch (Throwable e) {
// processError(e);
// }
// finally {
// cleanUp();
//
// if (in != null) {
// try {
// in.close();
// }
// catch (Exception e) {}
// in = null;
// }
// }
// }
//
//
/**
* 处理接收消息
* @param in BufferedReader
*/
protected void processIn(BufferedReader in){
// in = (BufferedReader) bufferReaders.firstElement();
try {
while(true){
String line = in.readLine();
if (Debug.printInput)
System.out.println(name + "<=" + line);
if(line == null)
break;
IncomingMessage msg = null;
try{
msg = IncomingMessage.getInstance(line);
}
catch(Exception e){}
if (msg == null){
continue;
}
// Bodyless = falase
if (!msg.isNotify()) {
Integer trId = new Integer(msg.getTransactionId());
Callback cb = (Callback) callbackMap.get(trId);
if (cb != null) {
if (!cb.isInfinite())
callbackMap.remove(trId);
dispatchCallbackEvent(cb.getProcessType(), msg);
}
else
processMessage(msg);
}
else {
processNotifyMessage(msg, in);
}
}//~end while
}
catch (Exception e) {
}
}
/**
*
**/
public abstract void cleanUp();
/**
* 异常处理
*/
public abstract void processError(Throwable e);
/**
* 设置回调函数接口
* @param listener CallbackListener
*/
public void setCallbackListener(CallbackListener listener) {
callbackListener = listener;
}
/**
* 发布回调函数类型
* @param proType int 回调函数
* @param args Object[] 参数
*/
private void dispatchCallbackEvent(int proType, IncomingMessage args) {
if (callbackListener == null)
return;
callbackListener.callbackEvent(proType, args);
}
/**
* 处理http反馈\uFFFD\uFFFD分解SessionId 和ip
* @param headValue String
*/
private void parseResponseHead(String headValue) {
if (headValue.endsWith("Session=close")) {
if (msn != null) {
if (name.equals("NS"))
msn.fireConnectFailEvent(MSNMessenger.ERR_SESSION_CLOSE, true);
else
msn.fireConnectFailEvent(MSNMessenger.ERR_SESSION_CLOSE, false);
}
return;
}
int tempStart = 0, tempEnd = 0;
//headValue 格式: SessionID=913000827.10930; GW-IP=207.46.110.23
tempStart = headValue.indexOf("=");
tempEnd = headValue.indexOf(";");
sessionID = null;
sessionID = headValue.substring(tempStart + 1, tempEnd);
tempStart = headValue.indexOf("=", tempEnd);
hostURL = null;
hostURL = "http://" + headValue.substring(tempStart + 1);
}
/**
* 向服务器发poll消息
*/
protected synchronized void sentPollMessage() throws IOException {
System.out.println("poll==>");
flushMessage(null);
}
// /**
// * 中断线程 ceze 2004/6/12
// */
public void cancel() {
isLive = false;
}
/**
* 设置向服务器发起查询时间间隔
* @param intervalTime short
*/
protected void setPollInterval(short intervalTime) {
pollIntervalTime = intervalTime;
}
public void setRequestCommand(String cmd){
requestCmd = cmd;
}
class FlushTask
extends TimerTask {
String toFlush;
public FlushTask(String s) {
toFlush = s;
}
public void run() {
try{
flushMessage(toFlush);
}catch(Exception e){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -