Example #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())
	}
}
Example #2
0
// Looks for an error message in the response body and parses it into a
// B2Error object
func (c *B2) parseError(body []byte) error {
	b2err := &B2Error{}
	if ffjson.Unmarshal(body, b2err) != nil {
		return nil
	}
	return b2err
}
Example #3
0
func GetUsername(authorization string) (username, token string, err error) {

	var data TokenData
	var dataSp []string
	var body []byte

	token, err = GetAccessToken(authorization)
	if err != nil {
		return
	}

	body, err = GetUsernameFromToken(token)
	if err != nil {
		return
	}

	err = ffjson.Unmarshal(body, &data)
	if err != nil {
		return
	}

	dataSp = strings.Split(data.Aeris, "+")
	if len(dataSp) != 2 {
		err = errors.New("username errror.")
		return
	}

	username = "******" + dataSp[1]
	return
}
Example #4
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)
}
Example #5
0
func (server *Server) wsSubscribe(conn *websocket.Conn, p *proto.Proto) (userid int64, topics []int32, token string, err error) {
	if err = p.ReadWebsocket(conn); err != nil {
		return
	}
	if p.OpCode != define.OP_SUB_REQ {
		err = ErrOperation
		return
	}

	sub := new(proto.SubREQ)
	err = ffjson.Unmarshal(p.Body, sub)
	if err != nil {
		return
	}
	sub.Server = server.Config.Id
	if err = server.Subscribe(sub); err != nil {
		return
	}
	userid = sub.UserId
	topics = sub.Topics
	token = sub.Token
	p.Body = nil
	p.OpCode = define.OP_SUB_RES
	err = p.WriteWebsocket(conn)
	return
}
Example #6
0
func (g *Geocoder) Geocode(q, countryCode string, offline bool) (
	*jstruct.Location, error) {

	res, err := g.geocodeFromCache(q, countryCode)
	if err != nil || res != nil || offline {
		return res, err
	}
	r, err := g.rawGeocode(q, countryCode)
	if err != nil {
		return nil, err
	}
	defer r.Close()
	data, err := ioutil.ReadAll(&io.LimitedReader{
		R: r,
		N: 4 * 1024 * 1024,
	})
	if err != nil {
		return nil, err
	}
	res = &jstruct.Location{}
	err = ffjson.Unmarshal(data, res)
	if err != nil {
		return nil, err
	}
	key, _ := makeKeyAndCountryCode(q, countryCode)
	err = g.cache.Put(key, data, buildLocation(res))
	if err != nil {
		return nil, err
	}
	return res, err
}
Example #7
0
func ParseConfigFile(file string) (conf TreeScaleConf, err tree_lib.TreeError) {
	err.From = tree_lib.FROM_PARSE_CONFIG_FILE
	switch strings.Replace(filepath.Ext(file), ".", "", 1) {
	case "toml":
		{
			_, err.Err = toml.DecodeFile(file, &conf)
		}
	case "yaml":
		{
			var fdata []byte
			fdata, err.Err = ioutil.ReadFile(file)
			if !err.IsNull() {
				return
			}
			err.Err = yaml.Unmarshal(fdata, &conf)
		}
	case "json":
		{
			var fdata []byte
			fdata, err.Err = ioutil.ReadFile(file)
			if !err.IsNull() {
				return
			}
			err.Err = ffjson.Unmarshal(fdata, &conf)
		}
	}

	return
}
Example #8
0
//json转map
func Json2map(jsonString string) (s map[string]interface{}, err error) {
	var result map[string]interface{}
	if err := ffjson.Unmarshal([]byte(jsonString), &result); err != nil {
		return nil, err
	}
	return result, nil
}
Example #9
0
func GetParentInfo(node string) (node_info.NodeInfo, tree_lib.TreeError) {
	var (
		err   tree_lib.TreeError
		pname string
	)
	err.From = tree_lib.FROM_GET_PARENT_INFO

	err = ForEach(DB_NODE, func(key []byte, val []byte) error {
		n := node_info.NodeInfo{}
		err := ffjson.Unmarshal(val, &n)
		if err != nil {
			return err
		}

		if _, ok := tree_lib.ArrayContains(n.Childs, node); ok {
			pname = n.Name
			return errors.New("Just Err for break")
		}

		return nil
	})

	if len(pname) == 0 {
		return node_info.NodeInfo{}, tree_lib.TreeError{}
	}

	// Node relations first element should be parent node
	return GetNodeInfo(pname)
}
Example #10
0
// broadcast :  push message to all users
func (h *HttpServer) PushAll(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}
	var (
		bodyBytes []byte
		err       error
	)
	defer r.Body.Close()
	if bodyBytes, err = ioutil.ReadAll(r.Body); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	msg := &proto.PushMsg{}
	err = ffjson.Unmarshal(bodyBytes, msg)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if err := DefaultMQ.Broadcast(msg); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	return
}
Example #11
0
// push message of topic to users who subscribe the topic
func (h *HttpServer) PushTopic(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}
	var (
		bodyBytes []byte
		err       error
	)
	defer r.Body.Close()
	if bodyBytes, err = ioutil.ReadAll(r.Body); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	reqmsg := &PushTopicREQ{}
	err = ffjson.Unmarshal(bodyBytes, reqmsg)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	servers := DefaultServer.GetTopicServers(reqmsg.Topic)
	if len(servers) == 0 {

		http.Error(w, "no servers sub the topic", http.StatusInternalServerError)
		return
	}
	if err = DefaultMQ.BroadcastTopic(servers, reqmsg.Topic, reqmsg.Msg); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	return
}
Example #12
0
// Request function for send requests to betfair via REST JSON
func (b *Betting) Request(reqStruct interface{}, url BetURL, method string, filter *Filter) error {
	req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()

	urlBuild := bytes.NewBuffer([]byte{})
	urlBuild.WriteString(string(url))
	urlBuild.WriteString("/")
	urlBuild.WriteString(method)
	urlBuild.WriteString("/")

	req.SetRequestURI(urlBuild.String())
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Application", b.ApiKey)
	req.Header.Set("X-Authentication", b.SessionKey)
	req.Header.SetMethod("POST")

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

		req.SetBody(filterBody)
	}

	err := fasthttp.Do(req, resp)
	if err != nil {
		return err
	}

	if resp.StatusCode() == 400 {
		err = ffjson.Unmarshal(resp.Body(), &bettingError)
		if err != nil {
			return err
		}

		return fmt.Errorf("Error with code - %s and string - %s", bettingError.Faultcode, bettingError.Faultstring)
	}

	err = ffjson.Unmarshal(resp.Body(), reqStruct)
	if err != nil {
		return err
	}

	return nil
}
func BenchmarkFFJsonSmall(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var data SmallPayload
		ffjson.Unmarshal(smallFixture, &data)

		nothing(data.Uuid, data.Tz, data.Ua, data.St)
	}
}
Example #14
0
func enumerateStoredOffers(store *Store,
	callback func(offer *jstruct.JsonOffer, do *DeletedOffer) error) error {

	// Enumerate deleted offers
	ids, err := store.ListDeletedIds()
	if err != nil {
		return err
	}
	for _, id := range ids {
		deletedIds, err := store.ListDeletedOffers(id)
		if err != nil {
			return err
		}
		for _, deleted := range deletedIds {
			data, err := store.GetDeleted(deleted.Id)
			if err != nil {
				return err
			}
			js := &jstruct.JsonOffer{}
			err = ffjson.Unmarshal(data, js)
			if err != nil {
				return err
			}
			err = callback(js, &deleted)
			if err != nil {
				return err
			}
		}
	}
	// Enumerate valid offers
	ids, err = store.List()
	if err != nil {
		return err
	}
	for _, id := range ids {
		offer, err := getStoreJsonOffer(store, id)
		if err != nil {
			return err
		}
		if offer == nil {
			fmt.Printf("skipping %s\n", id)
			data, err := store.Get(id)
			if err != nil {
				fmt.Printf("error %s: %s\n", id, err)
			} else if len(data) == 0 {
				fmt.Printf("error %s: nil\n", id)
			} else {
				fmt.Printf("error %s: %s\n", id, string(data))
			}
			continue
		}
		err = callback(offer, nil)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #15
0
//  push
// does invalidate msg, if it is invalid, ignore it
func (c *CometRpc) PushMQ(msg []byte) (err error) {
	m := &proto.MQMsg{}
	if err = ffjson.Unmarshal(msg, m); err != nil {
		log.Errorf("json.Unmarshal(%s) error(%s)", msg, err)
		return
	}
	c.push(m)
	return
}
Example #16
0
func (b *esBackend) TimeFromLine(line *[]byte) string {
	var f nf
	err := ffjson.Unmarshal(*line, &f)
	if err != nil {
		return time.Now().UTC().Format(b.ctx.cfg.Backend["es"].Index)
	}
	t, _ := time.Parse(time.RFC3339Nano, f.Ltimestamp)
	return t.Format(b.ctx.cfg.Backend["es"].Index)
}
Example #17
0
func HandleUpdateCommand(ev *tree_event.Event, cmd Command) {
	var (
		data Command
		info node_info.NodeInfo
		err  tree_lib.TreeError
	)
	err.From = tree_lib.FROM_HANDLE_UPDATE_COMMAND
	err.Err = ffjson.Unmarshal(ev.Data, &data)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}
	err.Err = ffjson.Unmarshal(data.Data, &info)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}
	UpdateNodeChange(info)
	SendCommandCallback(ev, ev.Data)
}
Example #18
0
func HandlPostReciveFirst(c *gin.Context) (bodyStr, code string) {
	body, err := ioutil.ReadAll(c.Request.Body)
	CheckError(err)
	templCode := TemplCode{}
	ffjson.Unmarshal(body, &templCode)
	//    c.String(200, string(body))
	//    fmt.Println("打印值", string(body))
	bodyStr = string(body)
	code = templCode.Code
	return
}
Example #19
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)
}
Example #20
0
func Status(url string) (resp StatusResp, err error) {

	body, err := send(url+"v1/", "")
	if err != nil {
		return
	}
	buf := []byte(body)
	err = ffjson.Unmarshal(buf, &resp)

	return
}
Example #21
0
func getUpstreamStatus(propagation *RequestPropagation) UpstreamStatus {
	var (
		successes = propagation.ResponsesSuccess()
		errors    = propagation.ResponsesError()

		status = UpstreamStatus{
			Total: len(successes) + len(errors),
			Error: len(errors),
		}
	)

	for _, response := range successes {
		var slave ServerStatus

		err := ffjson.Unmarshal([]byte(response.ResponseBody), &slave)
		if err != nil {
			logger.Errorf(
				"can't decode JSON response from %s: %s, response:\n%s",
				response.Slave, err, response.ResponseBody,
			)

			err := NewError(
				err,
				"can't decode JSON response",
			)

			slave.Error = err.Error()
			slave.HierarchicalError = err.HierarchicalError()

			status.Error++
		} else {
			status.Success++
		}

		slave.Address = string(response.Slave)

		status.Slaves = append(status.Slaves, slave)
	}

	for _, response := range errors {
		status.Slaves = append(status.Slaves, ServerStatus{
			BasicServerStatus: BasicServerStatus{
				Address:           string(response.Slave),
				Error:             response.Error(),
				HierarchicalError: response.HierarchicalError(),
			},
		})
	}

	status.ErrorPercent = percent(status.Error, status.Total)
	status.SuccessPercent = percent(status.Success, status.Total)

	return status
}
Example #22
0
func getStoreJsonOffer(store *Store, id string) (*jstruct.JsonOffer, error) {
	data, err := store.Get(id)
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, nil
	}
	js := &jstruct.JsonOffer{}
	err = ffjson.Unmarshal(data, js)
	return js, err
}
Example #23
0
func MemberList(url string) (resp []Member, err error) {

	body, err := send(url+"v1/members", "")
	if err != nil {
		return
	}

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

	return
}
Example #24
0
func ExecutionJobList(url string, jobName string) (resp []ExecutionJobResp, err error) {

	body, err := send(url+"v1/executions/"+jobName, "")
	if err != nil {
		return
	}

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

	return
}
Example #25
0
func RunJob(url string, jobName string) (resp JobResp, err error) {

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

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

	return
}
func BenchmarkFFJsonMedium(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var data MediumPayload
		ffjson.Unmarshal(mediumFixture, &data)

		nothing(data.Person.Name.FullName, data.Person.Github.Followers, data.Company)

		for _, el := range data.Person.Gravatar.Avatars {
			nothing(el.Url)
		}
	}
}
Example #27
0
func ShowJobs(url string) (resp []JobResp, err error) {

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

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

	return
}
Example #28
0
func TestUnmarshalFaster(t *testing.T) {
	buf := []byte(`{"id": 123213, "OriginId": 22, "meth": "GET"}`)
	record := newLogFFRecord()
	err := ffjson.UnmarshalFast(buf, record)
	require.NoError(t, err)

	r2 := newLogRecord()
	err = ffjson.UnmarshalFast(buf, r2)
	require.Error(t, err, "Record should not support UnmarshalFast")
	err = ffjson.Unmarshal(buf, r2)
	require.NoError(t, err)
}
Example #29
0
func GetNodeInfo(name string) (nf node_info.NodeInfo, err tree_lib.TreeError) {
	var (
		value []byte
	)
	err.From = tree_lib.FROM_GET_NODE_INFO
	value, err = Get(DB_NODE, []byte(name))
	if !err.IsNull() {
		return
	}

	err.Err = ffjson.Unmarshal(value, &nf)
	return
}
Example #30
0
func ListNodeInfos() (nfs []node_info.NodeInfo, err tree_lib.TreeError) {
	err.From = tree_lib.FROM_LIST_NODE_INFOS
	err = ForEach(DB_NODE, func(key []byte, val []byte) error {
		n := node_info.NodeInfo{}
		err := ffjson.Unmarshal(val, &n)
		if err != nil {
			return err
		}
		nfs = append(nfs, n)
		return nil
	})
	return
}