Пример #1
0
func ParseParams(paramEncoding int8, params []byte) (*dynmap.DynMap, error) {
	if len(params) == 0 {
		return dynmap.New(), nil
	}

	if paramEncoding == 0 {
		//json
		mp := dynmap.New()
		err := mp.UnmarshalJSON(params)
		return mp, err
	}
	return nil, fmt.Errorf("Unsupported param encoding %d", paramEncoding)
}
Пример #2
0
//creates a new response from a dynmap
func NewResponseDynMap(mp *dynmap.DynMap) *Response {
	strest := mp.MustDynMap("strest", dynmap.New())
	status := mp.MustDynMap("status", dynmap.New())
	mp.Remove("strest")
	mp.Remove("status")

	response := &Response{
		DynMap:        *mp,
		txnId:         strest.MustString("txn.id", ""),
		txnStatus:     strest.MustString("txn.status", "completed"),
		statusCode:    status.MustInt("code", 200),
		statusMessage: status.MustString("message", "OK"),
	}
	return response
}
Пример #3
0
//creates a new request from a dynmap
func NewRequestDynMap(mp *dynmap.DynMap) *Request {
	request := &Request{
		// version : mp.MustFloat32("strest.v", mp.StrestVersion), //TODO
		version:   StrestVersion,
		uri:       mp.MustString("strest.uri", ""),
		method:    mp.MustString("strest.method", "GET"),
		txnId:     mp.MustString("strest.txn.id", ""),
		txnAccept: mp.MustString("strest.txn.accept", "single"),
		params:    mp.MustDynMap("strest.params", dynmap.New()),
	}

	shardMp, ok := mp.GetDynMap("strest.shard")
	if ok {
		request.Shard = &ShardRequest{
			Partition: shardMp.MustInt("partition", -1),
			Key:       shardMp.MustString("key", ""),
			Revision:  shardMp.MustInt64("revision", int64(-1)),
		}
	} else {
		request.Shard = &ShardRequest{
			Partition: request.params.MustInt(PARAM_SHARD_PARTITION, -1),
			Key:       request.params.MustString(PARAM_SHARD_KEY, ""),
			Revision:  request.params.MustInt64(PARAM_SHARD_REVISION, int64(-1)),
		}
	}
	return request
}
Пример #4
0
func (this *BinDecoder) DecodeHello() (*dynmap.DynMap, error) {
	log.Println("DECODE HELLO")
	//read the hello
	helloEncoding := int8(0)
	err := binary.Read(this.reader, binary.BigEndian, &helloEncoding)
	if err != nil {
		return nil, err
	}

	hello, err := ReadByteArray(this.reader)
	if err != nil {
		log.Print(err)
		//TODO: Send bad hello.
		return nil, err
	}
	log.Printf(" HELLO %s", hello)
	this.Hello = dynmap.New()
	err = this.Hello.UnmarshalJSON(hello)
	if err != nil {
		log.Print(err)
		//TODO: Send bad hello.
		return nil, err
	}
	return this.Hello, nil
}
Пример #5
0
func newCheshireConn(protocol cheshire.Protocol, addr string, writeTimeout time.Duration, maxInFlight int) (*cheshireConn, error) {
	conn, err := net.DialTimeout("tcp", addr, time.Second)
	if err != nil {
		return nil, err
	}

	err = protocol.WriteHello(conn, dynmap.New())
	if err != nil {
		return nil, err
	}

	nc := &cheshireConn{
		Conn:         conn,
		connected:    1,
		addr:         addr,
		writeTimeout: writeTimeout,
		exitChan:     make(chan int),
		incomingChan: make(chan *cheshire.Response, 25),
		outgoingChan: make(chan *cheshireRequest, 25),

		inflightChan: make(chan bool, maxInFlight),
		requests:     make(map[string]*cheshireRequest),
		connectedAt:  time.Now(),
		protocol:     protocol,
		maxInFlight:  maxInFlight,
	}
	return nc, nil
}
Пример #6
0
// Creates a new dynmap with all the appropriate fields
// This is here for compatibility, but is rather inefficient
func (this *Request) ToDynMap() *dynmap.DynMap {
	request := dynmap.New()
	request.PutWithDot("strest.params", this.params)
	request.PutWithDot("strest.v", this.version)
	request.PutWithDot("strest.uri", this.uri)
	request.PutWithDot("strest.method", this.method)
	request.PutWithDot("strest.txn.accept", this.txnAccept)
	return request
}
Пример #7
0
func (this *JSONDecoder) DecodeRequest() (*Request, error) {
	mp := dynmap.New()
	err := this.dec.Decode(mp)
	if err != nil {
		return nil, err
	}
	req := NewRequestDynMap(mp)
	return req, nil
}
Пример #8
0
// Create a new request object.
// Values are all set to defaults
func NewRequest(uri, method string) *Request {
	request := &Request{
		version:   StrestVersion,
		uri:       uri,
		method:    method,
		txnAccept: "single",
		params:    dynmap.New(),
		Shard: &ShardRequest{
			Partition: -1,
		},
	}
	return request
}
Пример #9
0
func ToStrestRequest(req *http.Request) *Request {

	//print out the http request.
	// log.Println("*******************")
	// var doc bytes.Buffer
	// req.Write(&doc)
	// log.Println(doc.String())
	// log.Println("*******************")

	var request = NewRequest(req.URL.Path, req.Method)
	request.SetUri(req.URL.Path)
	request.SetMethod(req.Method)
	request.SetTxnId(req.Header.Get("Strest-Txn-Id"))
	request.SetTxnAccept(req.Header.Get("Strest-Txn-Accept"))
	if len(request.TxnAccept()) == 0 {
		request.SetTxnAccept("single")
	}

	//deal with the params

	params := dynmap.New()

	//we always do parse form since it will handle the
	// url params as well
	err := req.ParseForm()
	if err != nil {
		log.Printf("Error parsing form values: %s", err)
	}
	err = params.UnmarshalURLValues(req.Form)
	if err != nil {
		log.Printf("Error parsing form values: %s", err)
	}

	//now deal with possible different content types.
	ct := req.Header.Get("Content-Type")
	switch {
	case strings.Contains(ct, "json"):
		//parse as json
		bytes, err := ReadHttpBody(req)
		if err != nil {
			log.Printf("ERROR reading body %s", err)
		}
		err = params.UnmarshalJSON(bytes)
	}

	request.SetParams(params)
	return request
}
Пример #10
0
// loop that listens for incoming messages.
func (this *cheshireConn) listener() {
	decoder := json.NewDecoder(bufio.NewReader(this.Conn))
	log.Printf("Starting Cheshire Connection %s", this.addr)
	defer func() { this.exitChan <- 1 }()
	for {
		res := &cheshire.Response{*dynmap.New()}
		err := decoder.Decode(res)
		if err == io.EOF {
			log.Print(err)
			break
		} else if err != nil {
			log.Print(err)
			break
		}
		this.incomingChan <- res
	}
}
Пример #11
0
// Creates a new Stats tracker. caller must still envoke the Start function
// timeamounts should be in the form "{num} {timeframe}"
// example
// NewStats("1 minute", "30 minute", "1 day")
func New(timeamounts ...string) (*Stats, error) {
	s := &Stats{
		itemChan: make(chan statsItem, 5),
		getChan:  make(chan getRequest, 10),
		items:    make(map[timeamount.TimeAmount]*StatsSave),
	}
	for _, ta := range timeamounts {
		t, err := timeamount.Parse(ta)
		if err != nil {
			return nil, err
		}
		s.items[t] = &StatsSave{
			Epoch:      t.ToTrendrrEpoch(time.Now()),
			TimeAmount: t,
			Values:     dynmap.New(),
		}
	}
	return s, nil
}
Пример #12
0
func (this *Stats) add(item statsItem) {
	for ta, sts := range this.items {
		epoch := ta.ToTrendrrEpoch(time.Now())

		if epoch != sts.Epoch {
			// need to persist this..
			this.persist(sts)
			sts = &StatsSave{
				Epoch:      epoch,
				TimeAmount: ta,
				Values:     dynmap.New(),
			}
			this.items[ta] = sts
		}

		if item.typ == INC {
			val := sts.Values.MustInt64(item.Key, int64(0))
			sts.Values.PutWithDot(item.Key, int64(val+item.Val))
		} else if item.typ == SET {
			sts.Values.PutWithDot(item.Key, int64(item.Val))
		}

	}
}
Пример #13
0
func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	uri := req.Uri()

	var reqBody io.Reader

	if req.Method() == "GET" {
		joiner := "&"
		//add params to the uri
		if !strings.Contains(uri, "?") {
			joiner = "?"
		}

		pms, err := req.Params().MarshalURL()
		if err != nil {
			return nil, err
		}
		uri = fmt.Sprintf("%s%s%s", uri, joiner, pms)
	} else {
		// set the request body as json
		json, err := req.Params().MarshalJSON()
		if err != nil {
			return nil, err
		}

		// log.Printf("JSON %s", string(json))
		reqBody = bytes.NewReader(json)
	}

	url := fmt.Sprintf("http://%s%s", this.Address, uri)
	//convert to an http.Request
	request, err := http.NewRequest(req.Method(), url, reqBody)

	if req.Method() != "GET" {
		//set the content type
		request.Header.Set("Content-Type", "application/json")
	}

	if err != nil {
		return nil, err
	}
	res, err := http.DefaultClient.Do(request)

	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	//convert to a strest response2
	mp := dynmap.New()
	// var response = &cheshire.Response{*dynmap.NewDynMap()}
	err = mp.UnmarshalJSON(body)
	response := cheshire.NewResponseDynMap(mp)
	if err != nil {
		return nil, err
	}
	return response, nil
}