Example #1
0
// 注册资料
func dockerGreetings(c *utils.Connection, data []byte) {
	c.Src = string(data)
	config.Dockers[string(data)] = time.Now().Unix()
	log.Println("Docker:", string(data), "is online.")
	log.Println("Dockers:", config.Dockers)
	c.SendCommandString("docker_greetings_reply", config.ClusterAddress)
}
Example #2
0
// docker连接到controller,保持着
func connectController(address string) {

	var (
		length     int
		exist      bool
		err        error
		cmd        string
		data       []byte
		payload    []byte
		conn       net.Conn
		handler    HandlerFunc
		connection *utils.Connection
	)

	defer func() { connCloseCh <- address }()

	log.Printf("Connect controller %s", address)

	conn, err = net.Dial("tcp", address)
	if err != nil {
		log.Println(err)
		waitGroup.Done()
		return
	}

	connection = &utils.Connection{
		Conn: conn,
		Src:  address,
	}
	ClusterSwitcher.register <- connection

	defer func() {
		conn.Close()
		ClusterSwitcher.unregister <- connection
	}()

	waitGroup.Done()
	connection.SendCommandString("docker_greetings", config.ClusterAddress)

	for {
		if length, data, err = connection.Read(); err != nil {
			break
		}
		cmd, payload = utils.CmdDecode(length, data)

		log.Printf("Cmd:%s", cmd)

		if handler, exist = ClusterSwitcher.handlers[cmd]; exist {
			handler(connection, payload)
		} else {
			log.Printf("Command[%s] is not exist", cmd)
		}
	}
	log.Printf("Controller %s is disconnect", address)
}
Example #3
0
// 我收了个小弟
// todo:是否要审核
// 由于其知道我的名字我默认相信他
func dockerJoin(c *utils.Connection, data []byte) {
	// 向集群结构配置里面添加新成员
	config.Dockers[string(data)] = time.Now().Unix()
	// 返回组织中领导层所有人姓名以便小弟有事时着他们
	b, err := json.Marshal(config.Controllers)
	if err != nil {
		// 我收集的资料有误
		c.SendCommandString("docker_join", "")
	} else {
		// 告知其组织的领导层所有人员姓名
		c.SendCommandBytes("docker_join", b)
	}
}
Example #4
0
// 结拜了个兄弟
func controllerJoin(c *utils.Connection, data []byte) {
	address := string(data)
	// 把他名字记下来
	config.Controllers[address] = time.Now().Unix()
	// 把我以前结拜的所有兄弟告诉他,让他们也认识一下
	b, err := json.Marshal(config.Controllers)
	if err != nil {
		log.Println(err)
		c.SendCommandString("controller_join", "")
	} else {
		c.SendCommandBytes("controller_join", b)
	}
	// 先断开连接,以免其收到广播我发给小弟的通知
	c.Conn.Close()
	// 告知我的所有小弟,我认了个兄弟,以后的进贡也要给他们一份

	message := fmt.Sprintf("%s %s", address, "controller_join_to_docker")
	ClusterSwitcher.Broadcast(utils.PacketString(message))
	log.Println("Controllers", config.Controllers)
}
Example #5
0
// 心跳
func heartbeat(c *utils.Connection, data []byte) {
	c.SendCommandString("heartbeat", config.ClusterAddress)
}