This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
75 lines (51 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
comm "github.com/IITH-POPL2-Jan2018/concurrency-13/src/communication"
constants "github.com/IITH-POPL2-Jan2018/concurrency-13/src/constants"
message "github.com/IITH-POPL2-Jan2018/concurrency-13/src/message"
)
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
/* MAIN */
func main() {
addr, err := determineListenAddress()
if err != nil {
log.Fatal(err)
}
server := comm.NewServer("/")
fmt.Println("Client Count before gor: ", server.ClientCount)
go server.Listen()
go updateServiceRoutine(server)
fmt.Println("Client Count after gor: ", server.ClientCount)
log.Fatal(http.ListenAndServe(addr, nil))
fmt.Println(server.Pattern)
}
// Requests Gameplay Update from all clients concurrently, at regular intervals.
func updateServiceRoutine(s *comm.Server) {
/* UPDATE Service */
ticker := time.NewTicker(constants.UpdateRequestInterval) // Timer
go func() {
for range ticker.C {
select {
case stop := <-s.CeaseUpdates:
if stop { // if stop is true (no more updates required)
log.Println("Update cease signal received. Update Service Routine ceased.")
return
}
default:
uSignal := message.Message{Message: "requestUpdate"}
s.Broadcast(uSignal) // Send the request for update
}
}
}()
}