📄 inittermserver.java
字号:
/*
* InitTermServer.java
*
* Created on 2003年3月22日, 下午3:21
*/
package collector.communication;
import collector.common.*;
/**
*
* @author WangJun
*/
public class InitTermServer {
/** Creates a new instance of InitTermServer */
private static final int STRING = 0;
class TelnetProcStruct {
int m_flag;
int m_type;
int m_value;
String m_buffer;
}
private TelnetProcStruct m_TelNet = new TelnetProcStruct();
private String m_TsName;
private String m_Buffer = "";
private int m_Count;
private IComm m_IComm = null;
private int m_ErroeCount = 0, error_flag = 0;
// private int errno;
private int m_PortNo;
private byte m_RecBuffer[];
private byte m_SendBuffer[];
public PortConfig m_PortConfig = null;
private int m_CommDebug;
public InitTermServer(String m_TsName, int m_CommDebug) {
this.m_TsName = m_TsName;
this.m_CommDebug = m_CommDebug;
m_RecBuffer = new byte[1024];
m_SendBuffer = new byte[1024];
}
public int setPortConfig(PortConfig m_PortConfig, int m_PortNo) {
this.m_PortConfig = m_PortConfig;
this.m_PortNo = m_PortNo;
return 1;
}
int timeOver(int m_Nouse, String m_Tsname) {
String m_String = "INIT_TS: Cannot connect to terminal server" + m_Tsname;
CFunction.writeToSystem(this.m_CommDebug, m_String);
return 1;
}
int timeOver2(int m_Nouse, String m_Tsname) {
String m_String = "INIT_TS: Cannot initial terminal server" + m_Tsname;
CFunction.writeToSystem(this.m_CommDebug, m_String);
return 1;
}
public int initTcp(String m_Tsname, int m_PortNo) {
CFunction.writeToSystem(this.m_CommDebug, "start to init m_PortNo 23 ...");
this.m_IComm = new CommSocket("chase", m_PortNo, -1);
return this.m_IComm.initConnection();
}
public int closeTcp() {
int ret = -1;
if (this.m_IComm.isConnectionOpen() == true) {
ret = this.m_IComm.closeConnection();
}
return ret;
}
int clearFlag(String m_str, int m_count) {
m_TelNet.m_flag = 1;
m_Buffer = m_str;
m_Count = m_count;
return 0;
}
int sendBuffer(String m_Buffer, int m_flag) {
int num = m_Buffer.length();
m_Buffer = "";
this.m_IComm.sendString(m_Buffer);
return num;
}
int analyseReceivedData() {
CFunction.writeToSystem(this.m_CommDebug,
"begin to analyse receive data ....");
String m_String = this.m_IComm.recStringBuffer(this.m_RecBuffer, false, 3);
if (m_String != "")
{
m_ErroeCount = 0;
m_Buffer = m_String;
checkBuffer();
}
else {
m_ErroeCount++;
try {
Thread.sleep(1000);
}
catch (InterruptedException m_InterruptedException) {
CFunction.writeToSystem(this.m_CommDebug, "sleep error !!! ....");
}
}
return 0;
}
int checkBuffer() {
// CFunction.writeToSystem(this.m_CommDebug,"start to check m_Buffer : "+ m_Buffer);
if (m_TelNet.m_flag != 0) {
return 0;
}
if (m_TelNet.m_type == STRING) {
if (m_Buffer.length() == 0) {
return 0;
}
if (m_Buffer.indexOf(m_TelNet.m_buffer, 0) >= 0) {
// CollectorDefine.SystemPrintln("the receive m_buffer has need string");
m_TelNet.m_flag = 1;
}
else {
; //CollectorDefine.SystemPrintln("the receive m_buffer has not any need string !!!...");
}
}
else {
; //CollectorDefine.SystemPrintln("the m_type is not STRING !!!...");
}
return (0);
}
int readConfig(int m_PortNo) {
int m_RetCode;
while (true) {
if (m_ErroeCount > 600) {
CFunction.writeToSystem(this.m_CommDebug, "Cannot initial ");
break;
}
analyseReceivedData();
m_RetCode = decideReadCommand(m_PortNo);
if (m_RetCode < 0 || m_RetCode == 1) {
break;
}
}
return (0);
}
int decideReadCommand(int m_PortNo) {
String buf2;
if (m_TelNet.m_flag == 0) {
return (0);
}
int retvalue = 0;
switch (m_Count) {
case 0:
String m_String = "show m_PortNo " + new Integer(m_PortNo + 1).toString() +
"\n";
sendBuffer(m_String, 0);
m_TelNet.m_buffer = ">";
m_TelNet.m_type = STRING;
m_TelNet.m_flag = 0;
break;
default:
readTsConfig();
retvalue = 1;
break;
}
m_Count++;
return (retvalue);
}
int readTsConfig() {
String buf2 = "";
int intvalue = 0;
int baudrate, databit, stopbit, parity;
int cp = m_Buffer.indexOf("Speed", 0);
if (cp < 0) {
CollectorDefine.SystemPrintln("Speed cp =" + cp);
// return(-1);
}
else {
buf2 = m_Buffer.substring(cp + 12, cp + 12 + 11);
// CollectorDefine.SystemPrintln("&&&&&&&&&&&&&&&&&&&&&&&&&&& speed"+buf2);
try {
intvalue = (int) Integer.parseInt(buf2, 10);
}
catch (NumberFormatException m_NumberFormatException) {
CFunction.writeToSystem(this.m_CommDebug, "parseInt error !!!...");
}
// m_PortConfig.setBaudRate(intvalue);
}
cp = m_Buffer.indexOf("Bit", 0);
if (cp < 0) {
return ( -1);
}
else {
buf2 = m_Buffer.substring(cp + 12, cp + 12 + 11);
try {
intvalue = (int) Integer.parseInt(buf2, 10);
}
catch (NumberFormatException m_NumberFormatException) {
CFunction.writeToSystem(this.m_CommDebug, "parseInt error !!!...");
}
}
cp = m_Buffer.indexOf("Stop", 0);
if (cp < 0) {
}
else {
if (m_Buffer.indexOf("1.5", 0) >= 0) {
;
}
else if (m_Buffer.indexOf("2", 0) >= 0) {
;
}
else {
; //CollectorDefine.SystemPrintln("&&&&&&&&&&&&&&&&&&&&&&&&&&& stop1");
}
}
cp = m_Buffer.indexOf("Parity", 0);
if (cp < 0) {
}
else {
if (m_Buffer.indexOf("none", 0) >= 0) {
; //CollectorDefine.SystemPrintln("&&&&&&&&&&&&&&&&&&&&&&&&&&& parity none");
}
else if (m_Buffer.indexOf("Odd", 0) >= 0) {
; //CollectorDefine.SystemPrintln("&&&&&&&&&&&&&&&&&&&&&&&&&&& parity odd");
}
else {
; //CollectorDefine.SystemPrintln("&&&&&&&&&&&&&&&&&&&&&&&&&&& parity even");
}
}
return 1;
}
int connectTs() {
int m_RetCode;
int m_success_falg = -1;
while (true) {
if (m_ErroeCount > 1) {
CFunction.writeToSystem(this.m_CommDebug, "Cannot initial");
break;
}
analyseReceivedData();
m_RetCode = decideConnectCommand();
if (m_RetCode < 0 || m_RetCode == 1) {
m_success_falg = 1;
break;
}
}
return m_success_falg;
}
int decideConnectCommand() {
String buf2;
CFunction.writeToSystem(this.m_CommDebug,
"### start to logon the terminal server .....");
if (m_TelNet.m_flag == 0) {
return (0);
}
int retvalue = 0;
switch (m_Count) {
case 0:
m_TelNet.m_buffer = "password:";
m_TelNet.m_type = STRING;
m_TelNet.m_flag = 0;
break;
case 1:
sendBuffer("\n", 0);
m_TelNet.m_buffer = ">";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -