📄 netterminal.java
字号:
package name.lxm.robot.arch.module;
import name.lxm.robot.arch.*;
import name.lxm.robot.arch.util.*;
import java.io.*;
import java.net.*;
/**
* This class is used to transmit and recieve messages (text based)
* between two modules throught the ethernet <br><br>
*
* It can be refered from the name that this class is implemented
* based on http protocol. That's true, and the command is delivered
* by use of the method ``get'', therefore it is should not be too
* long (less 1024 bytes theoretically).
*/
public class NetTerminal extends AbstractModule
{
private String[] port_names = {"data_in", "data_out"};
private SimpleInPort in_port;
private SimpleOutPort out_port;
private String conn_string;
private String verify_string;
private ServerSocket ssocket;
int listenPort;
public void valueUpdated()
{
String msg = in_port.getValue().toString();
new HttpSender(conn_string, msg).start();
}
public void run()
{
while(bRun)
{
acceptConnection();
}
}
public Port getPort(String name)
{
if(port_names[0].equals("data_in"))
return in_port;
if(port_names[1].equals("data_out"))
return out_port;
return null;
}
public void init(ModuleDoc doc) throws Exception
{
super.init(doc);
in_port = new SimpleInPort(this, port_names[0]);
out_port = new SimpleOutPort(this, port_names[1]);
in_port.registerListener(this);
String server_string = doc.getParamValue("server_string");
String server_port = doc.getParamValue("server_port");
verify_string = doc.getParamValue("verify_string");
conn_string = "http://"+server_string+":"+server_port+"/"+verify_string;
String s = doc.getParamValue("listen_port");
if(s == null) throw new IllegalXMLFormatException("listen_port is required for the module " + module_name);
listenPort = Integer.parseInt(s);
ssocket = new ServerSocket(listenPort);
}
private void acceptConnection()
{
Socket s;
BufferedReader br ;
PrintWriter pw;
String line;
boolean bValid = false;
try{
s = ssocket.accept();
System.out.println("A connection comes in!");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
int p = 0;
while((line = br.readLine()) != null)
{
System.out.println(line);
if(line.equals(""))
break;
if((p=line.indexOf(verify_string)) != -1)
{
bValid = true;
//extract the company command string
int ep = line.indexOf("HTTP/1.1");
String ss = line.substring(p+verify_string.length()+1, ep);
ss = URLDecoder.decode(ss, "UTF-8");
System.out.println("Incoming Command: " + ss);
out_port.setValue(this, ss, 1000);
break;
}
}
if(!bValid)
pw.println("Unwelcome connection.");
else
{
pw.println("HTTP/1.1 200 OK");
pw.println("Content-type:text/xml");
pw.println();
pw.println("Hello, " + s.toString());
}
pw.close();
br.close();
s.close();
}catch(Exception e)
{
if(! (e instanceof SocketException))
e.printStackTrace();
}
}
public void start() throws Exception
{
super.start();
}
public void stop() throws Exception
{
bRun = false;
ssocket.close();
super.stop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -