package pl.edu.elka.raj;

import pl.edu.elka.models.Message;
import pl.edu.elka.models.Node;
import pl.edu.elka.util.Log;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by carol on 20/04/2016.
 */
public class TCPClient implements Runnable {

    private String address;
    private int port;
    private Socket clientSocket;
    private Node server;

    public TCPClient(String server) {
        String[] serverArray = server.split(":");
        address = serverArray[0];
        port = Integer.parseInt(serverArray[1]);

    }

    public Node getServer() {
        return server;
    }

    public void setServer(Node server) {
        this.server = server;
    }

    public void run() {
        try {
            clientSocket = new Socket();
            clientSocket.connect(new InetSocketAddress(address, port), 1000);
            server = new Node(null, clientSocket, true);
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            Message messageSent = new Message(Message.Type.HANDSHAKE, Main.pid, null, null);
            server.write(messageSent);
            while (!clientSocket.isClosed()) {
                NetworkController.processMessage(server, inFromServer.readLine());
            }
        } catch (Exception e) {
            Log.LogError(Log.SUBTYPE.CLIENTSOCKET, "Message: " + e.getMessage());
        } finally {
            NetworkController.restartClient();
        }
    }
}

@@ -5,43 +5,30 @@
 age 
-pl.edu.elka.raj;%0A%0Aimport pl.edu.elk
+Agrawalas;%0A%0Aimport jav
 a.mo
@@ -48,26 +48,19 @@
 %0Aimport 
-pl.edu.elk
+jav
 a.models
@@ -77,24 +77,18 @@
 ort 
-pl.edu.elka.u
+java.U
 til
+s
 .Log
@@ -172,51 +172,8 @@
 t;%0A%0A
-/**%0A * Created by carol on 20/04/2016.%0A */%0A
 publ

Update TCPClient.java