Exemplo n.º 1
0
func HandleAddCustomEventHandlers(e *tree_event.Event, api_cmd Command) {
	var (
		out          = &WriterCallback{BufferMaxSize: 1024}
		handler_data map[string]interface{}
		err          tree_lib.TreeError
		ev_data      []byte
	)
	err.From = tree_lib.FROM_ADD_CUSTOM_EVENT
	err.Err = ffjson.Unmarshal(api_cmd.Data, &handler_data)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	out.OutCallback = func(data []byte, ended bool) {
		cb_cmd := api_cmd
		cb_cmd.Ended = ended
		cb_cmd.Data = data
		ev_data, err.Err = ffjson.Marshal(cb_cmd)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
		SendCommandCallback(e, ev_data)
	}
	defer out.End()

	event_name := handler_data["name"].(string)
	handles_interfaces := handler_data["handlers"].([]interface{})
	var (
		handles_interfaces_data []byte
		handlers                []custom_event.Handler
	)
	handles_interfaces_data, err.Err = ffjson.Marshal(handles_interfaces)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	err.Err = ffjson.Unmarshal(handles_interfaces_data, &handlers)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	err = custom_event.ON(event_name, handlers...)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}
}
Exemplo n.º 2
0
func main() {
	r := new(RetNode)
	r.Name = "sunsc"
	r.Sex = "male"
	r.Age = 36
	buf, err := json.Marshal(r)
	log.Println("err==>", err, "| buf=>", string(buf))

	secr := new(ReqCardInfoType)
	secr.ApiName = "test"
	secr.Id = "72789765231"
	secr.BizApiCode = "0005000400000001"
	buf, err = json.Marshal(secr)
	log.Println("err==>", err, "| buf=>", string(buf))
}
Exemplo n.º 3
0
// Executing some commands using exec.Command functionality from Go in OS
func HandleExecCommand(e *tree_event.Event, api_cmd Command) {
	var (
		out         = &WriterCallback{BufferMaxSize: 1024}
		cmd_str     = string(api_cmd.Data)
		cmd_options = strings.Split(cmd_str, " ")
		cmd         = exec.Command(cmd_options[0], cmd_options[1:]...)
		err         tree_lib.TreeError
		ev_data     []byte
	)
	err.From = tree_lib.FROM_HANDLE_EXEC_COMMAND
	out.OutCallback = func(data []byte, ended bool) {
		cb_cmd := api_cmd
		cb_cmd.Ended = ended
		cb_cmd.Data = data
		ev_data, err.Err = ffjson.Marshal(cb_cmd)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
		SendCommandCallback(e, ev_data)
	}

	defer out.End()

	cmd.Stdout = out
	cmd.Stderr = out
	err.Err = cmd.Run()
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}
}
Exemplo n.º 4
0
func SendCommand(cmd *Command, path *tree_graph.Path, cb func(*tree_event.Event, Command) bool) (err tree_lib.TreeError) {
	// If command ID not set just setting random string
	if len(cmd.ID) == 0 {
		cmd.ID = tree_lib.RandomString(10)
	}

	var (
		cmd_data []byte
	)

	cmd_data, err.Err = ffjson.Marshal(cmd)
	if !err.IsNull() {
		err.From = tree_lib.FROM_SEND_COMMAND
		return
	}

	e := &tree_event.Event{
		Name:    tree_event.ON_API_COMMAND,
		Data:    cmd_data,
		FromApi: node_info.CurrentNodeInfo.Value,
		From:    path.From,
	}

	tree_event.Emit(e, path)

	if cb != nil {
		subscribed_command_callbacks[cmd.ID] = cb_str{f: cb, c: make(chan bool)}
		<-subscribed_command_callbacks[cmd.ID].c
	}

	return
}
Exemplo n.º 5
0
func HandleContainerCommand(ev *tree_event.Event, cmd Command) {
	var (
		out        = &WriterCallback{BufferMaxSize: 1024}
		docker_cmd = tree_docker.DockerCmd{}
		err        tree_lib.TreeError
		ev_data    []byte
	)
	err.From = tree_lib.FROM_HANDLE_CONTAINER_COMMAND
	err.Err = ffjson.Unmarshal(cmd.Data, &docker_cmd)
	if !err.IsNull() {
		tree_log.Error(err.From, "unable to unmarshal command data as a docker command -> ", err.Error())
		return
	}
	out.OutCallback = func(data []byte, ended bool) {
		cb_cmd := cmd
		cb_cmd.Ended = ended
		cb_cmd.Data = data
		ev_data, err.Err = ffjson.Marshal(cb_cmd)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
		SendCommandCallback(ev, ev_data)
	}

	defer out.End()

	tree_docker.ContainerCommands(&docker_cmd, out)
}
Exemplo n.º 6
0
func FillDatabase(listApps []App) {

	for _, app := range listApps {
		appdb.Update(func(tx *bolt.Tx) error {
			b, err := tx.CreateBucketIfNotExists([]byte("AppBucket"))
			if err != nil {
				return fmt.Errorf("create bucket: %s", err)
			}

			serialize, err := json.Marshal(app)

			if err != nil {
				return fmt.Errorf("Error Marshaling data: %s", err)
			}
			err = b.Put([]byte(app.Guid), serialize)

			if err != nil {
				return fmt.Errorf("Error inserting data: %s", err)
			}
			return nil
		})

	}

}
Exemplo n.º 7
0
///从数据库pull消息
func GetMsgHandler(w http.ResponseWriter, req *http.Request) {
	//获取客户端通过GET/POST方式传递的参数
	var retmsg string
	req.ParseForm()
	param1, found1 := req.Form["topic"]
	param2, _ := req.Form["limit"]

	if !found1 {
		retmsg = "请勿非法访问"
	}

	topic := param1[0]
	limit, err := strconv.Atoi(param2[0])
	if err != nil {
		retmsg = "请勿非法访问"
	}

	if topic != "" {

		dblist := models.GetMessagesForField("topic", topic, limit)
		msgjson, _ := ffjson.Marshal(dblist)
		retmsg = string(msgjson)
	} else {

		retmsg = "topic is null"
	}
	io.WriteString(w, retmsg)

}
Exemplo n.º 8
0
func main() {
	fmt.Println("helloworld")
	DbaEnvrEx := buildEnvr()

	var listCard = make([]TCARD, 0)
	DbaEnvrEx.Select(&listCard, "SELECT  * FROM TBL_CARD")

	t0 := time.Now()
	//result, _ := json.Marshal(listCard)
	result, _ := ffjson.Marshal(listCard)
	t1 := time.Since(t0)
	fmt.Println(t1)
	fmt.Println(string(result))

	/*
		icount, ERR := DbaEnvrEx.SelectInt("SELECT COUNT(*) AS ICOUNT FROM TBL_CARD") //select count(*) as icount from tbl_card

		checkErr(ERR, "NULL")
		fmt.Println(icount)
	*/

	/*
		var listData = make([]TCARD, 0)
		_, eror := DbaEnvrEx.Select(&listData, "SELECT CARD_CODE,CARD_NAME,CARD_ZCTM,CARD_ZCYZ FROM TBL_CARD ORDER BY CARD_CODE")
		if eror != nil {
			fmt.Println(len(listData))
		}
	*/

}
Exemplo n.º 9
0
func PostMsgHandler(w http.ResponseWriter, req *http.Request) {
	//获取客户端通过GET/POST方式传递的参数
	req.ParseForm()
	param1, found1 := req.Form["sendmsg"]
	result := NewBaseJsonBean()

	if !found1 {
		result.Code = -99
		result.Message = "请勿非法访问"
	}

	fdata := param1[0]

	if fdata != "" {
		faction := "POST"
		furl := "http://nsq-ttthzygi35.tenxcloud.net:20157/put?topic=test"
		result.Code = 100
		result.Message = lib.HttpDo_NSQ(faction, furl, fdata)
	} else {
		result.Code = 101
		result.Message = "消息不能为空,请重新发送!"
	}

	//向客户端返回JSON数据,用到了ffjson包,据说比自带的json效率高3倍
	//bytes, _ := json.Marshal(result)
	bytes, _ := ffjson.Marshal(result)
	io.WriteString(w, string(bytes))

}
Exemplo n.º 10
0
func NetworkEmmit(e *tree_event.Event, path *tree_graph.Path) (err tree_lib.TreeError) {
	var (
		sdata []byte
		p     *big.Int
	)
	err.From = tree_lib.FROM_NETWORK_EMIT

	// Calling get value, because maybe some one will calculate this path before calling this functions
	// If path is not calculated yet, it will be automatically calculated in GetValue function
	p, err = path.GetValue()
	if !err.IsNull() {
		return
	}

	// If we emitting from API then we need to multiply path with connected node
	// For sending data through him
	if strings.Contains(node_info.CurrentNodeInfo.Name, tree_api.API_NAME_PREFIX) {
		p.Mul(p, node_info.ChildsNodeValue[path.From])
	}

	// If from not set, setting it before network sending
	if len(e.From) == 0 {
		e.From = node_info.CurrentNodeInfo.Name
	}

	sdata, err.Err = ffjson.Marshal(e)
	if !err.IsNull() {
		return
	}

	SendToPath(sdata, p)
	return
}
Exemplo n.º 11
0
func SetPathValues() (err tree_lib.TreeError) {
	var (
		nodes []node_info.NodeInfo
		prime int64
		value []byte
	)
	err.From = tree_lib.FROM_SET_PATH_VALUES
	nodes, err = ListNodeInfos()
	if !err.IsNull() {
		return
	}
	prime = 2
	for _, n := range nodes {
		n.Value = prime
		value, err.Err = ffjson.Marshal(n)
		if !err.IsNull() {
			return
		}
		err = Set(DB_NODE, []byte(n.Name), value)
		if !err.IsNull() {
			return
		}
		prime = tree_lib.NextPrimeNumber(prime)
	}
	return
}
Exemplo n.º 12
0
// Perform a B2 API request with the provided request and response objects
func (c *B2) apiRequest(apiPath string, request interface{}, response interface{}) error {
	body, err := ffjson.Marshal(request)
	if err != nil {
		return err
	}
	defer ffjson.Pool(body)

	if c.Debug {
		log.Println("----")
		log.Printf("apiRequest: %s %s", apiPath, body)
	}

	err = c.tryAPIRequest(apiPath, body, response)

	// Retry after non-fatal errors
	if b2err, ok := err.(*B2Error); ok {
		if !b2err.IsFatal() && !c.NoRetry {
			if c.Debug {
				log.Printf("Retrying request %q due to error: %v", apiPath, err)
			}

			return c.tryAPIRequest(apiPath, body, response)
		}
	}
	return err
}
Exemplo n.º 13
0
func HandleTriggerCustomEvent(e *tree_event.Event, api_cmd Command) {
	var (
		out        = &WriterCallback{BufferMaxSize: 1024}
		event_name = string(api_cmd.Data)
		err        tree_lib.TreeError
		ev_data    []byte
	)
	err.From = tree_lib.FROM_TRIGGER_CUSTOM_EVENT
	out.OutCallback = func(data []byte, ended bool) {
		cb_cmd := api_cmd
		cb_cmd.Ended = ended
		cb_cmd.Data = data
		ev_data, err.Err = ffjson.Marshal(cb_cmd)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
		SendCommandCallback(e, ev_data)
	}
	defer out.End()
	err = custom_event.Trigger(event_name, out)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}
}
Exemplo n.º 14
0
func toJSON(o interface{}) string {
	bytes, err := ffjson.Marshal(o)
	if err != nil {
		log.Fatal(err)
	}
	return string(bytes)
}
Exemplo n.º 15
0
func SendDockerCommand(cmd tree_docker.DockerCmd, node string, target []string, tag []string, group []string) {
	var err tree_lib.TreeError
	if !tree_api.API_INIT(node) {
		fmt.Println("Unable to init api client")
		fmt.Println("Exiting ...")
		return
	}
	err.From = tree_lib.FROM_SEND_DOCKER_COMMAND
	var (
		api_cmd = tree_api.Command{}
		wait    = make(chan bool)
	)

	api_cmd.ID = tree_lib.RandomString(20)
	api_cmd.Data, err.Err = ffjson.Marshal(cmd)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	api_cmd.CommandType = tree_api.COMMAND_CONTAINER

	tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) {
		path := &tree_graph.Path{From: node, Nodes: target, Groups: group, Tags: tag}
		tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool {
			fmt.Println(string(c.Data))
			fmt.Println(c.Ended)
			if c.Ended {
				return false
			}
			return true
		})
		wait <- true
	})
	<-wait
}
Exemplo n.º 16
0
func (this *Bucket) String() string {
	str, err := ffjson.Marshal(this)
	if err != nil {
		return "Error encoding to JSON: " + err.Error()
	} else {
		return string(str)
	}
}
Exemplo n.º 17
0
func StartMQTTSender(MQTTServerAddress string, MQTTSendChan chan *lib.PostMessage, channel_name string) chan bool {
	close_chan := make(chan bool)
	go func() {
		defer func() {
			if err := recover(); err != nil {
				utils.Log.Println(err)
				debug.PrintStack()
			}
		}()

		var post_message *lib.PostMessage
		var cli *client.Client
		var err error

		cli, err = ConnectToMQTTServer(MQTTServerAddress)
		if err != nil {
			utils.Log.Println("Connect to MQTT Server failed:", err)
			return
		}

		for {
			select {
			case <-close_chan:
				close_chan <- true
				utils.Log.Printf("Quit MQTT Sender worker.")
				return

			case post_message = <-MQTTSendChan:
				message_json_buffer, err := ffjson.Marshal(post_message)
				if err != nil {
					utils.Log.Println()
				}

				err = cli.Publish(&client.PublishOptions{
					QoS:       mqtt.QoS0,
					TopicName: []byte(channel_name),
					Message:   []byte(message_json_buffer),
				})

				if err != nil {
					utils.Log.Println("Publish to MQTT Failed:", err)
					utils.Log.Println("Try to connect MQTT Server.")
					cli, err = ConnectToMQTTServer(MQTTServerAddress)
					if err != nil {
						utils.Log.Println("Connect to MQTT Server failed:", err)
					}
				}

				utils.Log.Printf("Send message to MQTT Server: %s, Length: %d\n", MQTTServerAddress, len(message_json_buffer))

				ffjson.Pool(message_json_buffer)
			}
		}
	}()

	return close_chan
}
Exemplo n.º 18
0
///ESM推送
func RevMsgHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	//

	bytes, _ := ffjson.Marshal(lib.RevMsg)
	msg := "data:" + string(bytes) + "\n\n"
	io.WriteString(w, msg)

}
Exemplo n.º 19
0
func TestMarshalFaster(t *testing.T) {
	record := newLogFFRecord()
	_, err := ffjson.MarshalFast(record)
	require.NoError(t, err)

	r2 := newLogRecord()
	_, err = ffjson.MarshalFast(r2)
	require.Error(t, err, "Record should not support MarshalFast")
	_, err = ffjson.Marshal(r2)
	require.NoError(t, err)
}
Exemplo n.º 20
0
func main() {
	t1 := time.Now()
	defer func() {
		fmt.Println(time.Now().Sub(t1))
	}()
	m := mould.Input("input1.xml", "text1.htm")
	json, err := ffjson.Marshal(m)
	fmt.Println(string(json), err)
	mould.Output(m, "output.xml", "test2.html")

}
Exemplo n.º 21
0
func parseLine(line *[]byte) {
	var f nf
	var t time.Time
	var realRegionName, regionName string
	var record *geoip2.City

	err := ffjson.Unmarshal(*line, &f)
	if err != nil {
		*line = []byte("")
		log.Error(err, "couldn't unmarshal ", *line)
		return
	}

	if f.Srcip == nil {
		log.Error("Packet without source ip, shouldn't happen: ", string(*line))
		return
	}

	// use LRU cache
	if val, ok := geoipCache.Get(*f.Srcip); ok {
		record = val.(*geoip2.City)
	} else {
		ip := net.ParseIP(*f.Srcip)
		record, _ = geoipDB.City(ip)
		geoipCache.Add(*f.Srcip, record)
	}

	// add @timestamp with zulu (ISO8601 time)
	t, _ = time.ParseInLocation(nfLayout, *f.Timestamp, myLocation)
	f.Ltimestamp = t.UTC().Format(time.RFC3339Nano)

	if record.Location.Longitude != 0 && record.Location.Latitude != 0 {
		mylen := len(record.Subdivisions)
		if mylen > 0 {
			mylen--
			realRegionName = record.Subdivisions[mylen].Names["en"]
			regionName = record.Subdivisions[mylen].IsoCode
		}
		f.GeoIP.Longitude = &record.Location.Longitude
		f.GeoIP.Latitude = &record.Location.Latitude
		f.GeoIP.CountryName = record.Country.Names["en"]
		f.GeoIP.Timezone = &record.Location.TimeZone
		f.GeoIP.ContinentCode = &record.Continent.Code
		f.GeoIP.CityName = record.City.Names["en"]
		f.GeoIP.CountryCode2 = &record.Country.IsoCode
		f.GeoIP.RealRegionName = &realRegionName
		f.GeoIP.RegionName = &regionName
		f.GeoIP.IP = f.Srcip
		f.GeoIP.Location = &esGeoIPLocation{f.GeoIP.Longitude, f.GeoIP.Latitude}
		f.GeoIP.Coordinates = f.GeoIP.Location
	}

	*line, _ = ffjson.Marshal(f)
}
Exemplo n.º 22
0
Arquivo: kv.go Projeto: zhuharev/users
func (kv *KV) Set(key string, data interface{}) error {
	bts, e := ffjson.Marshal(data)
	if e != nil {
		return e
	}
	e = kv.levelDb.Put([]byte(key), bts, nil)
	if e != nil {
		return e
	}
	return nil
}
Exemplo n.º 23
0
// MarshalJSON will be called by json.Marshal it will return json
// representation of server's status.
func (status ServerStatus) MarshalJSON() ([]byte, error) {
	var (
		buffer bytes.Buffer
		data   []byte
		err    error
	)

	if status.Role == "master" {
		data, err = ffjson.Marshal(unencodable(status))
	} else {
		status.Role = "slave"
		data, err = ffjson.Marshal(status.BasicServerStatus)
	}

	err = json.Indent(&buffer, data, "", ServerStatusResponseIndentation)
	if err != nil {
		logger.Errorf("can't indent json %s: %s", data, err)
		return data, nil
	}

	return buffer.Bytes(), nil
}
Exemplo n.º 24
0
func SendJson(v interface{}, conn *net.TCPConn) (len int, err TreeError) {
	var (
		s_data []byte
	)
	err.From = FROM_SEND_JSON
	s_data, err.Err = ffjson.Marshal(v)
	if !err.IsNull() {
		return
	}

	len, err.Err = SendMessage(s_data, conn)
	return
}
Exemplo n.º 25
0
// Enqueue adds job to the queue.
func (c *Client) Enqueue(queue string, data ...interface{}) error {
	buf, err := ffjson.Marshal(backJob{Class: queue, Args: data})
	if err != nil {
		return err
	}
	rc := c.pool.Get()
	defer rc.Close()
	defer ffjson.Pool(buf)

	_, err = rc.Do("RPUSH", fmt.Sprintf("%vqueue:%v", c.prefix, queue), buf)
	return err

}
Exemplo n.º 26
0
func SetNodeInfo(name string, nf node_info.NodeInfo) (err tree_lib.TreeError) {
	var (
		value []byte
	)
	err.From = tree_lib.FROM_SET_NODE_INFO
	value, err.Err = ffjson.Marshal(nf)
	if !err.IsNull() {
		return
	}

	err = Set(DB_NODE, []byte(name), value)
	return
}
Exemplo n.º 27
0
// send a million documents
func ffmillion(w http.ResponseWriter, r *http.Request) {

	for i := 0; i < 10000000; i++ {
		docId := i % docs.number
		item := docs.docMap[docs.docList[docId]]
		bytes, err := ffjson.Marshal(item)
		if err != nil {
			fmt.Fprintf(w, "Failed to marshal document %v", err)
			return
		}
		fmt.Fprintf(w, string(bytes)) // send data to client side
		fmt.Fprintf(w, "\n\n")
		ffjson.Pool(bytes)
	}
}
Exemplo n.º 28
0
Arquivo: api.go Projeto: ReetiR/Go-API
func Index(w http.ResponseWriter, r *http.Request) {
	wg := sync.WaitGroup{}
	data := Data{}
	var res Respslice
	var c chan Resp = make(chan Resp)
	var temp Resp
	var keyString string
	keys := r.URL.Query().Get("key")
	if len(keys) != 0 {

		tmp := strings.Split(keys, ",")
		for _, element := range tmp {
			val, found := ch.Get(element)
			if !found {
				keyString += `'` + element + `'`
				continue
			}
			data.ID = element
			data.Path = val.(string)
			wg.Add(1)
			go computationWork(&wg, data, c)
			temp = <-c
			res.Resps = append(res.Resps, temp)
		}

		if len(keyString) != 0 {
			keyString = strings.Replace(keyString, `''`, `','`, -1)
			// keyString= `'`+strings.Replace(keys, `,`,`','`,-1)+`'`
			rows, _ := db.Query("SELECT `id`, `file_name` FROM DB.table WHERE `id` IN (" + keyString + ")")
			defer rows.Close()
			for rows.Next() {

				_ = rows.Scan(&data.ID, &data.Path)
				ch.Set(data.ID, data.Path, cache.DefaultExpiration)
				wg.Add(1)
				go computationWork(&wg, data, c)
				temp = <-c
				res.Resps = append(res.Resps, temp)
			}

		}
		wg.Wait()
		b, _ := ffjson.Marshal(res)
		w.Header().Set("Content-Type", "application/json")
		w.Write(b)
		// ffjson.Pool(b)
	}
}
Exemplo n.º 29
0
func UpdateNodeChange(info node_info.NodeInfo) {
	var (
		ev  = &tree_event.Event{}
		err tree_lib.TreeError
	)
	err.From = tree_lib.FROM_UPDATE_NODE_CHANGE
	ev.Data, err.Err = ffjson.Marshal(info)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	path := &tree_graph.Path{From: node_info.CurrentNodeInfo.Name, Nodes: []string{"*"}}
	ev.Name = tree_event.ON_UPDATE_NODE_INFO
	tree_event.Trigger(ev)
	tree_event.Emit(ev, path)
}
Exemplo n.º 30
0
func CreateOrUpdateJob(url string, job Job) (resp JobResp, err error) {

	buf, err := ffjson.Marshal(&job)
	if err != nil {
		return
	}

	body, err := sendPost(url+"v1/jobs", string(buf))
	if err != nil {
		return
	}

	data := []byte(body)
	err = ffjson.Unmarshal(data, &resp)

	return
}