Esempio n. 1
0
func (ts *Tribserver) PostTribble(args *tribproto.PostTribbleArgs, reply *tribproto.PostTribbleReply) error {
	// check that user exists
	_, err := ts.ls.Get(args.Userid)
	if err != nil {
		reply.Status = tribproto.ENOSUCHUSER
		return nil
	}
	// Create the tribble structure
	timestamp := time.Now()
	tribble := tribproto.Tribble{Userid: args.Userid, Posted: timestamp, Contents: args.Contents}
	tribid := fmt.Sprintf("%s:%x", args.Userid, timestamp.UnixNano())
	// marshal the tribble structure
	mtrib, e := json.Marshal(tribble)
	if e != nil {
		return e
	}
	// post the tribble
	err = ts.ls.Put(tribid, string(mtrib))
	if err != nil {
		return err
	}
	postsid := fmt.Sprintf("%s:posts", args.Userid)
	err = ts.ls.AppendToList(postsid, tribid)
	if err != nil {
		return err
	}
	reply.Status = tribproto.OK
	return nil
}
Esempio n. 2
0
func (ts *Tribserver) PostTribble(args *tribproto.PostTribbleArgs, reply *tribproto.PostTribbleReply) error {
	now := time.Now().UnixNano()
	//Make into Tribble struct.
	tribble := tribproto.Tribble{Userid: args.Userid, Posted: time.Unix(0, now), Contents: args.Contents}

	//check errors for any calls to libstore

	//check if user exists
	_, userErr := ts.lstore.Get(args.Userid)
	if userErr != nil {
		reply.Status = tribproto.ENOSUCHUSER
		return nil
	}

	//else
	//append to list([user:tribbles], tribbleID)
	//put tribble [user:tribbleID] = tribble
	//return nil
	tribbleJSON, marshalErr := json.Marshal(tribble)
	if marshalErr != nil {
		return marshalErr
	}

	//format timestamps in hex
	tribbleId := strconv.FormatInt(now, 16)
	ts.lstore.Put(args.Userid, tribbleId) //mostly for checking for existance for other functions
	ts.lstore.Put(args.Userid+":"+tribbleId, string(tribbleJSON))
	ts.lstore.AppendToList(args.Userid+":timestamps", tribbleId)
	reply.Status = tribproto.OK
	return nil
}
Esempio n. 3
0
/**@brief Post Tribbles
 * @param PostTribbleArgs
 * @param PostTribbleReply
 * @return error
 */
func (ts *Tribserver) PostTribble(
	args *tribproto.PostTribbleArgs, reply *tribproto.PostTribbleReply) error {
	var trib_key string
	var trib tribproto.Tribble
	var enc []byte
	var err error

	//do not allow empty post
	if args.Contents == "" {
		return nil
	}

	trib_key = fmt.Sprintf("%s:T", args.Userid)
	_, err = ts.Store.GetList(trib_key)
	if lsplog.CheckReport(1, err) {
		reply.Status = tribproto.ENOSUCHUSER
		return nil
	}

	err = ts.Store.AppendToList(trib_key, strconv.Itoa(ts.Id))
	if lsplog.CheckReport(1, err) {
		reply.Status = tribproto.EEXISTS
		return nil
	}

	trib.Userid = args.Userid
	trib.Posted = time.Now()
	trib.Contents = args.Contents

	enc, err = json.Marshal(trib)
	if lsplog.CheckReport(1, err) {
		reply.Status = tribproto.OK
		return err
	}

	err = ts.Store.Put(strconv.Itoa(ts.Id), string(enc))
	if lsplog.CheckReport(1, err) {
		reply.Status = tribproto.OK
		return err
	}

	reply.Status = tribproto.OK
	ts.Id++

	return nil
}