Ejemplo n.º 1
0
//run service
func (this *CsvInput) Run() {
	log.Infoln("CsvInput is starting")
	log.Infof("open file %s", this.Config.FileName)

	g := float64(this.Config.Period)
	if bts, err := ioutil.ReadFile(this.Config.FileName); err == nil {
		strs := strings.Split(string(bts), "\n")
		for common.IsRun {
			beginget := time.Now()
			mod := this.TIME.Minute() % 3
			//			log.Infof("w count is %d", mod+1)
			for i := 0; i <= mod; i++ {
				row := new(common.DataRows)
				row.Time = time.Now().Format(this.Config.TimeFormat)
				row.RowMap = this.RowMap
				for _, str := range strs {
					if this.Config.OutputColumn == nil {
						log.Infoln("output column is nil")
						break
					}
					cols := strings.Split(str, ",")
					var r []interface{}
					for _, c := range cols {
						if d, err := strconv.ParseFloat(c, 10); err == nil {
							r = append(r, d)
						} else {
							r = append(r, 0)
						}
					}
					row.Rows = append(row.Rows, r)
				}
				this.Output <- row
				//log.Infof("push row is %v", row.Time)
			}
			//计算下一次运行的时间

			this.TIME = this.TIME.Add(time.Minute * time.Duration(g))
			log.Infof("next time is %v", this.TIME)
			sec := time.Since(beginget).Seconds()

			//计算下一次运行的时间
			if sec < g*60 { //如果一个处理周期内处理完,就延时,没有处理完就立即执行,如果正式运行期间还需要进行时间纠正
				log.Infof("sleep seconds %f", (g*60)-sec)
				time.Sleep(time.Second * (time.Duration(g*60 - sec))) //sleep one minute-sec
			}
		}

	} else {
		log.Error(err.Error())
	}
	close(this.Output)
}
Ejemplo n.º 2
0
//将客户端从在线列表中删除
func Delete(name string) {
	if c, ok := onlineClient[name]; ok {
		var cc ClientClose
		cc = c
		cc.Close()
		delete(onlineClient, name)
		log.Infof("client: %s close and remove from online", c.Name)
	}
}
Ejemplo n.º 3
0
//启动处理引擎
func Start(conf *config.Config) {
	log.Infof("dispatcher is starting")
	common.IsRun = true
	common.IsDebug = conf.IsDebug
	createDataPeriod(conf)
	service.Start(conf)
	createInput(conf)
	createSummary(conf)
	//cron.Start(conf)
	Run()
}
Ejemplo n.º 4
0
func (this *RequestData) GetStringArray(key string) []string {
	if v, ok := this.Param[key]; ok {
		if v, ok := v.([]interface{}); ok {
			re := []string{}
			for _, s := range v {
				re = append(re, compute.AsString(s))
			}
			return re
		} else {
			log.Infof("param %s value error", key, v)
		}
	}
	return nil
}
Ejemplo n.º 5
0
//init
func init() {
	log.Info("Session is started,expired every %d seconds", Expired)
	go func() {
		var i int
		timer := time.Tick(time.Minute)
		for t := range timer {
			i = 0
			lock.Lock()
			for k, s := range sessions {
				if s.IsExpired() {
					delete(sessions, k)
					i = i + 1
				}
			}
			lock.Unlock()
			log.Infof("clear %d sessions at %v,used %v", i, t, time.Since(t))
		}
	}()
}
Ejemplo n.º 6
0
// login in
func pid_1(cmd *common.RequestData, c *online.Client) {
	if checkParamError(cmd, c, "Login", "Password") {
		return
	}

	login := cmd.GetString("Login")
	pwd := cmd.GetString("Password")
	c.Name = login
	log.Infof("user %s login", c.Name, common.IsDebug)
	loginSuccess := false
	if common.IsDebug { //调试时直接登陆,不验证密码
		loginSuccess = true
	} else {
		param := make(map[string]interface{})
		param["login"] = login
		_, epwd, err := rpc.BySqlParamName("login", param)
		if err != nil {
			log.Error(err.Error())
		}
		if err == nil && len(epwd) > 0 {
			loginSuccess = epwd[0][0] == pwd //需要对密码进行加密再进行比较
		}
	}

	//check
	re := make(map[string]interface{})
	if loginSuccess {
		c.IsLogin = true
		c.Key = common.HashString(c.UUID + ":" + c.Name)
		online.SetSession(c.UUID, c.Name, c.Key)
		re["LoginState"] = 0
		re["Message"] = "login success"
		re["UID"] = c.UUID
		re["Key"] = c.Key
		online.Set(c)
	} else {
		re["LoginState"] = -1
		re["Message"] = "login failed"
	}
	writepid(c, cmd, 0, re)
}
Ejemplo n.º 7
0
//run service
func (this *TestInput) Run() {
	log.Infoln("TestInput is starting")
	g := float64(this.Config.Period)
	for common.IsRun {
		beginget := time.Now()
		for i := 0; i < 60; i++ {
			row := new(common.DataRows)
			row.RowMap = this.RowMap
			if this.Config.OutputColumn == nil {
				log.Infoln("output column is nil")
				break
			}
			rnd := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
			for j := 0; j < 100000; j++ {
				var r []interface{}
				if this.Config.OutputColumn.Sum != nil {
					for k := 0; k < len(this.Config.OutputColumn.Sum); k++ {
						r = append(r, rnd.Float32())
					}
				}
				if this.Config.OutputColumn.Max != nil {
					for k := 0; k < len(this.Config.OutputColumn.Max); k++ {
						r = append(r, rnd.Float32())
					}
				}
				if this.Config.OutputColumn.Min != nil {
					for k := 0; k < len(this.Config.OutputColumn.Min); k++ {
						r = append(r, rnd.Float32())
					}
				}

				if this.Config.OutputKey != nil {
					for _, k := range this.Config.OutputKey {
						var v interface{}
						if k == this.Config.TimeKey {
							v = time.Now()
						} else {
							v = i * j
						}
						r = append(r, v)
					}
				}
				row.Rows = append(row.Rows, r)
			}

			row.Time = this.TIME.Format(this.Config.TimeFormat)

			//log.Infof("testinput 正在输出数据...%v", row.Time)
			this.Output <- row

		}
		//计算下一次运行的时间

		this.TIME = this.TIME.Add(time.Minute * time.Duration(g))
		log.Infof("next time is %v", this.TIME)
		//计算时间间隔
		sec := time.Since(beginget).Seconds()

		//计算下一次运行的时间
		if sec < g*60 { //如果一个处理周期内处理完,就延时,没有处理完就立即执行,如果正式运行期间还需要进行时间纠正
			log.Infof("sleep seconds %f", (g*60)-sec)
			time.Sleep(time.Second * (time.Duration(g*60 - sec))) //sleep one minute-sec
		}

	}
	close(this.Output)
}
Ejemplo n.º 8
0
//处理单独的Http请求
func (this *HttpService) HandleHttp(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	uid := req.Header.Get(Sec_Auth_Uid)
	key := req.Header.Get(Sec_Auth_Key)
	c := this.getClient(uid, key)
	defer c.Close()

	if data := req.FormValue("json"); len(data) > 0 {
		cmd := new(common.RequestData)
		c.Read([]byte(data), cmd)
		protocol.Exec(cmd, &c.Client)
	} else {
		cmd := common.ResponseData{0, -1, "json string is empty", ""}
		c.Send(&cmd)
	}
	for {
		timeout := time.After(time.Nanosecond * 10) //10ns就超时
		select {
		case data := <-c.OutputChan:
			if _, err := w.Write(data); err != nil {
				log.Error("%s send cmd error and login out,%s\n", c.Name, err.Error())
			}
			break
		case <-timeout:
			goto end
		}
	}
end:
}

