/* JSON EchoServer
 */
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"log"
)

type Person struct {
	Name  Name
	Email []Email
}

type Name struct {
	Family   string
	Personal string
}

type Email struct {
	Kind    string
	Address string
}

func main() {
	service := "0.0.0.0:1200"
	tcpAddr, err := net.ResolveTCPAddr("tcp", service)
	checkError(err)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		for n := 0; n < 10; n++ {
			var person Person
			buf, _ := readFully(conn)
			err = json.Unmarshal(buf, &person)

			fmt.Println(person)

			data, _ := json.Marshal(person)
			conn.Write(data)
		}
		conn.Close() // we're finished
	}
}

func readFully(conn net.Conn) ([]byte, error) {
	result := bytes.NewBuffer(nil)
	var buf [512]byte
	for {
		n, err := conn.Read(buf[0:])
		result.Write(buf[0:n])

		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, err
		}
		if n < 512 {
			break
		}
	}
	return result.Bytes(), nil
}

func checkError(err error) {
	if err != nil {
		log.Fatalln("Fatal error ", err.Error())
	}
}
