/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package walltest;

import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

class TCPClient {

    Socket clientSocket;
    DataOutputStream outToServer;
    BufferedReader inFromServer;

    public TCPClient(String address, int port) {
        try {
            clientSocket = new Socket("localhost", 7654);
            outToServer = new DataOutputStream(clientSocket.getOutputStream());
            inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            System.out.println("TCPClient connected.");
        } catch (Exception ex) {
            System.out.println("TCPClient could not connect to server.");
        }
    }

    //--------------------------------------------------------------------------
    public float[][] readMap() {
        LinkedList<float[]> list = new LinkedList<float[]>();
        try {
            outToServer.writeBytes("map\n");
            while (true) {
                String answer = inFromServer.readLine();
                if (answer.equals("end")) {
                    break;
                }
                Scanner sc = new Scanner(answer);
                float[] f = {sc.nextFloat(), sc.nextFloat(), sc.nextFloat(), sc.nextFloat()};
                list.addLast(f);
            }
        } catch (IOException ex) {
        }
        float[][] map = new float[list.size()][];
        int i = 0;
        for (float[] s : list) {
            map[i++] = s;
        }
        return (map);
    }

    //--------------------------------------------------------------------------
    public float[][] readRobots() {
        LinkedList<float[]> list = new LinkedList<float[]>();
        try {
            outToServer.writeBytes("robots\n");
            while (true) {
                String answerP = inFromServer.readLine();
                if (answerP.equals("end")) {
                    break;
                }
                String answerD = inFromServer.readLine();
                Scanner sc = new Scanner(answerP);
                String s = sc.next();
                float[] f = {sc.nextFloat(), sc.nextFloat(), sc.nextFloat()};
                list.addLast(f);
            }
        } catch (IOException ex) {
        }
        float[][] poses = new float[list.size()][];
        int i = 0;
        for (float[] p : list) {
            poses[i++] = p;
        }
        return (poses);
    }

    public static void main(String argv[]) throws Exception {
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost", 7654);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        while (true) {
            String sentence = inFromUser.readLine();
            outToServer.writeBytes(sentence + '\n');
            while (true) {
                String answer = inFromServer.readLine();
                System.out.println(answer);
                if (answer.equals("end")) {
                    break;
                }
            }
        }
        //clientSocket.close();
    }
}