-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (72 loc) · 2.01 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
package main
import (
"flag"
"fmt"
"os" // way to interact with the OS
"time"
"github.com/anchi205/Echonet/src"
"github.com/sirupsen/logrus" // structured logging.
)
// Called automatically before the main function
func init() {
// Sets the log format to a colored, timestamped text format
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
// Log output to os.Stdout (standard output)
logrus.SetOutput(os.Stdout)
}
func main() {
// Define input flags
username := flag.String("user", "", "Username to use in the chatroom.")
chatroom := flag.String("room", "", "Chatroom to join.")
loglevel := flag.String("log", "", "Level of logs to print.")
discovery := flag.String("discover", "", "Method to use for discovery.")
// Parse input flags
flag.Parse()
// Set the log level
switch *loglevel {
case "panic", "PANIC":
logrus.SetLevel(logrus.PanicLevel)
case "fatal", "FATAL":
logrus.SetLevel(logrus.FatalLevel)
case "error", "ERROR":
logrus.SetLevel(logrus.ErrorLevel)
case "warn", "WARN":
logrus.SetLevel(logrus.WarnLevel)
case "debug", "DEBUG":
logrus.SetLevel(logrus.DebugLevel)
default:
logrus.SetLevel(logrus.InfoLevel)
}
// Display the introductory welcome msg
fmt.Println("Echonet is starting.")
fmt.Println("This may take a few seconds.")
fmt.Println()
// Create a new P2PHost
p2phost := src.NewP2P()
logrus.Infoln("P2P Setup Completed!")
// Connect to peers with the chosen discovery method
switch *discovery {
case "announce":
p2phost.AnnounceConnect()
break
case "advertise":
p2phost.AdvertiseConnect()
break
default:
p2phost.AdvertiseConnect()
}
logrus.Infoln("Connected to Peers")
// Join the chat room
chatapp, _ := src.JoinChatRoom(p2phost, *username, *chatroom)
logrus.Infof("Joined the '%s' chatroom as '%s'", chatapp.RoomName, chatapp.UserName)
// Wait for network setup to complete
time.Sleep(time.Second * 5)
// Create the Chat UI
ui := src.NewUI()
// Start the UI system
ui.Run()
}