-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
197 lines (181 loc) · 8.56 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"math/rand"
"net"
"net/rpc"
"os"
"strconv"
"time"
"github.com/fauzxan/ivy/central"
"github.com/fauzxan/ivy/client"
// "time"
"github.com/fatih/color"
)
// Color coded logs
var system = color.New(color.FgCyan).Add(color.BgBlack)
var systemTime = color.New(color.FgHiRed).Add(color.BgBlack)
/*
Show a list of options to choose from.
*/
func showmenu() {
}
func main() {
// get port from cli arguments (specified by user)
helper := "" // IP address of the port number we are using to join. Will be specified iff I am not the central manager.
backup := ""
reboot_address := ""
doIPing := false
doesTheOtherPing := false
var port int
// var joinerPort string
for i, arg := range os.Args {
switch arg {
case "-u":
// Specified if you are the client, and you want to get the clientlist metadata from the central manager
if i+1 >= len(os.Args) {
system.Println("Enter valid helper port!!")
}
helper = os.Args[i+1]
case "-cm":
// Specified if you are the central manager
doIPing = false // If I am the central, then I don't ping, the other does.
doesTheOtherPing = true
if i+1 >= len(os.Args){
system.Println("There is no backup!")
} else{
backup = os.Args[i+1]
system.Println("There is a backup! IP of backup is: ", backup)
doIPing = true // If I am backup then I ping, the other doesn't (main)
doesTheOtherPing = false
}
case "-r":
if i+1 >= len(os.Args){
system.Println("System is rebooting!! But not enough arguments!")
} else{
reboot_address = os.Args[i+1]
system.Println("Will get up and running again on: ", reboot_address)
}
default:
}
}
// Create new Node object for yourself
if helper != "" { // case when you are a client
me := client.Client{}
me.ServerIP = helper
system.Println("Joining using ", me.ServerIP)
port, _ = GetFreePort()
addr := GetOutboundIP().String() + ":" + strconv.Itoa(port)
me.IP = addr
system.Println("My IP is:", me.IP)
// Bind yourself to a port and listen to it
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
system.Println("Error resolving TCP address", err)
}
inbound, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
system.Println("Could not listen to TCP address", err)
}
// Register RPC methods and accept incoming requests
rpc.Register(&me)
system.Println("Client is runnning at IP address: ", tcpAddr)
go rpc.Accept(inbound)
me.JoinNetwork(helper)
// Keep the parent thread alive
for {
system.Println("********************************")
system.Println("\t\tMENU")
system.Println("Press 1 to see the client list")
system.Println("Press 2 to see which Central Manager you are contacting now")
system.Println("Press 3 to start the read/write loop. The client will randomly read/write from/to a page")
system.Println("Press 4 to see the pages cached in this client")
system.Println("Press m to see the menu")
system.Println("********************************")
system.Println("Alive")
var input string
fmt.Scanln(&input)
switch input {
case "1":
system.Println("Clientlist requested")
me.PrintClientList()
case "2":
system.Println("Server IP Requested")
me.PrintCentralIP()
case "3":
go func(){
for
{
res :=rand.Intn(2)
if res == 1{
start := time.Now().UnixMilli()
me.ReadRequest()
end := time.Now().UnixMilli()
systemTime.Println("***********\nTIME TAKEN:", end-start, "\n***********")
} else{
start := time.Now().UnixMilli()
me.WriteRequest()
end := time.Now().UnixMilli()
systemTime.Println("***********\nTIME TAKEN:", end-start, "\n***********")
}
}
}()
case "4":
me.PrintPages()
default:
system.Println("Enter valid input")
}
}
} else { // case when you are a cm, or backup cm
me := central.Central{}
var addr string
if reboot_address == ""{
port, _ = GetFreePort()
addr = GetOutboundIP().String() + ":" + strconv.Itoa(port)
} else {
addr = reboot_address
}
me.DoIPing = doIPing
me.DoesTheOtherPing = doesTheOtherPing
me.IP = addr
system.Println("My IP is:", me.IP)
// Bind yourself to a port and listen to it
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
system.Println("Error resolving TCP address", err)
}
inbound, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
system.Println("Could not listen to TCP address", err)
}
// Register RPC methods and accept incoming requests
rpc.Register(&me)
system.Println("Central manager is runnning at IP address:", tcpAddr)
go rpc.Accept(inbound)
go me.CreateNetwork(backup)
go SetupCloseHandler(&me)
showmenu()
// Keep the parent thread alive
for {
system.Println("********************************")
system.Println("\t\tMENU")
system.Println("Press 1 to see the client list")
system.Println("Press 2 to see the records metadata. This will show you which nodes own which pages, and who has copies")
system.Println("Press m to see the menu")
system.Println("********************************")
system.Println("Alive")
var input string
fmt.Scanln(&input)
switch input {
case "1":
system.Println("Clientlist requested")
me.PrintClientList()
case "2":
system.Println("Records requested")
me.PrintRecords()
default:
system.Println("Enter valid input")
}
}
}
}