📄 abstractprocessor.java
字号:
/*
* @(#)AbstractProcessor.java
*
* Copyright (c) 2001-2002, JangHo Hwang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the JangHo Hwang nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: AbstractProcessor.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
*/
package vitular.msnp;
import java.io.*;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import java.util.Hashtable; //HashMap --> Hashtable 2004/5/29 ceze
import java.util.Vector;
import vitular.msnp.entity.Callback;
import vitular.msnp.entity.CallbackListener;
import vitular.msnp.msg.IncomingMessage;
import vitular.msnp.msg.OutgoingMessage;
import vitular.msnp.msg.MimeMessage;
import vitular.msnp.util.BufferedReader;
import java.util.TimerTask;
import java.util.Timer;
/**
*
* <p>Title: UnijaMsn</p>
* <p>Description: Msn Unija平台客户端</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Vitular</p>
* @author not attributable
* @version 1.1
*/
public abstract class AbstractProcessor
extends Thread {
//消息回调处理函数类型
protected static final char PROCESS_AUTH = 0X01;
protected static final char PROCESS_CVR = 0X02;
protected static final char PROCESS_LOGON = 0X03;
protected static final char PROCESS_RENAME = 0X04;
protected static final char PROCESS_VER = 0X05;
protected static final char RESPONSE_ADD = 0X06;
protected static final char RESPONSE_BLOCK = 0X07;
protected static final char RESPONSE_CREATEGROUP = 0X08;
protected static final char RESPONSE_MOVEGROUP = 0X09;
protected static final char RESPONSE_RENAMEGROUP = 0X0A;
protected static final char RESPONSE_REMOVEGROUP = 0X0B;
protected static final char JUDGE_SERIAL = 0X0C;
protected static final char DO_COLLECT_LIST = 0X0D;
protected static final char CONNECT_SWITCHBOARD = 0X0E;
protected static final char PROCESS_USER_CALL = 0X0F;
protected static final char PROCESS_CALL_RESULT = 0X10;
protected static final char PROCESS_ROSTER_INFO = 0X11;
protected static final char PROCESS_XFR = 0X12;
protected static final char RESPONSE_REMOVE = 0X13;
protected static final char RESPONSE_GROUPADD = 0X14;
protected static final char RESPONSE_GROUPREMOVE = 0X15;
public final String REQUEST_HEAD_ACCEPT = "Accept";
public final String REQUEST_HEAD_ACCEPT_LANGUAGE = "Accept-Language";
public final String REQUEST_HEAD_ACCEPT_ENCODING = "Accept-Encoding";
public final String REQUEST_HEAD_USER_AGENT = "User-Agent";
public final String REQUEST_HEAD_HOST = "Host";
public final String REQUEST_HEAD_PROXY_CONNECTION = "Proxy-Connection";
public final String REQUEST_HEAD_CONNECTION = "Connection";
public final String REQUEST_HEAD_PRAGMA = "Pragma";
public final String REQUEST_HEAD_CONTENT_TYPE = "Content-Type";
public final String REQUEST_HEAD_CONTENT_LENGTH = "Content-Length";
private final String SERVER_URL_CGI = "/gateway/gateway.dll?";
public final MSNMessenger msn;
protected String sessionID = null; //服务器返回的sessionID
protected String hostURL = null; //服务器ip
protected String requestCmd = null;//向服务器发送的request命令
protected short pollIntervalTime = 5000; //向服务器查询时间间隔
private boolean autoOut = false;
private int trId;
private String name = "";//服务器类型名称
protected Hashtable callbackMap = new Hashtable();
protected boolean isLive;
// protected BufferedReader in = null; //原BufferedReader
protected CallbackListener callbackListener = null;
// protected boolean isFlushBusy = false;
// protected Hashtable msgToFlush = new Hashtable();
protected boolean isProcessInit = false; //与服务器初次交互,(在Init())
//待处理消息队列
// protected Vector bufferReaders = new Vector();
// protected Timer timer = new Timer();
/**
* Client processor\uFFFD\uFFFD \uFFFD\u01B4\uFFFD Server processor\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD\u04B6\uFFFD\uFFFD\uFFFD\uFFFD, ServerInfo\uFFFD\uFFFD \uFFFD\u02BF\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD
* \uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\u06B8\uFFFD \uFFFD\uFFFD\uFFFD\u03FF\uFFFD \uFFFDν\uFFFD\uFFFD\u03FD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\u03F5\uFFFD\uFFFD\uFFFD \uFFFD\u0474\uFFFD.
*/
protected AbstractProcessor(MSNMessenger msn) {
this(msn, null, 1);
}
protected AbstractProcessor(MSNMessenger msn, String servURL) {
this(msn, servURL, 1);
}
/**
* \uFFFD\u05BE\uFFFD\uFFFD\uFFFD ServerInfo\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFDtrId\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD \u01AE\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u0335\uFFFD\uFFFD\uFFFD
* \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFDProcessor\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\u0474\uFFFD.
*/
protected AbstractProcessor(MSNMessenger msn, String servURL, int trId) {
this.msn = msn;
this.hostURL = servURL;
this.trId = trId;
}
public void setServerName(String name) {
this.name = name;
//setName("MSN Channel(" + name + ")");
}
public String getServerName() {
return this.name;
}
/**
* \uFFFD\u0634\uFFFD processor\uFFFD\uFFFD ~\uFFFD\uFFFD\u0276\uFFFD\uFFFDOUT \uFFFD\u07BD\uFFFD\uFFFD\uFFFD \uFFFD\u06B5\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD
* \uFFFD\uFFFDd\uFFFD\u0474\uFFFD. \uFFFD\u2EBB\uFFFD\uFFFD: false\uFFFD\u0334\uFFFD.
*/
public void setAutoOutSend(boolean autoOut) {
this.autoOut = autoOut;
}
/**
* \uFFFD\u0634\uFFFD processor\uFFFD\uFFFD ~\uFFFD\uFFFD\u0276\uFFFD\uFFFDOUT \uFFFD\u07BD\uFFFD\uFFFD\uFFFD \uFFFD\u06B5\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u03F0\uFFFD\uFFFD\uFFFD\u01FE\uFFFD\uFFFD\u05B4\uFFFD\uFFFD\uFFFD
* \u022E\uFFFD\uFFFD\uFFFD\u0474\uFFFD.
*/
public boolean isAutoOutSend() {
return this.autoOut;
}
public void setHostURL(String url) {
hostURL = url;
}
protected String getHostURL() {
return hostURL;
}
/**
* 组织连接的URL
* @return String
*/
// protected synchronized String makeURL(String hostAddress, String cmdParam, String cmdValue) {
// StringBuffer sb = new StringBuffer(hostAddress.length() +
// SERVER_URL_CGI.length() +
// cmdParam.length() + cmdValue.length());
// sb.append(hostAddress);
// sb.append(SERVER_URL_CGI);
// sb.append(cmdParam);
// sb.append(cmdValue);
// sb.append("HTTP/1.1");
// sb.append("\r\n");
// serverURL = sb.toString();
// System.out.println(serverURL);
// return serverURL;
//
// }
/**
* 创建HttpConnection连接对象
*/
protected synchronized HttpConnection makeConnection1(String host, String request) throws IOException {
HttpConnection httpConn = null;
//组织url
StringBuffer sbUrl = new StringBuffer();
sbUrl.append(host);
sbUrl.append(SERVER_URL_CGI);
sbUrl.append(request);
httpConn = (HttpConnection) Connector.open(sbUrl.toString(), Connector.READ_WRITE);
//采用Post方式连接
httpConn.setRequestMethod(HttpConnection.POST);
// httpConn.setRequestProperty(REQUEST_HEAD_ACCEPT, "*/*");
// httpConn.setRequestProperty(REQUEST_HEAD_ACCEPT_LANGUAGE, "en-us");
// httpConn.setRequestProperty(REQUEST_HEAD_ACCEPT_ENCODING,"gzip, deflate");
// //httpConn.setRequestProperty(REQUEST_HEAD_USER_AGENT, "MSMSGS"); //Mozilla/4.0
// httpConn.setRequestProperty(REQUEST_HEAD_PROXY_CONNECTION, "Keep-Alive");
// httpConn.setRequestProperty(REQUEST_HEAD_CONNECTION, "Keep-Alive");
// httpConn.setRequestProperty(REQUEST_HEAD_PRAGMA, "no-cache");
// httpConn.setRequestProperty(REQUEST_HEAD_CONTENT_TYPE, "application/x-msn-messenger");
// httpConn.setRequestProperty("Proxy", "192.168.1.27");
return httpConn;
}
/**
* 虚函数,初始化
* @throws IOException
*/
public abstract void init() throws IOException;
/**
* 传输消息字符串,并接收反馈
* @param msgStr String
* @throws IOException
*/
public synchronized void flushMessage(String msgStr) throws IOException {
String cmd;
if(isProcessInit){
//第一次连接
cmd = requestCmd;//"Action=open&Server=" + getServerName() + "&IP=" + requestValue;
}
else
{
if(msgStr == null){
//poll命令
cmd = "Action=poll&SessionID=" + sessionID;
}
else{
//其它命令
cmd = "SessionID=" + sessionID;
}
}
HttpConnection httpCon = makeConnection1(hostURL, cmd);
if(httpCon == null)
return;
DataOutputStream out = null;
short timer = 3;
while(timer-- > 0){
try{
out = httpCon.openDataOutputStream();
if (msgStr != null) {
out.write(msgStr.getBytes());
trId++;
}
out.flush();
break;
}
catch(IOException e){
if(timer < 0){
cleanUp();
if (getName().equals("NS"))
msn.fireConnectFailEvent(MSNMessenger.ERR_SERVER_FAIL, true);
else
msn.fireConnectFailEvent(MSNMessenger.ERR_SERVER_FAIL, false);
return;
}
}
}
//反馈代码非正确(200)
if (httpCon.getResponseCode() != 200) {
out.close();
httpCon.close();
out = null;
httpCon = null;
return;
}
//分解反馈内容
parseResponseHead(httpCon.getHeaderField("X-MSN-Messenger"));
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.openInputStream(),"UTF-8"));
out.close();
httpCon.close();
out = null;
httpCon = null;
//处理反馈消息
processIn(in);
}
/**
* 发送命令信息
*/
public synchronized void sendMessage(OutgoingMessage msg) throws IOException {
if (msg == null)
return;
if (Debug.printOutput)
System.out.println(getServerName() + "=> " + msg.toString());
//添加到回调处理函数
if (trId != -1 && msg.getBackProcess() != null) {
callbackMap.put(new Integer(trId), msg.getBackProcess());
}
String toFlush = msg.toString() + "\r\n";
flushMessage(toFlush);
// FlushTask t = new FlushTask(toFlush);
// timer.schedule(t, 0);
}
/**
* 发送对话(mime)消息
*/
public synchronized void sendMimeMessage(OutgoingMessage msg,
MimeMessage mime) throws IOException {
if(mime == null)
return;
if (trId != -1 && msg != null && msg.getBackProcess() != null) {
callbackMap.put(new Integer(trId), msg.getBackProcess());
}
byte[] raw = mime.getBytes();
int len = raw.length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -