Example #1
1
func NewSocketLogWriter(proto, hostport string) SocketLogWriter {
	sock, err := net.Dial(proto, hostport)
	if err != nil {
		fmt.Fprintf(os.Stderr, "NewSocketLogWriter(%q): %s\n", hostport, err)
		return nil
	}

	w := SocketLogWriter(make(chan *LogRecord, LogBufferLength))

	go func() {
		defer func() {
			if sock != nil && proto == "tcp" {
				sock.Close()
			}
		}()

		for rec := range w {
			// Marshall into JSON
			js, err := json.Marshal(rec)
			if err != nil {
				fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err)
				return
			}

			_, err = sock.Write(js)
			if err != nil {
				fmt.Fprint(os.Stderr, "SocketLogWriter(%q): %s", hostport, err)
				return
			}
		}
	}()

	return w
}
Example #2
0
func (ts *Tribserver) CreateUser(args *tribproto.CreateUserArgs, reply *tribproto.CreateUserReply) os.Error {
	//check if user exists
	_, stat, err := ts.tm.GET(args.Userid + LAST)
	if err != nil {
		return err
	}

	//return if exists already
	if stat == storageproto.OK {
		log.Printf("create user: user already exists")
		reply.Status = tribproto.EEXISTS

	} else {
		log.Printf("Creating new user")
		//initialize the 3 maps for the user
		val, _ := json.Marshal(0)
		ts.tm.PUT(args.Userid+LAST, val)
		val, _ = json.Marshal(make(map[string]bool))
		ts.tm.PUT(args.Userid+SUBSCRIPTIONS, val)
		log.Printf("FInished creating new user %s", args.Userid)
	}

	log.Printf("returning nil from create user")
	return nil
}
Example #3
0
func TestOT(t *testing.T) {
	text1 := "{\"_rev\":0, \"_data\":{\"$object\":true, \"abc\":\"xyz\", \"num\": 123, \"foo\":{\"$object\":true, \"a\":1,\"b\":3}, \"arr\":{\"$array\":[1,2,3]}}}"
	dest1 := make(map[string]interface{})
	err := json.Unmarshal([]byte(text1), &dest1)
	if err != nil {
		t.Errorf("Cannot parse: %v", err)
		return
	}

	text2 := "{\"_rev\":0, \"_data\":{\"$object\":true, \"torben\":\"weis\", \"num\": 321, \"foo\":{\"$object\":true, \"a\":6,\"c\":33}, \"arr\":{\"$array\":[4,5,6]}}}"
	dest2 := make(map[string]interface{})
	err = json.Unmarshal([]byte(text2), &dest2)
	if err != nil {
		t.Errorf("Cannot parse: %v", err)
		return
	}

	m1 := DocumentMutation(dest1)
	m2 := DocumentMutation(dest2)
	var ot Transformer
	err = ot.Transform(m1, m2)
	if err != nil {
		t.Errorf("Cannot transform: %v", err)
		return
	}

	enc1, _ := json.Marshal(dest1)
	log.Println("t1=", string(enc1))
	enc2, _ := json.Marshal(dest2)
	log.Println("t2=", string(enc2))
}
Example #4
0
func (p *Point) MarshalJSON() ([]byte, os.Error) {
	x, ex := json.Marshal(p.x)
	if ex != nil {
		return nil, ex
	}
	y, ey := json.Marshal(p.y)
	if ey != nil {
		return nil, ey
	}
	return []byte(fmt.Sprintf("{\"x\":%s,\"y\":%s}", x, y)), nil
}
Example #5
0
func TestQuorumStrings(t *testing.T) {
	out, err := json.Marshal(QUORUM)
	fatalIf(t, err != nil, "Couldn't marshal QuorumValue: %v", err)
	fatalIf(t, string(out) != `"quorum"`, "Got wrong value: '%s'", out)
	out, err = json.Marshal(ALL)
	fatalIf(t, err != nil, "Couldn't marshal QuorumValue: %v", err)
	fatalIf(t, string(out) != `"all"`, "Got wrong value: '%s'", out)
	out, err = json.Marshal(QuorumValue(4))
	fatalIf(t, err != nil, "Couldn't marshal QuorumValue: %v", err)
	fatalIf(t, string(out) != `4`, "Got wrong value: '%s'", out)
}
Example #6
0
func (self QuorumValue) MarshalJSON() (out []byte, err os.Error) {
	switch self {
	case QUORUM:
		out, err = json.Marshal("quorum")
	case ALL:
		out, err = json.Marshal("all")
	default:
		out, err = json.Marshal(int(self))
	}
	return
}
func main() {
session, err := mgo.Mongo("localhost") // comma separated
	if err != nil {
		panic(err)
	}
	defer session.Close() // should keep connection pool perhaps?

	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)

	c := session.DB("test").C("people")
	err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
		&Person{"Cla", "+55 53 8402 8510"})
	if err != nil {
		panic(err)
	}

	result := Person{}
	err = c.Find(bson.M{}).One(&result) 
	if err != nil {
		panic(err)
	}
	bytes, err2 := json.Marshal(result)
	if err2 != nil {
		panic(err2)
	}
	fmt.Println("person:", string(bytes))
	
	iter, err := c.Find(bson.M{}).Iter()
	if err != nil {
		panic(err)
	}
	for {
		err = iter.Next(&result)
		if err != nil {
			fmt.Println("err returned from iter:", err)
			break
		}
		//   fmt.Fprintln(w, result.Id)
		bytes, err2 := json.Marshal(result)
		if err2 != nil {
			panic(err2)
		}
		fmt.Println(string(bytes))

	}
	if err != nil {
		panic(err)
	}

}
Example #8
0
func encodeResult(key string, result *FetchResult) (encoded []byte, err os.Error) {
	// Copy of FetchResult struct with new field Key and base64-encoded Body.
	// This is ugly and violates DRY principle.
	// But also, it allows to extract fetcher as separate package.
	var report struct {
		Key        string            "key"
		Url        string            "url"
		Success    bool              "success"
		Status     string            "status"
		StatusCode int               "status_code"
		Headers    map[string]string "headers"
		Content    string            "content"
		Cached     bool              "cached"
		Visited    string            "visited"
		FetchTime  uint              "fetch_time"
		TotalTime  uint              "total_time"
	}
	report.Key = key
	report.Url = result.Url
	report.Success = result.Success
	report.Status = result.Status
	report.StatusCode = result.StatusCode
	report.Headers = result.Headers
	report.Cached = result.Cached
	report.Visited = time.UTC().Format(HeroshiTimeFormat)
	report.FetchTime = result.FetchTime
	report.TotalTime = result.TotalTime
	content_encoded := make([]byte, base64.StdEncoding.EncodedLen(len(result.Body)))
	base64.StdEncoding.Encode(content_encoded, result.Body)
	report.Content = string(content_encoded)

	encoded, err = json.Marshal(report)
	if err != nil {
		encoded = nil
		fmt.Fprintf(os.Stderr, "Url: %s, error encoding report: %s\n",
			result.Url, err.String())

		// Most encoding errors happen in content. Try to recover.
		report.Content = ""
		report.Status = err.String()
		report.Success = false
		report.StatusCode = 0
		encoded, err = json.Marshal(report)
		if err != nil {
			encoded = nil
			fmt.Fprintf(os.Stderr, "Url: %s, error encoding recovery report: %s\n",
				result.Url, err.String())
		}
	}
	return
}
Example #9
0
func BenchmarkJsonFieldSet(b *testing.B) {
	data, _ := json.Marshal(testObj)
	client.Set("tjs", data)

	for i := 0; i < b.N; i++ {
		var tt testType
		data, _ := client.Get("tjs")
		json.Unmarshal(data, &tt)
		tt.A = "z"
		data, _ = json.Marshal(tt)
		client.Set("tjs", data)
	}
	client.Del("tjs")
}
Example #10
0
func getHash(stubs1 allStubs) string {

	var stubs allStubs = *stubs1.Copy()

	//clear variant data out of stubs before checking hash
	stubs.Hash = ""
	for i, _ := range stubs.Items {
		stubs.Items[i].V = ""
	}

	//clear data out of non-hashed items
	for i, _ := range stubs.Values {
		if stubs.Values[i].T == 1 {
			stubs.Values[i].V = ""
		} else if stubs.Values[i].T == 4 {
			stubs.Values[i].V = 0
		}
	}

	stubs.Hash = "oiheworkvnxcvwetrytknmxznuihkfnknvkcskjnsjdnanjvdskjsvnmzxbc" // this works as a crude salt.
	b, _ := json.Marshal(stubs)

	h := md5.New()
	h.Write([]byte(b))

	return hex.EncodeToString([]byte(h.Sum()))

}
func Call(address string, method string, id interface{}, params []interface{}) (map[string]interface{}, os.Error) {
	data, err := json.Marshal(map[string]interface{}{
		"method": method,
		"id":     id,
		"params": params,
	})
	if err != nil {
		log.Fatalf("Marshal: %v", err)
		return nil, err
	}
	resp, err := http.Post(address,
		"application/json", strings.NewReader(string(data)))
	if err != nil {
		log.Fatalf("Post: %v", err)
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("ReadAll: %v", err)
		return nil, err
	}
	result := make(map[string]interface{})
	err = json.Unmarshal(body, &result)
	if err != nil {
		log.Fatalf("Unmarshal: %v", err)
		return nil, err
	}
	//log.Println(result)
	return result, nil
}
func bumpGeneration(c appengine.Context) {
	if item, err := memcache.Get(c, generationKey); err == memcache.ErrCacheMiss {
		initialValue, _ := json.Marshal(0)
		newItem := &memcache.Item{
			Key:   generationKey,
			Value: []byte(initialValue),
		}
		memcache.Set(c, newItem)
	} else {
		var oldValue int
		json.Unmarshal(item.Value, &oldValue)
		newValue := oldValue + 1
		item.Value, _ = json.Marshal(newValue)
		memcache.CompareAndSwap(c, item)
	}
}
Example #13
0
func (tm *TribMap) AddToList(key string, element []byte) (int, os.Error) {
	tm.lock.Lock()
	defer tm.lock.Unlock()
	_, ok := tm.data[key]
	var set map[string]bool
	var err os.Error
	if !ok {
		set = make(map[string]bool)
	} else {
		set, err = tm.GetSet(key)
		if err != nil {
			return storageproto.EPUTFAILED, err
		}
	}
	s := string(element)
	_, ok = set[s]
	if ok {
		return storageproto.EITEMEXISTS, err
	}
	set[s] = true
	val, err := json.Marshal(set)
	if err != nil {
		return storageproto.EPUTFAILED, err
	}
	tm.data[key] = val
	return storageproto.OK, nil
}
Example #14
0
func (qx *queryCodec) WriteResponse(resp *rpc.Response, ret interface{}) (err os.Error) {

	if resp.Error != "" {
		return qx.Query.Write(http.NewResponse400String(qx.Query.Req, resp.Error))
	}

	if ret == nil {
		return qx.Query.Write(http.NewResponse200(qx.Query.Req))
	}

	r := ret.(*Ret)

	var body []byte
	if r.Value != nil {
		body, err = json.Marshal(r.Value)
		if err != nil {
			qx.Query.Write(http.NewResponse500(qx.Query.Req))
			return err
		}
	}

	httpResp := http.NewResponse200Bytes(qx.Query.Req, body)
	httpResp.Header = make(http.Header)
	for _, setCookie := range r.SetCookies {
		httpResp.Header.Add("Set-Cookie", setCookie.String())
	}

	//dump, _ := http.DumpResponse(httpResp, true)
	//log.Printf("RPC-Resp:\n%s\n", string(dump))

	return qx.Query.Write(httpResp)
}
Example #15
0
func (v *jsonView) handle(w http.ResponseWriter, req *http.Request, s *session) {
	if v.checkUniqueURL != "" && req.URL.Path != v.checkUniqueURL {
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, "<pre>404 page not found</pre>")
		return
	}

	d := func() (ret interface{}) {
		defer func() {
			if e := recover(); e != nil {
				if gde, ok := e.(getDataError); ok {
					ret = gde
				} else if ose, ok := e.(os.Error); ok {
					ret = getDataError{http.StatusInternalServerError, ose}
				} else {
					ret = getDataError{http.StatusInternalServerError, os.NewError(fmt.Sprintf("%v", e))}
				}
			}
		}()
		return v.getData(req, s)
	}()

	dd, e := json.Marshal(d)
	if e != nil {
		fmt.Fprintf(w, "{Code: 500, Error: %#v}", e.String())
	} else {
		w.Write(dd)
	}
}
Example #16
0
func (_entity *Entity) JsonBytes() (json.RawMessage, os.Error) {
	_object, _error := _entity.JsonObject()
	if _error != nil {
		return nil, _error
	}
	return json.Marshal(_object)
}
Example #17
0
func TestUpdateUserAccountInvalidUserId(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1)
    accessKey := accessKeys[0]
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "Tom J")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/sdflsjflsjfslf", bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusNotFound {
        t.Error("Expected ", http.StatusNotFound, " status code but received ", resp.StatusCode)
    }
}
Example #18
0
func handleListInbox(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	userid, _, err := getSession(c, r)
	if err != nil {
		sendError(w, r, "No session cookie")
		return
	}

	s := newStore(c)
	inbox, err := s.ListInbox(userid, false)
	if err != nil {
		sendError(w, r, err.String())
		return
	}

	// Read the updates for all items in the inbox
	for _, entry := range inbox {
		fillInboxItem(s, entry["perma"].(string), entry["seq"].(int64), entry)
	}

	j := map[string]interface{}{"ok": true, "items": inbox}
	msg, err := json.Marshal(j)
	if err != nil {
		panic("Cannot serialize")
	}
	fmt.Fprint(w, string(msg))
}
Example #19
0
func TestUpdateUserAccountMissingSignature(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "GW")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "Pre-White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/" + gw.Id, bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusUnauthorized {
        t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
    }
}
Example #20
0
func BenchmarkJsonSet(b *testing.B) {
	for i := 0; i < b.N; i++ {
		data, _ := json.Marshal(testObj)
		client.Set("tjs", data)
	}
	client.Del("tjs")
}
Example #21
0
func serveObject(val reflect.Value, w http.ResponseWriter, r *http.Request) os.Error {
	switch r.Method {
	case "HEAD", "GET", "PUT":
	default:
		return nil
		return &BadMethod{r.URL.Path, r.Method, val.Interface()}
	}

	ctype := "application/json"
	// TODO(kevlar): Content type negotiation
	w.Header().Set("Content-Type", ctype)

	if r.Method == "HEAD" {
		return nil
	}

	js, err := json.Marshal(val.Interface())
	if err != nil {
		return &FailedEncode{err, ctype, val.Interface()}
	}

	if _, err := w.Write(js); err != nil {
		return err
	}

	return nil
}
Example #22
0
func (s *S) TestObjectIdJSONMarshaling(c *C) {
	id := bson.ObjectIdHex("4d88e15b60f486e428412dc9")
	v := jsonType{Id: &id}
	data, err := json.Marshal(&v)
	c.Assert(err, IsNil)
	c.Assert(string(data), Equals, `{"Id":"4d88e15b60f486e428412dc9"}`)
}
Example #23
0
func toJSON(v interface{}) string {
	b, err := json.Marshal(v)
	if err != nil {
		return fmt.Sprintf("%#v", v)
	}
	return string(b)
}
Example #24
0
func RemoveService(i int) {

	newServices := make([]*RpcService, 0)

	for k, v := range NOS.Services {
		if k != i {
			if v != nil {
				newServices = append(newServices, v)
			}
		}
	}
	NOS.Services = newServices
	b, err := json.Marshal(NOS)
	if err != nil {
		log.Panic(err.String())
	}
	rev, err := DC.Rev()
	if err != nil {
		log.Panic(err.String())
	}
	_, err = DC.Set("/servers/config/networkservers.conf", rev, b)
	if err != nil {
		log.Panic(err.String())
	}

}
Example #25
0
func (p *Parameters) Save(filename string) {
	o, err := json.Marshal(p)
	if err != nil {
		log.Panicf("Parameter Save %s failed error %v", filename, err)
	}
	ioutil.WriteFile(filename, o, 0666)
}
Example #26
0
func RemoveFromRegistry(r *RpcService) {

	newServices := make([]*RpcService, 0)

	for _, v := range NOS.Services {
		if v != nil {
			if !v.Equal(r) {
				newServices = append(newServices, v)
			}

		}
	}
	NOS.Services = newServices
	b, err := json.Marshal(NOS)
	if err != nil {
		log.Panic(err.String())
	}
	rev, err := DC.Rev()
	if err != nil {
		log.Panic(err.String())
	}
	_, err = DC.Set("/servers/config/networkservers.conf", rev, b)
	if err != nil {
		log.Panic(err.String())
	}
}
Example #27
0
func listenForMoveRequests(conn *net.TCPConn) {
	listenServe := make(chan []uint8)
	easynet.TieConnToChannel(conn, listenServe)
	for data := range listenServe {
		r := new(ttypes.BotMoveRequest)
		err := json.Unmarshal(data, r)
		easynet.DieIfError(err, "JSON error")

		if r.Kill {
			fmt.Printf("Bot on %s received kill signal\n", os.Args[0])
			os.Exit(0)
		}

		fmt.Printf("Bot at %d, %d received messages: %v\n", r.YourX, r.YourY, r.Messages)
		fmt.Printf("  Sees other bots: %v\n", r.OtherBots)
		//Do something

		response := new(ttypes.BotMoveResponse)
		if r.YourY < 5 {
			response.MoveDirection = "up"
		} else if r.YourY > 5 {
			response.MoveDirection = "down"
		}
		// response.BroadcastMessage = fmt.Sprintf("'I am %v at %d, %d'", os.Args[0], r.YourX, r.YourY)
		responseString, err := json.Marshal(response)
		easynet.DieIfError(err, "JSON marshal error")
		conn.Write(responseString)
	}
}
Example #28
0
// SendJSON is a helper function that sends a JSON-encoded value
// on the channel associated with clientID.
func SendJSON(c appengine.Context, clientID string, value interface{}) error {
	m, err := json.Marshal(value)
	if err != nil {
		return err
	}
	return Send(c, clientID, string(m))
}
Example #29
0
func handleInboxItem(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	userid, _, err := getSession(c, r)
	if err != nil {
		sendError(w, r, "No session cookie")
		return
	}

	s := newStore(c)
	perma_blobref := r.FormValue("perma")
	item, err := s.GetInboxItem(userid, perma_blobref)
	if err != nil {
		http.Error(w, "Could not get inbox item", http.StatusInternalServerError)
		c.Errorf("handleInboxItem: %v", err)
		return
	}
	entry := make(map[string]interface{})
	entry["perma"] = perma_blobref
	entry["seq"] = item.LastSeq
	err = fillInboxItem(s, perma_blobref, item.LastSeq, entry)
	if err != nil {
		fmt.Fprintf(w, `{"ok":false, "error":%v}`, err.String())
		return
	}

	info, _ := json.Marshal(entry)
	fmt.Fprintf(w, `{"ok":true, "item":%v}`, string(info))
}
Example #30
0
func join(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	u := user.Current(c)
	if u == nil {
		url, err := user.LoginURL(c, r.URL.String())
		if err != nil {
			http.Error(w, err.String(), http.StatusInternalServerError)
			return
		}
		w.Header().Set("Location", url)
		w.WriteHeader(http.StatusFound)
		return
	}
	r.ParseForm()
	// TODO check table arg
	state, err := joinTable(c, r.Form["table"][0], u.String())
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}
	var b []byte
	b, err = json.Marshal(state)
	if err != nil {
		http.Error(w, err.String(), http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, "%s", b)
}