Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import org.example.configFiles.Config;
- import org.example.configFiles.Route;
- import org.example.configFiles.Service;
- import java.io.*;
- import java.net.Socket;
- public class Client implements Runnable {
- private Socket clientSocket;
- private Socket serviceSocket;
- private Config configFile;
- private ByteArrayOutputStream requestBytes;
- private Service service;
- /**
- * Constructor for the client connection.
- *
- * @param clientSocket Socket of the client who started the connection.
- */
- public Client(Socket clientSocket, Config configFile) {
- this.clientSocket = clientSocket;
- this.configFile = configFile;
- }
- /**
- * Reads the input stream and write it into the output stream.
- *
- * @param input The input stream, where the data are read.
- * @param output The output stream, where the data are written.
- */
- private void copyData(InputStream input, OutputStream output) {
- byte[] buffer = new byte[4096];
- int byteRead;
- try {
- while ((byteRead = input.read(buffer)) != -1) {
- output.write(buffer, 0, byteRead);
- }
- output.flush();
- } catch (Exception e) {
- System.out.println("Reading stream error: " + e.getMessage());
- }
- }
- /**
- *
- * @param request The http header where the HttpRequestHandler object will find the path
- * @return
- */
- public Service findRoutes(String request) {
- HttpRequestHandler handler = new HttpRequestHandler(request);
- String path = handler.getPath();
- for(Route r : configFile.getRoutes())
- {
- if(r.getPath().equals(path)){
- return r.getService().getFirst();
- }
- }
- System.out.println("No route: " + path);
- return null;
- }
- /**
- * Method that is responsible for the comunication between the client e the service host.
- * Open the socket for the service, get the input streams and the output streams of both client and service sockets and swap them using the copyData() method.
- */
- public void run() {
- System.out.println("New connection from: " + clientSocket.getRemoteSocketAddress());
- try {
- clientSocket.setKeepAlive(true);
- InputStream fromClient = clientSocket.getInputStream();
- OutputStream toClient = clientSocket.getOutputStream();
- // --------------------------------------------------------
- // Routing
- // --------------------------------------------------------
- // Read a chunk of 4096 bytes of the HTTP request
- requestBytes = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int bytesRead;
- bytesRead = fromClient.read(buffer);
- if (bytesRead == -1) {
- System.out.println("Empty client request.");
- return;
- }
- requestBytes.write(buffer, 0, bytesRead);
- // Parsing to a string the chunks of bytes
- String requestLine = requestBytes.toString("UTF-8").split("\r\n")[0];
- System.out.println("First line: " + requestLine);
- // Find the correct host and port
- service = findRoutes(requestLine);
- if (service == null) {
- System.out.println("No route: " + requestLine);
- return;
- }
- System.out.println("Connection to " + service.getHost() + ":" + service.getPort());
- serviceSocket = new Socket(service.getHost(), service.getPort());
- InputStream fromService = serviceSocket.getInputStream();
- OutputStream toService = serviceSocket.getOutputStream();
- // Write the bytes already read
- System.out.println("Http request: " + requestBytes);
- toService.write(requestBytes.toByteArray());
- toService.flush();
- // --------------------------------------------------------
- // Proxy loop full-duplex
- // --------------------------------------------------------
- Thread serviceToClient= new Thread(() -> copyData(fromService, toClient));
- serviceToClient.start();
- Thread clientToService = new Thread(() -> copyData(fromClient, toService));
- clientToService.start();
- clientToService.join();
- serviceToClient.join();
- } catch (Exception e) {
- System.out.println("Error: " + e.getMessage());
- } finally {
- try {
- if (!clientSocket.isClosed() && clientSocket != null)
- clientSocket.close();
- if (!serviceSocket.isClosed() && serviceSocket != null)
- serviceSocket.close();
- } catch (IOException e) {
- System.out.println("Socket closing error: " + e.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment