Ejemplo n.º 1
0
func (self *PiAssistant) StartService() {
	for _, serviceConfi := range self.piAssiConf.ServicesConfig {
		if serviceConfi.Autostart {
			service := self.ServiceMgr.GetService(serviceConfi.ServiceId)
			if service != nil {
				startErr := service.StartService()
				if startErr != nil {
					l4g.Error("%s service start error: %v", service.GetServiceId(), startErr)
					return
				}
				l4g.Info("%s start successful!", service.GetServiceId())
			}
		}
	}
	l4g.Info("Start services successful!")

	// connect xmpp server
	connectError := self.connectXmppServer()
	if connectError != nil {
		l4g.Error("Connect xmpp server error: %v", connectError)
		return
	}
	l4g.Info("Xmpp is connected!")

	self.connErrorHandler = xmpp.NewConnErrorHandler()
	self.xmppClient.AddHandler(self.connErrorHandler)

	self.chathandler = xmpp.NewChatHandler()
	self.xmppClient.AddHandler(self.chathandler)

	self.subscribeHandler = xmpp.NewSubscribeHandler()
	self.xmppClient.AddHandler(self.subscribeHandler)

	//make sure will receive roster and subscribe message
	self.xmppClient.RequestRoster()
	//make resource available
	self.xmppClient.Send(&xmpp.Presence{})

	stopService := false
	for !stopService {
		select {
		case connEvent := <-self.connErrorHandler.GetEventCh():
			self.handleConnErr(connEvent)
		case event := <-self.chathandler.GetEventCh():
			self.handle(event.Stanza.(*xmpp.Message))
		case subsEvent := <-self.subscribeHandler.GetEventCh():
			self.handleSubscribe(subsEvent.Stanza.(*xmpp.Presence))
		case pushMsg := <-self.pushMsgCh:
			self.handlePushMsg(pushMsg)
		case <-self.stopCh:
			stopService = true
		}
	}
}
Ejemplo n.º 2
0
func (self *PiAssistant) initServices() error {
	for _, serviceConfi := range self.piAssiConf.ServicesConfig {
		if serviceConfi.Autostart {
			service := self.ServiceMgr.GetService(serviceConfi.ServiceId)
			if service != nil {
				initErr := service.Init(serviceConfi.ConfigRaw, self.pushMsgCh)
				if initErr != nil {
					return errors.New(fmt.Sprintf("%s init error: %v", service.GetServiceId(), initErr))
				}
				l4g.Info("%s initialize successful!", service.GetServiceId())
			}
		}
	}
	return nil
}