📄 objectclient.java
字号:
package name.lxm.robot.arch.module;
import name.lxm.robot.arch.*;
import java.net.*;
import java.io.*;
/**
* This class is supposed to send a request to a server
* and recieve the response object
*
* the out port name is ``data''
*
*/
public class ObjectClient implements Module, Runnable
{
private String module_name;
private String[] port_names = {"data"};
private SimpleOutPort data_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 ObjectClient()
{
}
public String getName()
{
return module_name;
}
public Port getPort(String name)
{
if(port_names[0].equals(name))
return data_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]);
}
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)
{
try{
doConnect();
}catch(IOException e)
{}
Thread.sleep(100);
}
}catch(Exception e)
{
if(e instanceof OptionalDataException)
{
OptionalDataException oe = (OptionalDataException) e;
System.out.println(oe.eof);
System.out.println(oe.length);
}
e.printStackTrace();
}
}
private void doConnect() throws Exception
{
URL url = new URL(url_string);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
Object o = ois.readObject();
if( o instanceof Transporter)
{
Transporter trans = (Transporter) o;
data_port.setValue(this, trans.getBytes(), 1000);
}else
data_port.setValue(this, o, 1000);
ois.close();
is.close();
conn.disconnect();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -