package weatherhandler;

import weatherhandler.parser.WeatherParser;
import weatherhandler.processor.Processor;
import weatherhandler.processor.DBStorageProcessor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer implements Runnable {
    private Socket socket;
    private ServerSocket TCPsocket;

    @Override
    public void run() {
        System.out.println("Server started");
        try {
            TCPsocket = new ServerSocket(7789);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            try {
                socket = TCPsocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            new Thread(new ServerHandler(socket)).start();
            System.out.println("New Client");
        }
    }

    public void interrupt() {
        try {
            TCPsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


class ServerHandler implements Runnable {

    private Socket socket;

    public ServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        String line;
        StringBuilder lines = new StringBuilder();
        Processor processor = new DBStorageProcessor("weather_measurements");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while ((line = in.readLine()) != null) {
                lines.append(line);
                if (line.contains("</WEATHERDATA>")) {
                    new WeatherParser(lines.toString(), processor);
                    lines.setLength(0);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();

            // Close connection
            try {
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }
}

@@ -105,16 +105,71 @@
 cessor;%0A
+import weatherhandler.processor.BatchUpdatesProcessor;%0A
 import w
@@ -686,32 +686,161 @@
 ce();%0A        %7D%0A
+%0A        Processor processor = new BatchUpdatesProcessor(1000,%0A                new DBStorageProcessor(%22weather_measurements%22));%0A%0A
         while (t
@@ -990,32 +990,33 @@
 ;%0A            %7D%0A
+%0A
             new 
@@ -1015,19 +1015,26 @@
     
-new T
+Runnable t
 hread
-(
+ = 
 new 
@@ -1053,17 +1053,59 @@
 r(socket
-)
+, processor);%0A            new Thread(thread
 ).start(
@@ -1331,17 +1331,16 @@
    %7D%0A%7D%0A%0A
-%0A
 class Se
@@ -1396,24 +1396,57 @@
 cket socket;
+%0A    private Processor processor;
 %0A%0A    public
@@ -1473,16 +1473,37 @@
 t socket
+, Processor processor
 ) %7B%0A    
@@ -1524,24 +1524,60 @@
 t = socket;%0A
+        this.processor = processor;%0A
     %7D%0A%0A    @
@@ -1685,86 +1685,8 @@
 ();%0A
-        Processor processor = new DBStorageProcessor(%22weather_measurements%22);%0A
     
@@ -1995,16 +1995,21 @@
 ring(), 
+this.
 processo

use 1 batched DB storage processor for all threads