-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
99 lines (85 loc) · 2.47 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/manishmeganathan/peerchat/src"
"github.com/sirupsen/logrus"
)
const figlet = `
W E L C O M E T O
db db
88 88
.8d888b. .d8888b. .d8888b. .d8888b. .d8888b. 88d888b. .d8888b. d8888P
88' '88 88ooood8 88ooood8 88' '88 88' 88' '88 88' '88 88
88. .88 88. 88. 88 88. 88 88 88. .88 88
888888P' '88888P' '88888P' db '88888P' db db '8888888 '88P
88
dP
`
func init() {
// Log as Text with color
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC822,
})
// Log to stdout
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 "info", "INFO":
logrus.SetLevel(logrus.InfoLevel)
case "debug", "DEBUG":
logrus.SetLevel(logrus.DebugLevel)
case "trace", "TRACE":
logrus.SetLevel(logrus.TraceLevel)
default:
logrus.SetLevel(logrus.InfoLevel)
}
// Display the welcome figlet
fmt.Println(figlet)
fmt.Println("The PeerChat Application is starting.")
fmt.Println("This may take upto 30 seconds.")
fmt.Println()
// Create a new P2PHost
p2phost := src.NewP2P()
logrus.Infoln("Completed P2P Setup")
// Connect to peers with the chosen discovery method
switch *discovery {
case "announce":
p2phost.AnnounceConnect()
case "advertise":
p2phost.AdvertiseConnect()
default:
p2phost.AdvertiseConnect()
}
logrus.Infoln("Connected to Service 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(chatapp)
// Start the UI system
ui.Run()
}