/// 启动monitor.
///
/// 启动monitor时会首先拿到所有注册的module,并挂载到MonitorConsoleService上,
/// 然后对于设置类型为“push”的module定时调度其MonitoHandler方法, 为设置interval时
/// 默认定时调度时间5秒.
func (m *MonitorConsoleService) Start() {
	if m.status == SV_START {
		return
	}

	for _, v := range m.Context.Modules {
		m.ModuleMap[v.ModuleID()] = v
	}

	conn, err := m.monitorAgent.Connect()
	if err != nil {
		fmt.Fprintf(os.Stdout, "Error:Fail connect to master host:%v post:%v ", m.Context.MasterInfo["host"], m.Context.MasterInfo["port"])
		os.Exit(1)
	}

	go m.handleConnection(conn)

	for _, mod := range m.ModuleMap {
		if mod != nil {
			if mod.GetType() == "push" {
				interval := mod.GetInterval()

				if interval == 0 {
					interval = 5
				}

				go module.PeriodicScheduler(mod.MonitorHandler, m.monitorAgent, interval)
			}
		}
	}

}
/// 启动MasterConsoleService.
///
/// 启动时,首先开启监听,遍历所有挂载的模块,如果模块配置类型为pull或者push,这开启定时
/// 调度,默认调度时间为5秒. 然后启动所有module.
func (m *MasterConsoleService) Start() {
	if m.status == SV_START {
		fmt.Println("Info: Master Server is started already")
		return
	}
	// for _, v := range m.Context.Modules {
	// 	if
	// 	m.ModuleMap[v.ModuleID()] = v
	// }

	host, ok := m.Context.MasterInfo["host"]
	if !ok {
		fmt.Fprintf(os.Stderr, "Error: Master host not set\n")
		os.Exit(1)
	}

	port, ok := m.Context.MasterInfo["port"]

	if !ok {
		fmt.Fprintf(os.Stderr, "Error: Master port not set\n")
		os.Exit(1)
	}

	host_s := host.(string)
	fmt.Println(port)
	port_i := port.(int)

	hostAndPort := host_s + ":" + strconv.Itoa(port_i)
	go m.Listen(hostAndPort)

	for _, v := range m.Context.Modules {
		if v != nil {
			if v.GetType() == "pull" {
				interval := v.GetInterval()
				if interval == 0 {
					interval = 5
				}
				go module.PeriodicScheduler(v.MasterHandler, m.MAgent, interval)
			}
		}
	}

	for _, v := range m.Context.Modules {
		if v != nil {
			v.Start()
		}
	}
}