Simon 2 роки тому
батько
коміт
1a1627a7e8
4 змінених файлів з 111 додано та 39 видалено
  1. 10 10
      client/client.go
  2. 5 2
      follower/follower.go
  3. 6 4
      leader/leader.go
  4. 90 23
      lib/databaseRead.go

+ 10 - 10
client/client.go

@@ -42,7 +42,7 @@ type tweet struct {
 const leader string = "127.0.0.1:4441"
 
 //needs to be changed at leader/follower/client at the same time
-const numClients = 500
+const numClients = 1
 
 //mylimit=8000
 //sudo prlimit --nofile=$mylimit --pid $$; ulimit -n $mylimit
@@ -61,7 +61,7 @@ var publisherAmount int
 var timeBounds []int
 var speedUp int = 50
 
-var maxTimePerRound time.Duration = 25 * time.Second
+var maxTimePerRound time.Duration = 5 * time.Second
 var startTime int
 
 //todo! expand this for multiple clients
@@ -170,7 +170,7 @@ func client(clientNumber int) {
 			sendQuerys(encryptedQueryLeader, encryptedQueryFollower, leaderConn, false)
 			pos := receiveVirtualAddress(sharedSecret[clientNumber], leaderConn)
 
-			tweet := getRealTweet(clientNumber)
+			tweet := getRandomTweet(clientNumber)
 			if clientNumber == numClients-1 {
 				fmt.Println("publisherAmount", publisherAmount)
 			}
@@ -458,12 +458,12 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 
 		index := strings.Index(string(tweets), ";;")
 		if index != -1 && clientNumber == 0 {
-			/*
-				fmt.Println("Correct")
-				textArr := strings.Split(string(tweets), ";;;;;;;;")
-				text := string(tweets)[:len(textArr)-1]
-				fmt.Println("Round", round, "Text", text[:5], "Length", len(tweets))
-			*/
+
+			fmt.Println("Correct")
+			//textArr := strings.Split(string(tweets), ";;;;;;;;")
+			//text := string(tweets)[:len(textArr)-1]
+			fmt.Println("Round", round, "Text", string(tweets), "Length", len(tweets))
+
 		} else if index == -1 {
 			fmt.Println("error")
 			fmt.Println("round", round, "text:", string(tweets), "length", len(tweets))
@@ -728,7 +728,7 @@ func getRandomTweet(clientNumber int) []byte {
 	if maxTopics == 0 {
 		maxTopics = 1
 	}
-	maxInt := 10000
+	maxInt := 3
 	topicNumbers := make([]int, maxTopics)
 	//fills the array with unique random ascending values ranging from 0 to maxInt
 	for i := 0; i < maxTopics; i++ {

+ 5 - 2
follower/follower.go

@@ -422,8 +422,11 @@ func phase2(leaderWorkerConnection net.Conn) {
 							topics = append(topics, topic)
 							topic = ""
 						} else {
-							//change this works for ints, need to be changed for strings
-							topic = topic + string(letter)
+							//if topics are
+							//int
+							topic = topic + fmt.Sprint(int(letter))
+							//string
+							//topic = topic + string(letter)
 						}
 					} else if position == 1 {
 						text = text + string(letter)

+ 6 - 4
leader/leader.go

@@ -72,7 +72,7 @@ var collisionCounter []float64
 
 var clientsConnected int
 
-var maxTimePerRound time.Duration = 25 * time.Second
+var maxTimePerRound time.Duration = 5 * time.Second
 
 var clientsServedPhase1 []int
 var clientsServedPhase3 []int
@@ -706,9 +706,11 @@ func phase2(followerConnection net.Conn) {
 							topics = append(topics, topic)
 							topic = ""
 						} else {
-							//change this works for ints, need to be changed for strings
-							//have
-							topic = topic + string(letter)
+							//if topics are
+							//int
+							topic = topic + fmt.Sprint(int(letter))
+							//string
+							//topic = topic + string(letter)
 						}
 					} else if position == 1 {
 						text = text + string(letter)

+ 90 - 23
lib/databaseRead.go

@@ -73,7 +73,7 @@ func NewEntries(inputTweets []Tweet, whereTo int) {
 //todo! add round to pirquery only get tweets that have been posted from that round onward
 func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte) []byte {
 	//fmt.Println("query", pirQuery)
-	//fmt.Println("dbR", dbR)
+	fmt.Println("dbR", dbR)
 	tmpdb := dbR
 	if whereFrom == 1 {
 		tmpdb = archive
@@ -98,7 +98,7 @@ func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte)
 		}
 	}
 
-	return tweetsToByteArray(tweetsToReturn)
+	return tweetsToByteArray(tweetsToReturn, whereFrom)
 }
 
 func maxTweetAmount(whereFrom int) int {
@@ -131,42 +131,109 @@ func getNamesForTopics(wantedIndices []byte, whereFrom int) []string {
 }
 
 //transform struct to byte array for sending
-func tweetsToByteArray(tweetsToReturn [][]Tweet) []byte {
+func tweetsToByteArray(tweetsToReturn [][]Tweet, whereFrom int) []byte {
 	tweetsAsBytes := make([]byte, minimumBlockSize)
-	for _, block := range tweetsToReturn {
-		var tweetToAppend []byte
+	for blockIndex, block := range tweetsToReturn {
+		var topicPadding []string
+		var blockToAppend []byte
 		for _, tweet := range block {
-			for _, topic := range tweet.Topics {
-				tweetToAppend = append(tweetToAppend, []byte(topic)...)
-				tweetToAppend = append(tweetToAppend, ","...)
+			for topicIndex, topic := range tweet.Topics {
+				blockToAppend = append(blockToAppend, []byte(topic)...)
+				blockToAppend = append(blockToAppend, ","...)
+
+				//gets the topic used for padding
+				if topicIndex > 0 {
+					topicPadding = append(topicPadding, topic)
+				}
 			}
 			//replaces last "," with ";;" bc there is text following and not another topic
-			tweetToAppend = tweetToAppend[:len(tweetToAppend)-1]
-			tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
-			tweetToAppend = append(tweetToAppend, []byte(tweet.Text)...)
-			tweetToAppend = append(tweetToAppend, []byte(";")[0])
+			blockToAppend = blockToAppend[:len(blockToAppend)-1]
+			blockToAppend = append(blockToAppend, []byte(";;")[:]...)
+			blockToAppend = append(blockToAppend, []byte(tweet.Text)...)
+			blockToAppend = append(blockToAppend, []byte(";")[0])
 		}
+
 		//adds padding
-		tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
-		length := minimumBlockSize - len(tweetToAppend)
-		//fmt.Println("len", len(tweetToAppend))
-		//todo! replace with grouping
-		//grouping using topics from recovered tweets
-		if length < 0 {
-			fmt.Println("<0", string(tweetToAppend))
+		blockToAppend = append(blockToAppend, []byte(";;")[:]...)
+		remainingLength := minimumBlockSize - len(blockToAppend)
+		//fmt.Println("block", string(blockToAppend))
+
+		//grouping using topic from recovered tweets
+		index := 0
+		for len(topicPadding) > index && remainingLength > 0 && topicPadding[index] != "" {
+			paddingTweet, err := getNextTweet(topicPadding[index], index, whereFrom)
+			if err {
+				break
+			}
+			if remainingLength < len(paddingTweet) {
+				break
+			}
+			blockToAppend = append(blockToAppend, paddingTweet...)
+
+			remainingLength -= len(paddingTweet)
+			index++
 		}
-		padding := bytes.Repeat([]byte(";"), length)
-		tweetToAppend = append(tweetToAppend, padding...)
-		Xor(tweetToAppend, tweetsAsBytes)
 
-		//fmt.Println(tweetsAsBytes)
+		if blockIndex == 0 {
+			fmt.Println("0000", string(blockToAppend))
+		}
+
+		padding := bytes.Repeat([]byte(";"), remainingLength)
+		blockToAppend = append(blockToAppend, padding...)
+		Xor(blockToAppend, tweetsAsBytes)
 
+		//fmt.Println(tweetsAsBytes)
 	}
 
 	//fmt.Println("length Returned", len(tweetsAsBytes))
 	return tweetsAsBytes
 }
 
+//gets the Tweet at index from wantedTopic for padding
+func getNextTweet(wantedTopic string, index, whereFrom int) ([]byte, bool) {
+	tmpdb := dbR
+	if whereFrom == 1 {
+		tmpdb = archive
+	}
+
+	var tweetToReturn Tweet
+	for tweetIndex, tweet := range tmpdb[wantedTopic] {
+		if len(tmpdb[wantedTopic]) <= index {
+			return nil, true
+		} else if tweetIndex < index {
+			continue
+		} else if tweetIndex > index {
+			break
+		}
+
+		//new Tweet
+		if tweet.Text != "" {
+			tweet.RoundPosted = 0
+			tweetToReturn = tweet
+		} else {
+			//"copied" tweet
+			//find tweet with pointers
+			tweet = tmpdb[tweet.TopicPointer][tweet.TextPointer]
+			tweet.RoundPosted = 0
+			tweetToReturn = tweet
+		}
+	}
+
+	fmt.Println("nextTweet", tweetToReturn)
+	var tweetToReturnBytes []byte
+	for _, topic := range tweetToReturn.Topics {
+		tweetToReturnBytes = append(tweetToReturnBytes, []byte(topic)...)
+		tweetToReturnBytes = append(tweetToReturnBytes, ","...)
+	}
+	//replaces last "," with ";;" bc there is text following and not another topic
+	tweetToReturnBytes = tweetToReturnBytes[:len(tweetToReturnBytes)-1]
+	tweetToReturnBytes = append(tweetToReturnBytes, []byte(";;")[:]...)
+	tweetToReturnBytes = append(tweetToReturnBytes, []byte(tweetToReturn.Text)...)
+	tweetToReturnBytes = append(tweetToReturnBytes, []byte(";")[0])
+
+	return tweetToReturnBytes, false
+}
+
 //see func name
 func GetTopicList(whereFrom int) ([]byte, int) {
 	tmpTopicList := topicList