예제 #1
0
파일: main.go 프로젝트: runningwild/sgf
func MakeHost(addr string, port int) (HostEngine, error) {
	coreHost, err := core.MakeHost(addr, port)
	if err != nil {
		return nil, err
	}
	var h host
	h.Host = coreHost
	return &h, nil
}
예제 #2
0
func SimpleServerSpec(c gospec.Context) {
	c.Specify("Hook up all of the basic parts and make them talk.", func() {
		host, err := core.MakeHost("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		client0, err := core.MakeClient("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		client1, err := core.MakeClient("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		fmt.Printf("%v %v %v\n", host, client0)
		registerGameForAll(&Game{}, host, client0, client1)
		registerRequestForAll(Advance{}, host, client0, client1)
		registerUpdateForAll(Advance{}, host, client0, client1)
		registerRequestForAll(Join{}, host, client0, client1)
		registerUpdateForAll(Join{}, host, client0, client1)
		game := &Game{}
		host.Start(game)
		client0.Start()
		client1.Start()

		client0.GameMutex.Lock()
		game0 := client0.Game.(*Game)
		game0.myName = "foo"
		client0.GameMutex.Unlock()
		client0.MakeRequest(Join{Name: "foo"})

		client1.GameMutex.Lock()
		game1 := client1.Game.(*Game)
		game1.myName = "bar"
		client1.GameMutex.Unlock()
		client1.MakeRequest(Join{Name: "bar"})

		time.Sleep(time.Millisecond * 100)

		client0.GameMutex.RLock()
		fmt.Printf("game0: %v\n", client0.Game)
		client0.GameMutex.RUnlock()
		client1.GameMutex.RLock()
		fmt.Printf("game1: %v\n", client1.Game)
		client1.GameMutex.RUnlock()

		incs := 10
		var wg sync.WaitGroup
		for _, client := range []*core.Client{client0, client1} {
			wg.Add(1)
			go func(client *core.Client) {
				for i := 0; i < incs; i++ {
					time.Sleep(time.Millisecond)
					client.GameMutex.RLock()
					g := client.Game.(*Game)
					if g.me == nil {
						client.GameMutex.RUnlock()
						continue
					}
					adv := Advance{g.myIndex, g.me.Pos + 1}
					fmt.Printf("Request(%d, %d): %v -> %d\n", i, g.myIndex, *g.me, adv.NewPos)
					client.GameMutex.RUnlock()
					client.MakeRequest(adv)
					client.MakeMinorUpdate(adv)
				}
				wg.Done()
			}(client)
		}
		wg.Wait()
		time.Sleep(time.Millisecond * 100)

		client0.GameMutex.RLock()
		fmt.Printf("game0: %v\n", client0.Game)
		for _, player := range client0.Game.(*Game).Players {
			fmt.Printf("Player: %v\n", *player)
		}
		client0.GameMutex.RUnlock()
		client1.GameMutex.RLock()
		fmt.Printf("game1: %v\n", client1.Game)
		for _, player := range client1.Game.(*Game).Players {
			fmt.Printf("Player: %v\n", *player)
		}
		client1.GameMutex.RUnlock()

		// // for _, client := range []*core.Client{client0, client1} {

		// // }

		// client1.GameMutex.RLock()
		// fmt.Printf("game: %v\n", client1.Game)
		// client1.GameMutex.RUnlock()

		// adv := Advance{0, 2}
		// client0.MakeRequest(adv)
		// // client0.MakeMinorUpdate(adv)
		// time.Sleep(time.Millisecond * 100)

		// client1.GameMutex.RLock()
		// fmt.Printf("game: %v\n", client1.Game)
		// client1.GameMutex.RUnlock()

		// c.Expect(true, gospec.Equals, false)
	})
}