// Create a new request object. // Values are all set to defaults func NewRequest(uri, method string) *Request { request := &Request{*dynmap.NewDynMap()} request.PutWithDot("strest.params", dynmap.NewDynMap()) request.PutWithDot("strest.v", StrestVersion) request.PutWithDot("strest.uri", uri) request.PutWithDot("strest.method", method) request.PutWithDot("strest.txn.accept", "single") return request }
func NewRouterTable(service string) *RouterTable { return &RouterTable{ Service: service, DynMap: dynmap.NewDynMap(), ReplicationFactor: 2, } }
// fills the passed in dynmap with the values from the yaml map func toDynMap(mp yaml.Map) *dynmap.DynMap { d := dynmap.NewDynMap() for k, v := range mp { d.Put(k, fromNode(v)) } return d }
func (this *Request) Params() *dynmap.DynMap { m, ok := this.GetDynMap("strest.params") if !ok { this.PutIfAbsentWithDot("strest.params", dynmap.NewDynMap()) m, ok = this.GetDynMap("strest.params") } return m }
// Create a new response object. // Values are all set to defaults func NewResponse() *Response { response := &Response{*dynmap.NewDynMap()} response.SetStatusMessage("OK") response.SetStatusCode(200) response.SetTxnStatus("completed") response.PutWithDot("strest.v", StrestVersion) return response }
// Translate to a DynMap of the form: // { // "service" : "trendrrdb", // "revision" : 898775762309309, // "total_partitions" : 256, // "entries" : [ // {/*router entry 1*/}, // {/*router entry 2*/} // ] // } func (this *RouterTable) ToDynMap() *dynmap.DynMap { if this.DynMap != nil && len(this.DynMap.Map) > 0 { return this.DynMap } mp := dynmap.NewDynMap() mp.Put("service", this.Service) mp.Put("revision", this.Revision) mp.Put("total_partitions", this.TotalPartitions) mp.Put("replication_factor", this.ReplicationFactor) entries := make([]*dynmap.DynMap, 0) for _, e := range this.Entries { entries = append(entries, e.ToDynMap()) } mp.Put("entries", entries) this.DynMap = mp return mp }
// 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 := &Response{*dynmap.NewDynMap()} err := decoder.Decode(res) if err == io.EOF { log.Print(err) break } else if err != nil { log.Print(err) break } this.incomingChan <- res } }
//loads the stored version func (this *Manager) load() error { bytes, err := ioutil.ReadFile(this.filename()) if err != nil { return err } mp := dynmap.NewDynMap() err = mp.UnmarshalJSON(bytes) if err != nil { return err } table, err := ToRouterTable(mp) if err != nil { return err } this.SetRouterTable(table) return nil }
// Translate to a DynMap of the form: // { // "id" : "localhost:8009" // "address" : "localhost", // "ports" : { // "json" : 8009, // "http" : 8010 // } // "partitions" : [1,2,3,4,5,6,7,8,9] // } func (this *RouterEntry) ToDynMap() *dynmap.DynMap { if this.DynMap != nil && len(this.DynMap.Map) > 0 { return this.DynMap } mp := dynmap.NewDynMap() mp.Put("address", this.Address) if this.JsonPort > 0 { mp.PutWithDot("ports.json", this.JsonPort) } if this.HttpPort > 0 { mp.PutWithDot("ports.http", this.HttpPort) } mp.Put("id", this.Id()) mp.Put("partitions", this.Partitions) this.DynMap = mp return mp }
func (this *HttpClient) ApiCallSync(req *Request, timeout time.Duration) (*Response, error) { uri := req.Uri() pms, err := req.Params().MarshalURL() if err != nil { return nil, err } reqBody := strings.NewReader("") if req.Method() == "GET" { joiner := "&" //add params to the uri if !strings.Contains(uri, "?") { joiner = "?" } uri = fmt.Sprintf("%s%s%s", uri, joiner, pms) } else { reqBody = strings.NewReader(pms) } url := fmt.Sprintf("http://%s%s", this.Address, uri) //convert to an http.Request request, err := http.NewRequest(req.Method(), url, reqBody) 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 var response = &Response{*dynmap.NewDynMap()} err = response.UnmarshalJSON(body) if err != nil { return nil, err } return response, nil }
// Creates a new server config with a default routematcher func NewServerConfig() *ServerConfig { return &ServerConfig{dynmap.NewDynMap(), NewDefaultRouter()} }