예제 #1
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	key := util.FormatUserKey(args.UserID)
	if ts.checkIfKnown(key) {
		newTrib := tribrpc.Tribble{args.UserID, time.Now(), args.Contents}
		tribStr, err := json.Marshal(newTrib)
		if err != nil {
			return err
		}

		id := newTrib.UserID
		time := newTrib.Posted.UnixNano()
		post_key := util.FormatPostKey(id, time)
		for ts.checkIfKnown(post_key) {
			post_key = util.FormatPostKey(id, time)
		}
		err = ts.storage.Put(post_key, string(tribStr))
		if err != nil {
			return err
		}

		triblist_key := util.FormatTribListKey(args.UserID)
		err = ts.storage.AppendToList(triblist_key, post_key)
		if err != nil {
			return err
		}

		reply.PostKey = post_key
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.NoSuchUser
	}
	return nil
}
예제 #2
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	reply.Status = tribrpc.OK
	t := time.Now()
	tribble := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   t,
		Contents: args.Contents,
	}
	tribbleByte, err := json.Marshal(tribble)
	tribbleString := string(tribbleByte)
	tribbleID := t.UTC().UnixNano()
	reply.PostKey = util.FormatPostKey(args.UserID, tribbleID)
	if err != nil {
		return errors.New("Error Marshal")
	}
	err = ts.ls.Put(reply.PostKey, tribbleString)
	if err != nil {
		return errors.New("Error Put")
	}
	err = ts.ls.AppendToList(util.FormatTribListKey(args.UserID), reply.PostKey)
	if err != nil {
		return errors.New("Error AppendToList: " + err.Error())
	}
	return nil
}
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {

	userkey := util.FormatUserKey(args.UserID)

	_, userErr := ts.libstore.Get(userkey)
	if userErr != nil {
		reply.Status = tribrpc.NoSuchUser
	} else {
		postTime := time.Now()
		tribble := &tribrpc.Tribble{
			UserID:   args.UserID,
			Posted:   postTime,
			Contents: args.Contents,
		}
		marshalTribble, _ := json.Marshal(tribble)

		unixTime := int64(postTime.UnixNano())
		postKey := util.FormatPostKey(args.UserID, unixTime)

		putErr := ts.libstore.Put(postKey, string(marshalTribble))
		if putErr != nil {
			return errors.New("Fail to put postkey and tribble")
		}

		tribListKey := util.FormatTribListKey(args.UserID)
		appendErr := ts.libstore.AppendToList(tribListKey, postKey)
		if appendErr != nil {
			return errors.New("Fail to append tribble to user")
		}
		reply.Status = tribrpc.OK
		reply.PostKey = postKey
	}

	return nil
}
예제 #4
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	if args == nil {
		return errors.New("ts: Can't post nil")
	}
	if reply == nil {
		return errors.New("ts: Can't reply with nil in Post")
	}

	curTime := time.Now()
	_, err := ts.ls.Get(util.FormatUserKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.PostKey = ""
		return nil
	}

	newTribID := util.FormatPostKey(args.UserID, curTime.UnixNano())

	for ts.ls.AppendToList(util.FormatTribListKey(args.UserID), newTribID) != nil {
		newTribID = util.FormatPostKey(args.UserID, curTime.UnixNano())
	}

	trb := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   curTime,
		Contents: args.Contents,
	}

	marshaled, _ := json.Marshal(trb)
	if ts.ls.Put(newTribID, string(marshaled[:])) != nil {
		ts.ls.RemoveFromList(util.FormatTribListKey(args.UserID), newTribID)
		reply.Status = tribrpc.NoSuchUser
		reply.PostKey = ""
		return errors.New("Fatal, failed to add new post")
	}

	reply.Status = tribrpc.OK
	reply.PostKey = newTribID
	return nil
}
예제 #5
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	userkey := util.FormatUserKey(args.UserID)

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	postkey := util.FormatPostKey(args.UserID, time.Now().UnixNano())
	// make sure postkey is unique
	for {
		if _, err := ts.ls.Get(postkey); err == nil {
			postkey = util.FormatPostKey(args.UserID, time.Now().UnixNano())
			continue
		}
		break
	}

	// Create the Tribble
	tribble := tribrpc.Tribble{UserID: args.UserID, Posted: time.Now(),
		Contents: args.Contents}
	value, err := json.Marshal(tribble)
	if err != nil {
		return errors.New("Couldn't create Tribble")
	}

	// Hash the Tribble and add the postkey to the user's TribList
	if ep := ts.ls.Put(postkey, string(value)); ep != nil {
		return errors.New("Couldn't post Tribble")
	}
	if ea := ts.ls.AppendToList(util.FormatTribListKey(args.UserID), postkey); ea != nil {
		return errors.New("Couldn't add TribbleID to user list")
	}
	reply.PostKey = postkey
	reply.Status = tribrpc.OK
	return nil
}
예제 #6
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	user_id := util.FormatUserKey(args.UserID)

	// If the user_id doesn't exist
	// Reture NoSuchUser
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	now := time.Now()
	tribble := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   now,
		Contents: args.Contents,
	}

	tribble_str, _ := json.Marshal(tribble)

	post_key := util.FormatPostKey(args.UserID, time.Now().UnixNano())
	err = ts.lib_store.Put(post_key, string(tribble_str))
	//if err != nil {
	//	reply.Status = tribrpc.NoSuchUser
	//	fmt.Println(1)
	//	return nil
	//}

	user_trib_list_key := util.FormatTribListKey(args.UserID)
	err = ts.lib_store.AppendToList(user_trib_list_key, post_key)
	//if err != nil {
	//	reply.Status = tribrpc.NoSuchUser
	//	fmt.Println(2)
	//	return nil
	//}

	reply.Status = tribrpc.OK
	reply.PostKey = post_key
	return nil
}
예제 #7
0
/*
	-Check if user is present
	-Create the tribble
	-timestamp: go time
	-create a tribble
	-marshal it

	-Appendto list that post key to the usrID
	-Add to the map from post key -> marshalled tribble
*/
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {

	if DBG {
		fmt.Println("-----> PostTribble")
	}
	var thisUsrId string = args.UserID
	var content string = args.Contents

	//check if user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(thisUsrId))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
		//user not found
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	var timeNow time.Time = time.Now()

	newTribble := tribrpc.Tribble{
		UserID:   thisUsrId,
		Posted:   timeNow,
		Contents: content,
	}

	marshalTrib, err := json.Marshal(&newTribble)

	if err != nil {
		fmt.Println("Error marshalling")
		//return err
	}

	var postKey string = util.FormatPostKey(thisUsrId, timeNow.UnixNano())

	//store the tribble itself
	err = ts.libStore.Put(postKey, string(marshalTrib))

	if err != nil {
		fmt.Println("Error putting tribble contents")
		//return err
	}

	//store the postkey
	err = ts.libStore.AppendToList(util.FormatTribListKey(thisUsrId), postKey)

	if err != nil {
		fmt.Println("Error putting postKey")
		//return err
	}

	reply.PostKey = postKey
	reply.Status = tribrpc.OK
	return nil
}