Browse Source

various bugfixes

Simon 2 years ago
parent
commit
8780e4bea6
5 changed files with 89 additions and 52 deletions
  1. 27 10
      client/client.go
  2. 1 1
      comm.go
  3. 7 5
      follower/follower.go
  4. 9 5
      leader/leader.go
  5. 45 31
      lib/databaseRead.go

+ 27 - 10
client/client.go

@@ -285,7 +285,9 @@ func createPIRQuery(subPhase int, clientNumber int) ([]byte, []byte) {
 	}
 
 	for topic, position := range tmptopicsOfInterest {
-		topicsOfInterestAsBytes[topic][position] = 1
+		if position > 0 {
+			topicsOfInterestAsBytes[topic][position-1] = 1
+		}
 	}
 
 	//pirQuery [serverAmount][topicsofinterest][topicAmount]byte
@@ -417,10 +419,10 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 
 		index := strings.Index(string(tweets), ";;;")
 		if index != -1 {
-			//text := string(tweets)[:index]
-			//fmt.Println(text)
+			text := string(tweets)[:index]
+			fmt.Println("received in round", round, "Text", text)
 		} else {
-			fmt.Println("received text not of correct format", round, string(tweets))
+			fmt.Println("received text not of correct format", round, "Text:", string(tweets))
 		}
 	}
 }
@@ -565,22 +567,37 @@ func getTweet(clientNumber int) []byte {
 		maxTopics = 1
 	}
 	maxInt := 10
