comm.go 1.7 KB

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