📄 server.java
字号:
package Pihatonttu;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
public class Server implements Runnable, SerialPortEventListener {
private SerialPort comPort;
private Calendar cal = null;
private int year = 0;
private String month = null;
private int day_of_month = 0;
private int hour = 0;
private int minute = 0;
private int second = 0;
private String accessTime = null;
private DataInputStream comReader;
private DataOutputStream comWriter;
private HttpURLConnection hc = null;
private Socket socket = null;
private DataInputStream reader;
private DataOutputStream writer;
private String requestMethod = null;
private String requestURI = null;
private String requestProtocol = null;
private String requestHost = null;
private int requestPort = 80;
private Hashtable<String, String> requestHeaderField = new Hashtable<String, String>();
private byte[] postData = null;
private String responseProtocol = null;
private String responseCode = null;
private String responseMessage = null;
private int byteTranfered = 0;
private String host = null;
private int port = 80;
Server(String com, String proxy){
CommPortIdentifier portID = null;
try{
portID = CommPortIdentifier.getPortIdentifier(com);
}catch(NoSuchPortException e){
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot find COM port.");
System.exit(1);
}
try{
comPort = (SerialPort)portID.open("Pihatonttu", 5000);
}catch(PortInUseException e){
Pihatonttu.PihatonttuMain.showErrorDialog("COM port is in use.");
System.exit(1);
}
try {
comPort.setSerialPortParams(460800, //9600?115200?230400?460800?921600?
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
comPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e){
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot configure COM port.");
System.exit(1);
}
try {
comPort.addEventListener(this);
} catch(TooManyListenersException ex) {
Pihatonttu.PihatonttuMain.showErrorDialog("COM port error.");
System.exit(1);
}
comPort.notifyOnDataAvailable(true);
if(!proxy.equals("null")) {
int delimitPos = proxy.indexOf(':');
host = proxy.substring(0, delimitPos);
port = Integer.valueOf(proxy.substring(delimitPos + 1)).intValue();
}
}
private void closeCOM(){
try{
if(comReader != null){
comReader.close();
comReader = null;
}
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot close input/output stream.");
}
try{
if(comWriter != null){
comWriter.close();
comWriter = null;
}
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot close input/output stream.");
}
}
private void closeHttp() {
try{
if(reader != null){
reader.close();
reader = null;
}
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot close input/output stream.");
}
try{
if(writer != null){
writer.close();
writer = null;
}
} catch(IOException e) {
Pihatonttu.PihatonttuMain.showErrorDialog("Cannnot close input/output stream.");
}
if(hc != null){
hc.disconnect();
hc = null;
}
}
private void closeCOMPort(){
if(comPort != null) {
comPort.close();
comPort = null;
}
}
public void exitServer() {
closeCOM();
closeHttp();
closeCOMPort();
}
public void serialEvent(SerialPortEvent event) throws MalformedURLException, ProtocolException {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
try {
Pihatonttu.PihatonttuMain.togleIcon(true);
comReader = new DataInputStream(comPort.getInputStream());
comWriter = new DataOutputStream(comPort.getOutputStream());
requestMethod = "";
requestURI = "";
requestProtocol = "";
requestHeaderField.clear();
requestHost = "";
requestPort = 80;
readRequestHeader();
if(!requestMethod.equals("")) {
// loadPage();
accessTime = getAccessTime();
Pihatonttu.PihatonttuMain.logFrame.addElement("localhost - - [" + getAccessTime()+ "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\"");
// if(!requestProtocol.equals("RTSP/1.0")) {
sendRequest();
responseProtocol = "";
responseCode = "";
responseMessage = "";
byteTranfered = 0;
readResponseHeader();
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode);
forwardResponse();
if(responseCode.startsWith("30")) {
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " -");
} else {
Pihatonttu.PihatonttuMain.logFrame.addElementFraction("localhost - - [" + accessTime + "] \"" +
requestMethod + " " + requestURI + " " + requestProtocol + "\" " + responseCode + " " + byteTranfered);
}
// } else {
// sendStreamingRequest();
// readStreamingResponse();
// }
}
} catch(UnknownHostException e){
returnUnknownHostException(comWriter);
} catch(SocketTimeoutException e){
returnUnknownHostException(comWriter);
} catch (IOException e){
if(e.getMessage().indexOf("unknown protocol") != -1) returnUnknownProtocolException(comWriter);
else returnInternalServerException(comWriter);
} finally {
closeCOM();
closeHttp();
Pihatonttu.PihatonttuMain.togleIcon(false);
}
break;
}
}
public void start(){
Thread thread = new Thread(this);
thread.start();
}
public void run(){
String buffer = null;
System.out.flush();
}
private void readRequestHeader() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
int line = 0;
while((c = comReader.read()) != -1) {
if((char)c == '\r') {
if(sb.length() == 0) {
break;
} else {
processRequestHeader(sb.toString(), line);
sb.delete(0, sb.length());
line++;
}
} else if((char)c == '\n') {
} else {
sb.append((char)c);
}
}
sb = null;
if(requestMethod.equals("POST")) {
int dummy = comReader.read();
if(requestHeaderField.get("content-length") != null) {
int contentLength = Integer.valueOf((String)requestHeaderField.get("content-length")).intValue();
postData = new byte[contentLength];
comReader.read(postData, 0, contentLength);
}
}
}
private void processRequestHeader(String str, int line) throws IOException {
str = str.trim();
if(line == 0){
int delimitPos = str.indexOf(' ');
requestMethod = (str.substring(0, delimitPos)).trim();
str = (str.substring(delimitPos + 1)).trim();
delimitPos = str.indexOf(' ');
requestURI = (str.substring(0, delimitPos)).trim();
requestProtocol = (str.substring(delimitPos + 1)).trim();
} else {
int delimitPos = str.indexOf(':');
String key = ((str.substring(0, delimitPos)).trim()).toLowerCase();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -