-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (69 loc) · 2.43 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
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"github.com/sigtot/elevio"
"github.com/sigtot/sanntid/buttons"
"github.com/sigtot/sanntid/buyer"
"github.com/sigtot/sanntid/elev"
"github.com/sigtot/sanntid/indicators"
"github.com/sigtot/sanntid/orders"
"github.com/sigtot/sanntid/orderwatcher"
"github.com/sigtot/sanntid/seller"
"github.com/sigtot/sanntid/types"
"github.com/sigtot/sanntid/utils"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
"math/rand"
"os"
"os/signal"
"sync"
"time"
)
const dbName = "orderwatcher.db"
const dbPerms = 0600
const dbTimeout = 300
const moduleName = "MAIN"
const defaultElevPort = 15657
func main() {
rand.Seed(time.Now().UnixNano())
// Read elevator server port flag
var elevPort = flag.Int("port", defaultElevPort, "port for connecting to the elevator server")
flag.Parse()
log := logrus.New()
utils.Log(log, moduleName, "Starting elevator")
var wg sync.WaitGroup
goalArrivals := make(chan types.Order)
currentGoals := make(chan types.Order)
floorArrivals := make(chan int)
quitElev := make(chan int)
go elevio.PollFloorSensor(floorArrivals)
elevator := elev.StartElevController(goalArrivals, currentGoals, floorArrivals, *elevPort, quitElev, &wg)
callsForSale := make(chan types.Call)
buttonEvents := make(chan elevio.ButtonEvent)
go elevio.PollButtons(buttonEvents)
buttons.StartButtonHandler(buttonEvents, callsForSale)
quitIndicators := make(chan int)
indicators.StartIndicatorHandler(quitIndicators, &wg)
oh, newOrders := orders.StartOrderHandler(currentGoals, goalArrivals, elevator)
buyer.StartBuying(oh, newOrders)
seller.StartSelling(callsForSale)
orderWatcherDb, err := bolt.Open(dbName, dbPerms, &bolt.Options{Timeout: dbTimeout * time.Millisecond})
utils.OkOrPanic(err)
quitOrderWatcher := make(chan int)
orderwatcher.StartOrderWatcher(callsForSale, orderWatcherDb, quitOrderWatcher, &wg)
quitDistributor := make(chan int)
orderwatcher.StartDbDistributor(orderWatcherDb, dbName, quitDistributor)
utils.Log(log, moduleName, "Successfully initialized all modules")
sigInt := make(chan os.Signal, 1)
signal.Notify(sigInt, os.Interrupt)
<-sigInt
signal.Stop(sigInt) // Stop trapping interrupt signal to give it back its usual behavior
utils.Log(log, moduleName, "Gracefully stopping all modules. Do ^C again to force")
quitElev <- 0
quitIndicators <- 0
quitOrderWatcher <- 0
err = orderWatcherDb.Close()
utils.OkOrPanic(err)
wg.Wait()
utils.Log(log, moduleName, "Stopped elevator")
}