mirror of
https://github.com/gnh1201/caterpillar.git
synced 2025-09-07 18:31:10 +00:00
readFromRemoteServer()
This commit is contained in:
parent
b83e332bc0
commit
ecf1628ee6
|
@ -14,10 +14,9 @@ import org.json.JSONObject;
|
||||||
import javax.servlet.jsp.JspWriter;
|
import javax.servlet.jsp.JspWriter;
|
||||||
|
|
||||||
public class Worker {
|
public class Worker {
|
||||||
private static final int DEFAULT_SOCKET_TIMEOUT = 5000;
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
private static class JsonRpc2 {
|
private static class JsonRpc2 {
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class Call {
|
public class Call {
|
||||||
public Call(Map<String, String> params, String id, String method) {
|
public Call(Map<String, String> params, String id, String method) {
|
||||||
this.params = params;
|
this.params = params;
|
||||||
|
@ -33,7 +32,6 @@ public class Worker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class Error {
|
public class Error {
|
||||||
public Error(Map<String, String> error, String id) {
|
public Error(Map<String, String> error, String id) {
|
||||||
this.error = error;
|
this.error = error;
|
||||||
|
@ -47,7 +45,6 @@ public class Worker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class Result {
|
public class Result {
|
||||||
public Result(Map<String, String> result, String id) {
|
public Result(Map<String, String> result, String id) {
|
||||||
this.result = result;
|
this.result = result;
|
||||||
|
@ -89,18 +86,42 @@ public class Worker {
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readFromRemoteServer(String remoteAddress, int remotePort, String scheme, byte[] requestData, object out, int bufferSize, String id) {
|
private static readFromRemoteServer(String remoteAddress, int remotePort, String scheme, byte[] requestData, object _out, int bufferSize, String id) {
|
||||||
Socket sock = new Socket();
|
try {
|
||||||
sock.connect(new InetSocketAddress(remoteAddress, remotePort));
|
Socket sock = new Socket();
|
||||||
DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream());
|
sock.connect(new InetSocketAddress(remoteAddress, remotePort));
|
||||||
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream());
|
||||||
|
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
|
||||||
|
|
||||||
if (out instanceof JspWriter) {
|
outToServer.write(requestData, 0, sendData.length);
|
||||||
// todo
|
|
||||||
} else if (out instanceof Socket) {
|
char[] buffer = new char[bufferSize];
|
||||||
// todo
|
int bytesRead;
|
||||||
} else {
|
StringBuilder response = new StringBuilder();
|
||||||
// todo
|
while ((bytesRead = inFromServer.read(buffer, 0, bufferSize)) != -1) {
|
||||||
|
response.append(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out instanceof JspWriter) {
|
||||||
|
JspWriter out = (JspWriter) _out;
|
||||||
|
out.println(response.toString());
|
||||||
|
} else if (out instanceof Socket) {
|
||||||
|
Socket conn = (Socket) _out;
|
||||||
|
conn.getOutputStream().write(response.toString().getBytes());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Map<String, Object> error = new HashMap<>();
|
||||||
|
error.put("status", 502);
|
||||||
|
error.put("code", e.getMessage());
|
||||||
|
error.put("message", e.getMessage());
|
||||||
|
String response = new JsonRpc2.Error(error, id);
|
||||||
|
if (out instanceof JspWriter) {
|
||||||
|
JspWriter out = (JspWriter) _out;
|
||||||
|
out.println(response.toString());
|
||||||
|
} else if (out instanceof Socket) {
|
||||||
|
Socket conn = (Socket) _out;
|
||||||
|
conn.getOutputStream().write(response.toString().getBytes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user