📄 netbridgeclient.java
字号:
package name.lxm.robot.arch.module;
import name.lxm.robot.arch.*;
import java.net.*;
import java.io.*;
public class NetBridgeClient implements Module, Runnable
{
private String module_name;
private String[] port_names = {"data", "cmd"};
private SimpleOutPort data_port;
private SimpleInPort cmd_port;
private ModuleDoc doc;
private int server_port = 80;
private boolean bRun = false;
private Thread t_this = null;
private String verify_string;
private String server_string;
private String url_string;
private String data_type;
public String getName()
{
return module_name;
}
public Port getPort(String name)
{
if(port_names[0].equals(name))
return data_port;
if(port_names[1].equals(name))
return cmd_port;
return null;
}
public ModuleDoc getModuleDoc()
{
return doc;
}
public int getLayer()
{
return doc.getLayer();
}
public void init(ModuleDoc cfg) throws Exception
{
doc = cfg;
String s = cfg.getParamValue("server_port");
if(s != null)
server_port = Integer.parseInt(s);
module_name = doc.getName();
verify_string = doc.getParamValue("verify_string");
server_string = doc.getParamValue("server_string");
url_string = "http://"+server_string+":"+server_port+"/"+verify_string;
data_type = doc.getParamValue("data_type");
data_port = new SimpleOutPort(this, port_names[0]);
cmd_port = new SimpleInPort(this, port_names[1]);
}
public void start() throws Exception
{
bRun = true;
t_this = new Thread(this);
t_this.start();
}
public void stop() throws Exception
{
bRun = false;
t_this = null;
}
public void destroy() throws Exception
{
}
public void run()
{
try{
while(bRun)
{
doConnect();
Thread.sleep(100);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
private void doConnect() throws Exception
{
//String cmd = cmd_port.getValue().toString();
URL url = new URL(url_string);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
Message msg = (Message) Class.forName(data_type).newInstance();
msg.loadFromReader(isr);
data_port.setValue(this, msg, 10000);
System.out.print("R");
isr.close();
conn.disconnect();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -