📄 switch.java
字号:
/*
* Switch.java
*
* Created on 2003年5月16日, 下午4:19
*/
package collector.communication;
import java.util.*;
import collector.common.*;
import collector.common.queueobject.*;
import collector.queue.*;
/**
*
* @author WangJun
*/
public class Switch {
/** Creates a new instance of Switch */
public Switch() {
}
public int startSwitch(int port_no, Collection ConfigPorts) {
byte[] m_SendBuffer = null;
m_SendBuffer = new byte[1024];
int m_SendNum = assSwitchReport(port_no, m_SendBuffer);
if (m_SendNum <= 0) {
CFunction.putHintString(1, " 切换端口出错 " + "原端口号:" + port_no);
return -1;
}
int m_Switch_Flag = -1;
Iterator m_IConfigPorts = ConfigPorts.iterator();
while (m_IConfigPorts.hasNext()) {
ConfigPortClass m_ConfigPortClass = (ConfigPortClass) m_IConfigPorts.next();
if (m_ConfigPortClass != null) {
String m_IpAddress = m_ConfigPortClass.getIpAddress();
int m_PortNo = m_ConfigPortClass.getPortNo();
m_PortNo = 965;
IComm m_IComm = new CommSocket(m_IpAddress, m_PortNo, -1);
//CollectorDefine.SystemPrintln("initconnect " + "IP:" + m_IpAddress+ "PORT:" + m_PortNo );
int flag = m_IComm.initConnection();
if (flag <= 0) {
continue;
}
//CollectorDefine.SystemPrintln(CFunction.translateBytesToString(1, m_SendBuffer,m_SendNum));
// while (true )
m_IComm.sendBuffer(m_SendBuffer, m_SendNum);
m_IComm.closeConnection();
m_Switch_Flag = 1;
}
}
m_SendBuffer = null;
if (m_Switch_Flag == 1) {
return 1;
}
else {
return -1;
}
}
private int assSwitchReport(int port_no, byte[] SendBuffer) {
int ptr = 0;
byte[] m_TempBuffer = null;
m_TempBuffer = new byte[1024];
int flag = switchOnePort(port_no, m_TempBuffer);
if (flag == -1) {
m_TempBuffer = null;
return -1;
}
for (int i = 0; i < 50; i++) {
for (int k = 0; k < 10; k++) {
SendBuffer[ptr + k] = m_TempBuffer[k];
}
ptr += 10;
}
m_TempBuffer = null;
return ptr;
}
private int getPortStatus(int port_no) {
int status = 0;
ConfigPortClass m_ConfigPortClass = (ConfigPortClass) ConfigPortQueue.
getConfigurePortBuffer(port_no);
if (m_ConfigPortClass == null) {
return -1;
}
else {
status = m_ConfigPortClass.getStatus();
}
return status;
}
private int getBoardNo(int port_no) {
int m_StartPort = 0;
ConfigPortClass m_ConfigPortClass = (ConfigPortClass) ConfigPortQueue.
getConfigurePortBuffer(1);
if (m_ConfigPortClass != null) {
m_StartPort = 1;
}
else {
m_ConfigPortClass = (ConfigPortClass) ConfigPortQueue.
getConfigurePortBuffer(2);
if (m_ConfigPortClass != null) {
m_StartPort = 2;
}
else {
m_StartPort = 3;
}
}
int m_BoardNo = -1;
int m_TS_No = (port_no - m_StartPort) / 16;
int m_PortPos = (port_no - m_StartPort) % 16;
m_BoardNo = (m_TS_No / 2) * 8 + m_PortPos % 2;
//CollectorDefine.SystemPrintln("m_BoardNo = " +m_BoardNo );
return m_BoardNo;
}
private int switchOnePort(int port_no, byte[] SendBuffer) {
int i, j;
int last_fac_status, next_fac_status;
// port_no = port_no - 3;
// org.omg.CORBA.IntHolder BoardNo = new org.omg.CORBA.IntHolder();
if (port_no % 2 == 0) {
last_fac_status = getPortStatus(port_no - 1);
if (last_fac_status == -1) {
last_fac_status = CollectorDefine.MASTER_PORT;
}
next_fac_status = getPortStatus(port_no);
if (next_fac_status == -1) {
//CollectorDefine.SystemPrintln("port_no = " + port_no);
return -1;
}
}
else {
last_fac_status = getPortStatus(port_no);
if (last_fac_status == -1) {
//CollectorDefine.SystemPrintln("port_no = " + port_no);
return -1;
}
next_fac_status = getPortStatus(port_no + 1);
if (next_fac_status == -1) {
next_fac_status = CollectorDefine.MASTER_PORT;
}
}
int m_BoardNo = getBoardNo(port_no);
SendBuffer[0] = (byte) 0xEB;
SendBuffer[1] = (byte) 0x90;
SendBuffer[2] = (byte) 0xEB;
SendBuffer[3] = (byte) 0x90;
SendBuffer[4] = (byte) 0xEB;
SendBuffer[5] = (byte) 0x90;
// SendBuffer[6] = (byte)BoardNo.value ;
SendBuffer[6] = (byte) m_BoardNo;
// SendBuffer[6] = (byte)0x00 ;
SendBuffer[7] = (byte) 0x55;
SendBuffer[8] = (byte) 0;
if (last_fac_status == 0) {
SendBuffer[8] |= 0x02;
}
else {
SendBuffer[8] |= 0x01;
}
if (next_fac_status == 0) {
SendBuffer[8] |= 0x08;
}
else {
SendBuffer[8] |= 0x04;
}
byte lpc = 0x00;
for (j = 0; j < 9; j++) {
lpc ^= SendBuffer[j];
}
SendBuffer[9] = (byte) (lpc ^ 0xff);
return 10;
}
//public int startSwitch(int port_no , int back_port_no , Collection ConfigPorts )
public static void main(String args[]) {
String dbHost = "localhost";
ConfigPortQueue m_ConfigPortQueue = new ConfigPortQueue();
ConfigPortClass m_ConfigPortClass1 = new ConfigPortClass(0,
"aaa",
3,
"ddd",
19,
1,
0,
0);
m_ConfigPortQueue.putConfigurePortBuffer(3, m_ConfigPortClass1);
m_ConfigPortClass1 = new ConfigPortClass(0,
"aaa",
4,
"ddd",
20,
1,
0,
0);
m_ConfigPortQueue.putConfigurePortBuffer(4, m_ConfigPortClass1);
Switch m_Switch = new Switch();
ArrayList ConfigPorts = new ArrayList();
ConfigPortClass m_ConfigPortClass = new ConfigPortClass();
m_ConfigPortClass.setIpAddress("192.1.1.112");
m_ConfigPortClass.setPortNo(965);
ConfigPorts.add(m_ConfigPortClass);
m_ConfigPortClass = new ConfigPortClass();
m_ConfigPortClass.setIpAddress("192.1.1.111");
m_ConfigPortClass.setPortNo(965);
ConfigPorts.add(m_ConfigPortClass);
// CollectorDefine.SystemPrintln("start to send ");
m_Switch.startSwitch(3, ConfigPorts);
// CollectorDefine.SystemPrintln("end to send");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -