Exemple #1
0
func InitPeer() (err error) {
	u, err := url.Parse(*upstream)
	if err != nil {
		return
	}
	u.Path = defaultWSURL
	conn, err := net.Dial("tcp", u.Host)
	if err != nil {
		return
	}
	wsclient, _, err = websocket.NewClient(conn, u, nil, 1024, 1024)
	if err != nil {
		return
	}

	// Get slave name from master
	_, port, _ := net.SplitHostPort(*address)
	wsclient.WriteJSON(map[string]string{
		"action": "LOGIN",
		"token":  *token,
		"port":   port,
	})
	var msg = make(map[string]string)
	if err = wsclient.ReadJSON(&msg); err != nil {
		return err
	}
	if me, ok := msg["self"]; ok {
		if pool == nil {
			pool = groupcache.NewHTTPPool(me)
		}
		peers := strings.Split(msg["peers"], ",")
		m := msg["mirror"]
		mirror = &m
		log.Println("Self name:", me)
		log.Println("Peer list:", peers)
		log.Println("Mirror site:", *mirror)
		pool.Set(peers...)
	} else {
		return errors.New("'peer_name' not found in master response")
	}

	// Listen peers update
	go func() {
		for {
			err := wsclient.ReadJSON(&msg)
			if err != nil {
				if state.IsClosed() {
					break
				}
				log.Println("Connection to master closed !!!")
				for {
					log.Println("> retry in 5 seconds")
					time.Sleep(time.Second * 5)
					if err := InitPeer(); err == nil {
						break
					}
				}
				break
			}
			action := msg["action"]
			switch action {
			case "PEER_UPDATE":
				peers := strings.Split(msg["peers"], ",")
				log.Println("Update peer list:", peers)
				pool.Set(peers...)
			}
		}
	}()

	// send queue
	go func() {
		for msg := range sendc {
			log.Println("Send msg:", msg)
			if msg["action"] == nil {
				msg["action"] = "LOG"
			}
			err := wsclient.WriteJSON(msg)
			if err != nil {
				log.Println("Send queue err:", err)
				break
			}
		}
	}()

	return nil
}
Exemple #2
0
func InitPeer(masterAddr string, listenAddr string, cachedir string, token string) (err error) {
	u, err := url.Parse(masterAddr)
	if err != nil {
		return
	}
	u.Path = defaultWSURL
	conn, err := net.Dial("tcp", u.Host)
	if err != nil {
		return
	}
	wsclient, _, err = websocket.NewClient(conn, u, nil, 1024, 1024)
	if err != nil {
		return
	}

	// sync slaves name from master
	_, port, _ := net.SplitHostPort(listenAddr)
	wsclient.WriteJSON(map[string]string{
		"action": ACTION_LOGIN,
		"token":  token,
		"port":   port,
	})
	var msg = make(map[string]string)
	if err = wsclient.ReadJSON(&msg); err != nil {
		return err
	}
	if me, ok := msg["self"]; ok {
		if pool == nil {
			pool = groupcache.NewHTTPPool(me)
		}
		peers := strings.Split(msg["peers"], ",")
		mirror := msg["mirror"]
		log.Println("Self name:", me)
		log.Println("Peer list:", peers)
		log.Println("Mirror site:", mirror)
		pool.Set(peers...)

		http.HandleFunc("/", NewFileHandler(false, mirror, cachedir))
	} else {
		return errors.New("'peer_name' not found in master response")
	}

	// Listen peers update
	go func() {
		for {
			err := wsclient.ReadJSON(&msg)
			if err != nil {
				if state.IsClosed() {
					break
				}
				log.Println("Connection to master closed !!!")
				break
				// for {
				// 	log.Println("> retry in 5 seconds")
				// 	time.Sleep(time.Second * 5)
				// 	if err := InitPeer(masterAddr, listenAddr, cachedir, token); err == nil {
				// 		break
				// 	}
				// }
				// break
			}
			action := msg["action"]
			switch action {
			case ACTION_PEER_UPDATE:
				peers := strings.Split(msg["peers"], ",")
				log.Println("Update peer list:", peers)
				pool.Set(peers...)
			}
		}
		os.Exit(1)
	}()

	// send queue
	go func() {
		for msg := range sendc {
			//log.Println("Send msg:", msg)
			if msg["action"] == nil {
				msg["action"] = ACTION_LOG
			}
			err := wsclient.WriteJSON(msg)
			if err != nil {
				log.Println("Send queue err:", err)
				break
			}
		}
	}()
	return nil
}