Browse Source

posting and recovering of tweets work for gui

Simon 1 year ago
parent
commit
0d25900ad9
7 changed files with 243 additions and 52 deletions
  1. 7 0
      .vscode/launch.json
  2. 47 4
      client/client.go
  3. 44 7
      comm.go
  4. 27 30
      index.html
  5. 1 1
      leader/leader.go
  6. 4 4
      lib/databaseRead.go
  7. 113 6
      main.js

+ 7 - 0
.vscode/launch.json

@@ -0,0 +1,7 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": []
+}

+ 47 - 4
client/client.go

@@ -61,12 +61,16 @@ var publisherAmount int
 var goodPadding int
 var blocksReceived int
 var timeBounds []float64
+
+//gui variables
 var guiTweet string
+var mainTweets string
+var archiveTweets string
 
 //this translates to a simulated round length of ~2h
 var speedUp float64 = 7200 / ((maxTimePerRound.Seconds()) * 3)
 
-var maxTimePerRound time.Duration = 5 * time.Second
+var maxTimePerRound time.Duration = 2 * time.Second
 var startTime int
 
 var archiveInterests = make([]int, 1)
@@ -274,7 +278,7 @@ func Client(clientNumber int, f *os.File) {
 			receiveTweets(sharedSecret[clientNumber], leaderConn, false, clientNumber)
 
 			if len(archiveTopicList) > 0 {
-				wantsArchive[0] = 0
+				wantsArchive[0] = 1
 			} else {
 				wantsArchive[0] = 0
 			}
@@ -298,8 +302,8 @@ func Client(clientNumber int, f *os.File) {
 func createPIRQuery(subPhase int, clientNumber int) ([]byte, []byte) {
 	//later this will be taken from gui, this is only for testing
 	topicsOfInterest := make([]int, 1)
-	topicsOfInterest[0] = mr.Intn(10)
-	archiveInterests[0] = mr.Intn(10)
+	topicsOfInterest[0] = 0 //mr.Intn(10)
+	archiveInterests[0] = 0 //mr.Intn(10)
 
 	tmpNeededSubscriptions := neededSubscriptions
 	if tmpNeededSubscriptions > len(topicList) {
@@ -471,6 +475,8 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 
 		index := strings.Index(string(tweets), ";;")
 
+		fmt.Println("received", string(tweets))
+
 		if index != -1 {
 
 			textArr := strings.Split(string(tweets), ";;;")
@@ -485,6 +491,16 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 				goodPadding++
 			}
 
+			if getArchive {
+				if !strings.Contains(archiveTweets, text[0]) {
+					archiveTweets += text[0] + ";"
+				}
+			} else {
+				if !strings.Contains(mainTweets, text[0]) {
+					mainTweets += text[0] + ";"
+				}
+			}
+
 		} else if index == -1 && tweets[0] != 0 {
 			fmt.Println("error")
 			fmt.Println("round", round, string(tweets), "length", len(tweets))
@@ -493,6 +509,33 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 	}
 }
 
+func GetTweets(whereFrom int) string {
+
+	var tweets string
+
+	var recTweets string
+
+	if whereFrom == 0 {
+		recTweets = mainTweets
+		mainTweets = ""
+	} else {
+		recTweets = archiveTweets
+		archiveTweets = ""
+		fmt.Println("archive", recTweets)
+	}
+
+	tweetArr := strings.Split(recTweets, ";;")
+
+	for _, str := range tweetArr {
+		strArr := strings.Split(str, ";")
+		tweets += "<li>" + strArr[0] + "<br>" + strArr[1] + "</li>"
+	}
+
+	fmt.Println("tweetsToPost", tweets)
+
+	return tweets
+}
+
 //creates a shared secret for each server
 func createSharedSecret() [numClients][2][32]byte {
 	var tmpSharedSecret [numClients][2][32]byte

+ 44 - 7
comm.go

@@ -19,7 +19,10 @@ func main() {
 	defer f.Close()
 
 	log.SetOutput(f)
-	go client.Client(0, f)
+	if os.Args[len(os.Args)-1] == "1" {
+		go client.Client(0, f)
+
+	}
 
 	http.HandleFunc("/", ServeFiles)
 	fmt.Println("Serving @ : ", "http://127.0.0.1"+Port)
@@ -35,7 +38,7 @@ func ServeFiles(w http.ResponseWriter, r *http.Request) {
 
 		path := r.URL.Path
 
-		fmt.Println("path before: ", path)
+		//fmt.Println("path before: ", path)
 
 		if path == "/" {
 
@@ -44,7 +47,7 @@ func ServeFiles(w http.ResponseWriter, r *http.Request) {
 			path = "/home/simon/goCode/AnonymousTopicBasedPubSubCommunicationforMicroblogging" + path
 		}
 
-		fmt.Println("path after: ", path)
+		//fmt.Println("path after: ", path)
 
 		http.ServeFile(w, r, path)
 
@@ -55,13 +58,47 @@ func ServeFiles(w http.ResponseWriter, r *http.Request) {
 		message := r.FormValue("message")
 
 		fmt.Println("----------------------------------")
-		fmt.Println("Message from Client: ", message)
+		fmt.Println("Tweet to post: ", message)
 		client.SetGuiTweet(message)
-		// respond to client's request, dont think i need this
-		//fmt.Fprintf(w, "Server: %s \n", message+" | "+time.Now().Format(time.RFC3339))
+
+	case "getTweets":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Fprint(w, client.GetTweets(0))
+
+	case "getArchiveTweets":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Fprint(w, client.GetTweets(1))
+
+	case "updateMainTopicList":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Println("updateMainTopicList")
+
+	case "updateMainInterests":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Println("updateMainInterests")
+
+	case "updateArchiveTopicList":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Println("updateArchiveTopicList")
+
+	case "updateArchiveInterests":
+
+		r.ParseMultipartForm(0)
+
+		fmt.Println("updateArchiveInterests")
 
 	default:
-		fmt.Fprintf(w, "Request type other than GET or POST not supported")
+		fmt.Fprintf(w, "Request type not supported")
 
 	}
 

+ 27 - 30
index.html

@@ -17,12 +17,12 @@
 
       <div id="formBody">
         
-        <form method="POST" id="inputForm">
+        <form method="POST" id="inputTweet">
             <h1 style="font-size: large"; >Anonymous Topic Based PubSub Communication for Microblogging</h1>
             <br>
             <label for="message">Your Tweet here</label>
             <input id="message" name="message" value="house#tpc1#tpc2" type="text">
-            <button type="submit" style="width:100px;">Go ...</button>
+            <button type="submit" style="width:100px;">Post Tweet</button>
           <h1 style="font-size: medium;">Input like: "text#topic1#topic2"</h1>
         </form>   
 
@@ -33,12 +33,12 @@
 
         <div class="leftpane">
           <h1 style="font-size: large;text-align: center;">Topic list</h1>
-          <ol>
-            <li v-for="topic in topicList">
-              <input type="checkbox" :value="topic.topic" v-model="selectedTopics">
-              <label :for="topic.topic">{{topic.topic}}</label>
-            </li>
-          </ol>
+          
+          <button id="mainUpdateTopicList" method="updateMainTopicList" type="submit" style="width:100px;">Update Main Topic List</button>
+          <button id="mainUpdateInterests" method="updateMainInterests" type="submit" style="width:100px;">Update Interests</button>
+
+          <ol id="displayMainTopicList"></ol>
+
           <div class="impressum">
             <h1 style="font-size: medium;"><br>This is the gui accompanying the thesis Anonymous Topic Based PubSub Communication for Microblogging.
               <br><br>
@@ -49,33 +49,30 @@
       
         <div class="middlepane">
           <h1 style="font-size: large;text-align: center;">Tweets</h1>
-          <ul>
-            <Tweet
-              v-for="(tweet, index) in tweets"
-              :key=index
-              :topics="tweet.topics"
-              :text="tweet.text"
-            ></Tweet>
-          </ul>
+
+          <form method="getTweets" id="mainTweets">
+            <button type="submit" style="width:100px;">Get Tweets</button>
+          </form>
+
+          <ul id="displayMainTweets"></ul>
         </div>
       
         <div class="rightpane">
           <h1 style="font-size: large;text-align: center;">Topic list for archived tweets</h1>
-          <ol>
-            <li v-for="topic in topicListArchive">
-              <input type="checkbox" :value="topic.topic" v-model="selectedTopicsArchive">
-              <label :for="topic.topic">{{topic.topic}}</label>
-            </li>
-          </ol>
+          
+          form muss drum um jedes einzeln
+          <button id="archiveUpdateTopicList" method="updateMainTopicList" type="submit" style="width:100px;">Update Main Topic List</button>
+          <button id="archiveUpdateInterests" method="updateMainInterests" type="submit" style="width:100px;">Update Interests</button>
+
+          <ol id="displayArchiveTweets"></ol>
+
           <br>
-          <ul>
-            <Tweet
-              v-for="(tweet, index) in tweetsArchive"
-              :key=index
-              :topics="tweet.topics"
-              :text="tweet.text"
-            ></Tweet>
-          </ul>
+
+          <form method="getArchiveTweets" id="archiveTweets">
+            <button type="submit" style="width:100px;">Get Archive Tweets</button>
+          </form>
+
+          <ul id="displayArchiveTweets"></ul>
         </div>
       </div>
 </body>

+ 1 - 1
leader/leader.go

@@ -78,7 +78,7 @@ var extraPositions int = 10
 var collisionCounter []float64
 var clientsConnected float64
 
-var maxTimePerRound time.Duration = 5 * time.Second
+var maxTimePerRound time.Duration = 3 * time.Second
 
 //counts the number of rounds
 var round int

+ 4 - 4
lib/databaseRead.go

@@ -21,7 +21,7 @@ var dbR = make(map[string][]Tweet)
 var archive = make(map[string][]Tweet)
 
 //needs to be dividable by roundsBeforUpdate
-var roundsBeforeArchiving = -1
+var roundsBeforeArchiving = 2
 var roundsBeforeArchivingInc = roundsBeforeArchiving
 var bytesSaved float64
 
@@ -134,11 +134,11 @@ func tweetsToByteArray(tweetsToReturn [][]Tweet, whereFrom int, wantedTopics []s
 					topicPadding = append(topicPadding, topic)
 				}
 			}
-			//replaces last "," with ";;" bc there is text following and not another topic
+			//replaces last "," with ";" bc there is text following and not another topic
 			blockToAppend = blockToAppend[:len(blockToAppend)-1]
-			blockToAppend = append(blockToAppend, []byte(";;")[:]...)
+			blockToAppend = append(blockToAppend, []byte(";")[:]...)
 			blockToAppend = append(blockToAppend, []byte(tweet.Text)...)
-			blockToAppend = append(blockToAppend, []byte(";")[0])
+			blockToAppend = append(blockToAppend, []byte(";;")[:]...)
 		}
 
 		//adds padding

+ 113 - 6
main.js

@@ -2,9 +2,28 @@ console.log("JS Loaded")
 
 const url = "127.0.0.1:3000"
 
+var inputForm = document.getElementById("mainUpdateTopicList")
 
-var inputForm = document.getElementById("inputForm")
+inputForm.addEventListener("submit", (e)=>{
 
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+        method:"updateMainTopicList",
+        body:formdata,
+    })
+    .then(
+        response => response.text()
+    ).then(
+        (data) => {document.getElementById("mainTopicList").innerHTML=data}
+    ).catch(
+        error => console.error(error)
+    )
+})
+
+var inputForm = document.getElementById("mainUpdateInterests")
 
 inputForm.addEventListener("submit", (e)=>{
 
@@ -13,18 +32,106 @@ inputForm.addEventListener("submit", (e)=>{
 
     const formdata = new FormData(inputForm)
     fetch(url,{
+        method:"updateMainInterests",
+        body:formdata,
+    })
+    .then(
+        response => response.text()
+    ).catch(
+        error => console.error(error)
+    )
+})
 
-        method:"POST",
+var inputForm = document.getElementById("archiveUpdateTopicList")
+
+inputForm.addEventListener("submit", (e)=>{
+
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+        method:"updateArchiveTopicList",
+        body:formdata,
+    })
+    .then(
+        (data) => {document.getElementById("archiveUpdateTopicList").innerHTML=data}
+    ).catch(
+        error => console.error(error)
+    )
+})
+
+var inputForm = document.getElementById("archiveUpdateInterests")
+
+inputForm.addEventListener("submit", (e)=>{
+
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+        method:"updateArchiveInterests",
         body:formdata,
     })
     .then(
         response => response.text()
+    ).catch(
+        error => console.error(error)
+    )
+})
+
+var inputForm = document.getElementById("archiveUpdateTopicList")
+
+inputForm.addEventListener("submit", (e)=>{
+
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+        method:"updateArchiveTopicList",
+        body:formdata,
+    })
+    .then(
+        (data) => {document.getElementById("displayArchiveTweets").innerHTML=data}
+    ).catch(
+        error => console.error(error)
     )
-    /*.then(
-        (data) => {console.log(data);document.getElementById("serverMessageBox").innerHTML=data}
+})
+
+var inputForm = document.getElementById("mainTweets")
+
+inputForm.addEventListener("submit", (e)=>{
+
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+        method:"getTweets",
+        body:formdata,
+    }).then(
+        (data) => {document.getElementById("displayMainTweets").innerHTML=data}
+    ).catch(
+        error => console.error(error)
     )
-    */
-    .catch(
+})
+
+var inputForm = document.getElementById("inputTweet")
+
+inputForm.addEventListener("submit", (e)=>{
+
+    //prevent auto submission
+    e.preventDefault()
+
+    const formdata = new FormData(inputForm)
+    fetch(url,{
+
+        method:"POST",
+        body:formdata,
+    }).then(
+        response => response.text()
+    ).catch(
         error => console.error(error)
     )
 })