func TestConnection(t *testing.T) {

	ack_channel := make(chan state.Ack, 100)
	state_channel := make(chan state.StateResponse, 5)
	physics := processor.DefaultPhysics()
	factory := processor.NewFactory(&physics)
	manager := game.NewManager(physics.NewGameState(), state_channel, &factory)
	router := game.CommandRouter{GameManager: &manager, Acks: ack_channel}
	go manager.Start()

	reciever := network.UdpReceiver{
		Router:     router,
		Acks:       ack_channel,
		Responses:  state_channel,
		PortNumber: ":10002"}

	reciever.Run()
	conn, err := net.DialTimeout("udp", "127.0.0.1:10002", 1*time.Second)
	Convey("Test connect", t, func() {
		So(err, ShouldEqual, nil)
		So(conn, ShouldNotEqual, nil)
	})

	Convey("Test Connect command", t, func() {
		a := cmd.NewConnect("Dan")
		a.UniqueId = "abc123"
		message, err := json.Marshal(a)

		So(err, ShouldEqual, nil)

		i, error := conn.Write(message)

		So(i, ShouldBeGreaterThan, 0)
		So(error, ShouldEqual, nil)
	})

	Convey("Test State Responder", t, func() {
		reciever.Responses <- state.StateResponse{State: physics.NewGameState()}
		response := make([]byte, 2048)
		// read response
		conn.SetDeadline(time.Now().Add(2 * time.Second))
		n, err := bufio.NewReader(conn).Read(response)
		So(err, ShouldEqual, nil)
		So(string(response[:n]), ShouldEqual, "abc123")

		conn.SetDeadline(time.Now().Add(2 * time.Second))
		n, err = bufio.NewReader(conn).Read(response)
		So(err, ShouldEqual, nil)

		encoder := encoder.JsonEncoderDecoder{}

		_, encode_error := encoder.Decode(response[:n])
		So(encode_error, ShouldEqual, nil)

	})

}
Exemplo n.º 2
0
func main() {

	/*
		f, err := os.OpenFile("testlogfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			//f.Fatalf("error opening file: %v", err)
		}
		defer f.Close()

		log.SetOutput(f)
		log.Println("This is a test log entry")
	*/
	var waiter sync.WaitGroup
	waiter.Add(1)
	log.Println("Server starting up")
	decoder := encoder.JsonEncoderDecoder{}
	log.Println("Encoder created")

	ackChannel := make(chan state.Ack, 100)
	log.Println("Acking channel created")

	stateChannel := make(chan state.StateResponse)
	log.Println("State channel created")

	physics := processor.DefaultPhysics()
	log.Println("Default Physics created")

	factory := processor.NewFactory(&physics)
	log.Println("Command Processor Factory created")

	current_state := physics.NewGameState()

	gameManager := game.NewManager(current_state, stateChannel, &factory)
	log.Println("Gamemanager created")

	router := game.CommandRouter{Acks: ackChannel, GameManager: &gameManager}
	log.Println("Router created")

	receiver := network.UdpReceiver{PortNumber: ":10001",
		MaxPacket:      8192,
		EncoderDecoder: &decoder,
		Router:         router,
		Acks:           ackChannel,
		Responses:      stateChannel}

	log.Println("Udp reciever created")
	go gameManager.Start()
	log.Println("Gamemanager started")

	receiver.Run()
	log.Println("Udp reciever running, press ctr+c to shutdown")

	waiter.Wait()
	log.Println("Shutting down")

}