package de.haw.rnp.chat.networkmanager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPNode extends Node {
    private Socket clientSocket;
    private ServerSocket serverSocket;
    private PrintWriter out;
    private BufferedReader in;

    public TCPNode(int port, InetAddress hostName) {
        super(port, hostName);
    }

    public PrintWriter getOut() {
        return out;
    }

    public BufferedReader getIn() {
        return in;
    }

    public void startClientNode() {
        try {
            this.clientSocket = new Socket(this.hostName, this.port);
            this.out = new PrintWriter(this.clientSocket.getOutputStream(), true);
            this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Unknown host " + this.hostName.getHostAddress());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for " + this.hostName.getHostAddress());
            e.printStackTrace();
        }
    }

    public void stopClientNode()  {
        try {
            this.clientSocket.close();
            this.out.close();
            this.in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startServerNode() {
        try {
            this.serverSocket = new ServerSocket(this.port);
        } catch (IOException e) {
            System.err.println("Port is not available");
            e.printStackTrace();
        }

        try {
            this.clientSocket = this.serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Client Accept failed");
            e.printStackTrace();
        }

        System.out.println("Client connected to Server");

        try {
            this.out = new PrintWriter(this.clientSocket.getOutputStream(), true);
            this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        } catch (IOException e) {
            System.err.println("Trouble reading i/o");
            e.printStackTrace();
        }
    }

    public void stopServerNode() {
        try {
            this.out.close();
            this.in.close();
            this.serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -53,112 +53,9 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.io.PrintWriter
+*
 ;%0Aim
@@ -294,27 +294,28 @@
 private 
-PrintWriter
+OutputStream
  out;%0A  
@@ -446,27 +446,28 @@
  public 
-PrintWriter
+OutputStream
  getOut(
@@ -702,32 +702,16 @@
 s.out = 
-new PrintWriter(
 this.cli
@@ -729,39 +729,32 @@
 etOutputStream()
-, true)
 ;%0A            th
@@ -1204,17 +1204,16 @@
 ntNode()
- 
  %7B%0A     
@@ -1957,24 +1957,8 @@
 t = 
-new PrintWriter(
 this
@@ -1992,15 +1992,8 @@
 am()
-, true)
 ;%0A  

write directly byte array to output stream