//按cookei取在线用户
func (this *HttpService) getClient(uid string, key string) *HttpClient {
	c := new(HttpClient)
	if s, _, ok := online.GetSession(uid); ok && s == key { //key相同时认为已登陆过的
		if tc, ok := online.Get(uid); ok { //第一个登陆的Client的uuid被当作了uid
			c.Name = tc.Name
			c.IsLogin = tc.IsLogin
			c.IsPlayBack = c.IsPlayBack
		}
	}
	if c == nil {
		c = new(HttpClient)
	}
	c.UUID = uuid.New()
	c.IsRun = true
	c.OutputChan = make(chan []byte, 10)
	return c
}

////设置cookie
//func (this *HttpService) setCookie(c *HttpClient, w http.ResponseWriter, age int) {
//	id := &http.Cookie{Name: "monitor_http_key", Value: common.HashString(c.Name + ":monitor"), Path: "/", MaxAge: age, HttpOnly: true, Secure: true}
//	name := &http.Cookie{Name: "monitor_http_name", Value: c.Name, Path: "/", MaxAge: age, HttpOnly: true, Secure: true}
//	http.SetCookie(w, id)
//	http.SetCookie(w, name)
//}

//处理Socket请求
func (this *HttpService) HandleSocket(ws *websocket.Conn) {
	connFrom := ws.RemoteAddr()
	log.Infof("accept new http client from %s\n", connFrom)
	uid := ws.Request().Header.Get(Sec_Auth_Uid)
	key := ws.Request().Header.Get(Sec_Auth_Key)
	c := this.getClient(uid, key)
	c.Connect = ws
	go this.HandleResponse(c)
	this.HandleRequest(c)
}

//主运行方法
//根目录为标准Http请求
//socket为Socket请求
func (this *HttpService) Run() {
	this.IsRun = true
	http.Handle("/", http.FileServer(http.Dir("."))) // <-- note this line
	http.HandleFunc("/http", this.HandleHttp)
	http.Handle("/socket", websocket.Handler(this.HandleSocket))
	log.Infof("http service started at %s:%d", this.Host, this.Port)
	log.Info("http socket host on /http and /socket ")
	if err := http.ListenAndServe(fmt.Sprintf("%s:%d", this.Host, this.Port), nil); err != nil {
		log.Error("http service start error", err.Error())
	}
}

//send cmd to client
func (this *HttpService) HandleResponse(c *HttpClient) {
	var err error
	defer online.Delete(c.UUID)
	for cmd := range c.OutputChan {
		//cmd := <-c.OutputChan
		cmd = append(cmd, '\n')
		if err = websocket.Message.Send(c.Connect, string(cmd)); err != nil {
			log.Error("%s send cmd error and login out,%s\n", c.Name, err.Error())
			c.IsRun = false
			break
		}
		c.UpdateTime()
	}
}

//handle tcp request
func (this *HttpService) HandleRequest(c *HttpClient) {
	var err error
	defer online.Delete(c.UUID)
	cmd := new(common.RequestData)
	var data []byte
	for this.IsRun && c.IsRun {
		if err = websocket.Message.Receive(c.Connect, &data); err != nil {
			log.Infof("%s can't received cmd", c.Name, err.Error())
			c.IsRun = false
			break
		}
		c.Read(data, cmd)
		protocol.Exec(cmd, &c.Client)
		c.UpdateTime()
	}
}