comm.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "2PPS/client"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. )
  10. var Port = ":3000"
  11. func main() {
  12. f, err := os.OpenFile("evalDataClient", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  13. if err != nil {
  14. log.Fatalf("error opening file: %v", err)
  15. }
  16. defer f.Close()
  17. log.SetOutput(f)
  18. if os.Args[len(os.Args)-1] == "1" {
  19. go client.Client(0, f)
  20. }
  21. http.HandleFunc("/", ServeFiles)
  22. fmt.Println("Serving @ : ", "http://127.0.0.1"+Port)
  23. log.Fatal(http.ListenAndServe(Port, nil))
  24. }
  25. func ServeFiles(w http.ResponseWriter, r *http.Request) {
  26. switch r.Method {
  27. case "GET":
  28. path := r.URL.Path
  29. if path == "/" {
  30. path = "index.html"
  31. } else {
  32. path = "/home/simon/goCode/AnonymousTopicBasedPubSubCommunicationforMicroblogging" + path
  33. }
  34. http.ServeFile(w, r, path)
  35. case "POST":
  36. r.ParseMultipartForm(0)
  37. tweet := r.FormValue("message")
  38. fmt.Println("Tweet to post: ", tweet)
  39. client.SetGuiTweet(tweet)
  40. case "getTweets":
  41. r.ParseMultipartForm(0)
  42. fmt.Fprint(w, client.GetTweets(0))
  43. case "getArchiveTweets":
  44. r.ParseMultipartForm(0)
  45. fmt.Fprint(w, client.GetTweets(1))
  46. case "updateMainTopicList":
  47. r.ParseMultipartForm(0)
  48. fmt.Fprint(w, client.GetTopicList(0))
  49. case "updateMainInterests":
  50. var x struct {
  51. Topics []string `json:"mainTopics"`
  52. }
  53. if err := json.NewDecoder(r.Body).Decode(&x); err != nil {
  54. panic(err)
  55. }
  56. topicsToGo := x.Topics
  57. client.UpdateInterests(topicsToGo, 0)
  58. case "updateArchiveTopicList":
  59. r.ParseMultipartForm(0)
  60. fmt.Fprint(w, client.GetTopicList(1))
  61. case "updateArchiveInterests":
  62. var x struct {
  63. Topics []string `json:"archiveTopics"`
  64. }
  65. if err := json.NewDecoder(r.Body).Decode(&x); err != nil {
  66. panic(err)
  67. }
  68. topicsToGo := x.Topics
  69. client.UpdateInterests(topicsToGo, 1)
  70. default:
  71. fmt.Fprintf(w, "Request type not supported")
  72. }
  73. }