-	topics := make([]byte, maxTopics)
-	//fills the array with unique random ascending values ranging from 0 to max
+	topicNumbers := make([]int, maxTopics)
+	//fills the array with unique random ascending values ranging from 0 to maxInt
 	for i := 0; i < maxTopics; i++ {
-		topics[i] = byte(mr.Intn(maxInt))
+		topicNumbers[i] = mr.Intn(maxInt)
 
 		for j := 0; j < i; j++ {
-			if topics[i] == topics[j] {
+			if topicNumbers[i] == topicNumbers[j] {
 				i--
 				break
 			}
 		}
 	}
 
-	fmt.Println("topics", topics)
+	sort.Ints(topicNumbers)
+	fmt.Println("Writing to", topicNumbers)
+
+	var topics []byte
+
+	topicIndex := 0
+	for i := 0; i < len(topicNumbers)*2; i++ {
+		if i%2 == 0 {
+			topics = append(topics, byte(topicNumbers[topicIndex]))
+			topicIndex++
+		} else if i != (len(topicNumbers)*2)-1 {
+			topics = append(topics, []byte(",")[0])
+		}
+	}
+
+	topics = append(topics, []byte(";")[0])
 
-	text := []byte("I am a house in a mouse " + strconv.Itoa(r.Intn(100)) + ";")
+	text := []byte("I am a house in a mouse " + strconv.Itoa(r.Intn(10000)) + ";")
 	tweet = append(tweet, topics...)
 	tweet = append(tweet, text...)
 	tweet = append(tweet, []byte(";")[0])

+ 1 - 1
comm.go

@@ -7,7 +7,7 @@ import (
 	"time"
 )
 
-var Port = ":3001"
+var Port = ":3000"
 
 func main() {
 

+ 7 - 5
follower/follower.go

@@ -23,6 +23,7 @@ import (
 	"math/big"
 	"net"
 	"strconv"
+	"strings"
 	"sync"
 	"time"
 	"unsafe"
@@ -53,7 +54,7 @@ const numThreads = 12
 var dbWriteSize int = 100
 var maxTimePerRound time.Duration = 5 * time.Second
 
-var round int = 1
+var round int = 0
 var startTime time.Time
 
 var ignoreMe []byte
@@ -145,6 +146,7 @@ func main() {
 
 	for {
 
+		round++
 		fmt.Println("Phase 1 Round", round)
 
 		//create write db for this round
@@ -206,9 +208,6 @@ func main() {
 		wg.Wait()
 
 		lib.CleanUpdbR(round)
-
-		round++
-
 	}
 }
 
@@ -397,6 +396,8 @@ func phase2(leaderWorkerConnection net.Conn) {
 		//discard cover message
 		if tmpdb[i][0] == 0 {
 			continue
+		} else if -1 == strings.Index(string(tmpdb[i]), ";;") {
+			continue
 		} else {
 			//reconstruct tweet
 			var position int = 0
@@ -416,7 +417,8 @@ func phase2(leaderWorkerConnection net.Conn) {
 							topics = append(topics, topic)
 							topic = ""
 						} else {
-							topic = topic + string(letter)
+							//change this works for ints, need to be changed for strings
+							topic = topic + fmt.Sprint((int(letter)))
 						}
 					} else if position == 1 {
 						text = text + string(letter)

+ 9 - 5
leader/leader.go

@@ -25,6 +25,7 @@ import (
 	"net"
 	"sort"
 	"strconv"
+	"strings"
 	"sync"
 	"time"
 
@@ -57,7 +58,7 @@ var topicAmount int
 var archiveTopicAmount int
 
 // every roundsBeforeUpdate the client updates his pirQuery
-const roundsBeforeUpdate = 5
+const roundsBeforeUpdate = 1
 const neededSubscriptions = 1
 const numThreads = 12
 const dataLength = 128
@@ -66,7 +67,7 @@ var dbWriteSize int = 100
 var maxTimePerRound time.Duration = 5 * time.Second
 
 //counts the number of rounds
-var round int = 1
+var round int = 0
 
 var startTime time.Time
 
@@ -233,6 +234,8 @@ func main() {
 		startTime = time.Now()
 		phase[0] = 1
 
+		round++
+
 		fmt.Println("Phase 1 Round", round)
 
 		//creates a new write Db for this round
@@ -296,8 +299,6 @@ func main() {
 		wg.Wait()
 
 		lib.CleanUpdbR(round)
-
-		round++
 	}
 }
 
@@ -589,6 +590,8 @@ func phase2(followerConnection net.Conn) {
 		//discard cover message
 		if tmpdb[i][0] == 0 {
 			continue
+		} else if -1 == strings.Index(string(tmpdb[i]), ";;") {
+			continue
 		} else {
 			currentPublisherAmount++
 			//reconstruct tweet
@@ -609,7 +612,8 @@ func phase2(followerConnection net.Conn) {
 							topics = append(topics, topic)
 							topic = ""
 						} else {
-							topic = topic + string(letter)
+							//change this works for ints, need to be changed for strings
+							topic = topic + fmt.Sprint((int(letter)))
 						}
 					} else if position == 1 {
 						text = text + string(letter)

+ 45 - 31
lib/databaseRead.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"crypto/rand"
 	"encoding/json"
+	"fmt"
 )
 
 //topicPointer and textPointer should not be exported
@@ -53,11 +54,12 @@ func NewEntries(inputTweets []Tweet, whereTo int) {
 				//known tweet
 				//setting pointer for all other Topics
 				topic := tweet.Topics[index]
-				tweet.TopicPointer = tweet.Topics[0]
-				tweet.TextPointer = position
-				tweet.Topics = nil
-				tweet.Text = ""
-				tmpdb[topic] = append(tmpdb[topic], tweet)
+				var pointerTweet Tweet
+				pointerTweet.TopicPointer = tweet.Topics[0]
+				pointerTweet.TextPointer = position
+				pointerTweet.Topics = nil
+				pointerTweet.Text = ""
+				tmpdb[topic] = append(tmpdb[topic], pointerTweet)
 			}
 		}
 	}
@@ -70,12 +72,15 @@ 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) []byte {
+	fmt.Println("pirQuery", pirQuery)
+	fmt.Println(dbR)
 	tmpdb := dbR
 	if whereFrom == 1 {
 		tmpdb = archive
 	}
 	minimumBlockSize = dataLength * maxTweetAmount(whereFrom)
 	var wantedTopics = getNamesForTopics(pirQuery, whereFrom)
+	fmt.Println("topicNames", wantedTopics)
 	tweetsToReturn := make([][]Tweet, len(wantedTopics))
 	for index, wantedTopic := range wantedTopics {
 		for _, tweet := range tmpdb[wantedTopic] {
@@ -92,6 +97,13 @@ func GetTweets(pirQuery []byte, dataLength int, whereFrom int) []byte {
 			}
 		}
 	}
+
+	for _, row := range tweetsToReturn {
+		for _, tweet := range row {
+			fmt.Print(tweet.Text)
+		}
+		fmt.Println("")
+	}
 	return tweetsToByteArray(tweetsToReturn)
 }
 
@@ -168,39 +180,41 @@ func GetTopicList(whereFrom int) ([]byte, int) {
 
 //iterates through full dbR and moves old tweets to archive
 func CleanUpdbR(round int) {
-	var tweetsToArchive []Tweet
-	for j := range dbR {
-		tweets := dbR[j]
-		for i := len(tweets) - 1; i >= 0; i-- {
-			if round-roundsBeforeArchiving == tweets[i].RoundPosted {
-				//only adds the tweet to the archive when there is text
-				if tweets[i].Text != "" {
-					tweetsToArchive = append(tweetsToArchive, tweets[i])
+
+	if roundsBeforeArchiving%round == 0 {
+		var tweetsToArchive []Tweet
+		for j := range dbR {
+			tweets := dbR[j]
+			for i := len(tweets) - 1; i >= 0; i-- {
+				if round-roundsBeforeArchiving == tweets[i].RoundPosted {
+					//only adds the tweet to the archive when there is text
+					if tweets[i].Text != "" {
+						tweetsToArchive = append(tweetsToArchive, tweets[i])
+					}
+					//delets the tweet from the array
+					tweets = append(tweets[:i], tweets[i+1:]...)
 				}
-				//delets the tweet from the array
-				tweets = append(tweets[:i], tweets[i+1:]...)
 			}
+			dbR[j] = tweets
 		}
-		dbR[j] = tweets
-	}
-	NewEntries(tweetsToArchive, 1)
+		NewEntries(tweetsToArchive, 1)
 
-	//redoes the whole dbR to correct pointers
-	var tweetsToMain []Tweet
+		//redoes the whole dbR to correct pointers
+		var tweetsToMain []Tweet
 
-	for i := range dbR {
-		tweets := dbR[i]
-		for _, tweet := range tweets {
-			if tweet.Text != "" {
-				tweetsToMain = append(tweetsToMain, tweet)
+		for i := range dbR {
+			tweets := dbR[i]
+			for _, tweet := range tweets {
+				if tweet.Text != "" {
+					tweetsToMain = append(tweetsToMain, tweet)
+				}
 			}
 		}
-	}
 
-	dbR = nil
-	dbR = make(map[string][]Tweet)
-	topicList = nil
-
-	NewEntries(tweetsToMain, 0)
+		dbR = nil
+		dbR = make(map[string][]Tweet)
+		topicList = nil
 
+		NewEntries(tweetsToMain, 0)
+	}
 }