⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectserver.java

📁 j2me实现的移动机器人代码(Java实现)
💻 JAVA
字号:
package name.lxm.robot.arch.module;

import name.lxm.robot.arch.*;
import java.net.*;
import java.io.*;
import org.jdom.*;
import org.jdom.output.*;

/**
 * This class is used to transfer objects through the network,
 * cooperated with the module named ObjectClient
 * <br><br>
 * The out port name is ``data''
 * <br><br>
 * The server is supposed to transmit object to client
 */
public class ObjectServer implements Module, Runnable
{
	private ModuleDoc doc = null;
	private String module_name = null;
	private SimpleInPort data_port = null;
	private String[] port_names = {"data"};
	
	private int listen_port = 80;
	private ServerSocket ssocket = null;
	private String verifyString = null;
	private Thread this_thread = null;
	private boolean bRun = false;

	public ObjectServer()
	{
		data_port = new SimpleInPort(this, port_names[0]);
	}
	
	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;
		module_name = cfg.getName();
		if(cfg.getParamValue("listen_port") !=null)
		{
			listen_port = Integer.parseInt(cfg.getParamValue("listen_port"));
		}
		verifyString = cfg.getParamValue("verify_string");
	}
	
	public void start() throws Exception
	{
		ssocket = new ServerSocket(listen_port);
		this_thread = new Thread(this);
		bRun = true;
		this_thread.start();
	}
	
	public void stop() throws Exception
	{
		bRun = false;
		ssocket.close();
	}
	
	public void destroy() throws Exception
	{
	}

	public void run()
	{
		while(bRun)
		{
			acceptConnection();
		}
	}

	private void acceptConnection()
	{
		Socket s;
		BufferedReader br;
		PrintWriter pw;
		String line;
		boolean bValid = false;
		ObjectOutputStream oos;
		try{
			s = ssocket.accept();
			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			oos = new ObjectOutputStream(s.getOutputStream());
			while((line = br.readLine()) != null)
			{
				if(line.equals(""))
					break;
				if(line.indexOf(verifyString) != -1)
				{
					bValid = true;
					break;
				}
			}
			if(!bValid)
				pw.println("No one knows who you are!");
			else
			{
				pw.println("HTTP/1.1 200 OK\nContent-type:text/xml");
				pw.println();
				//put the data content here
				Object o = data_port.getValue2();
				if(o != null)
				{
					if(o instanceof byte[])
						oos.writeObject(new Transporter((byte[]) o));
					else
						oos.writeObject(o);
				}
				else
					oos.writeObject("No data available.");
			}
			oos.close();
			pw.close();
			br.close();
			s.close();
		}catch(SocketException e)
		{
			//e.printStackTrace();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -