-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (114 loc) · 4.08 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
package main
import (
"bufio"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"github.com/thamizhv/tgnutella/cache"
"github.com/thamizhv/tgnutella/constants"
"github.com/thamizhv/tgnutella/handlers/cmd"
"github.com/thamizhv/tgnutella/handlers/descriptor"
"github.com/thamizhv/tgnutella/handlers/files"
"github.com/thamizhv/tgnutella/servent"
)
func main() {
serventPort, err := validateAndGetPort(os.Args[1:])
if err != nil {
fmt.Println(err.Error())
return
}
parsedServentPort, err := strconv.ParseUint(serventPort, 10, 16)
if err != nil {
fmt.Printf("Please enter valid servent port number:%s\n", serventPort)
return
}
dir, err := os.Getwd()
if err != nil {
fmt.Printf("Error in getting current working directory:%v\n", err)
return
}
serventAddress := net.JoinHostPort(constants.LocalHost, serventPort)
httpPort := parsedServentPort + 1
httpPortString := strconv.FormatUint(httpPort, 10)
httpAddress := net.JoinHostPort(constants.LocalHost, httpPortString)
go startHTTPServer(httpPortString, dir)
fileHelper := files.NewFileHelper(dir)
serventCacheHelper := cache.NewServentCacheHelper()
descriptorHelper := descriptor.NewDescriptorHelper(constants.LocalHost, uint16(parsedServentPort),
uint16(httpPort), serventCacheHelper, fileHelper)
servent := servent.NewServent(serventAddress, serventCacheHelper)
addCMDHandlersTo(serventAddress, httpAddress, servent, serventCacheHelper, fileHelper)
addDescriptorHandlersTo(servent, descriptorHelper)
err = servent.Start()
if err != nil {
fmt.Printf("Error in starting servent :%s\n", err.Error())
return
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
go func() {
<-sigChan
fmt.Print("\nshutting down...\n")
os.Exit(0)
}()
var command string
cmdPlaceHolder := serventAddress + ">"
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(cmdPlaceHolder)
command, _ = reader.ReadString('\n')
cmd, arg := getCommandAndArgs(command)
if cmd == "" {
continue
}
err := servent.Handle(cmd, arg)
if err != nil {
fmt.Printf("Error in executing command %s, with argument %s :%v\n", cmd, arg, err)
}
}
}
func addCMDHandlersTo(serventAddres, httpAddress string, servent servent.Servent, serventCacheHelper *cache.ServentCacheHelper, fileHelper files.FileHandler) {
servent.AddCmdHandler(constants.CmdTypeHelp, cmd.NewHelpHandler())
servent.AddCmdHandler(constants.CmdTypeOpen, cmd.NewOpenHandler(serventAddres, httpAddress, serventCacheHelper))
servent.AddCmdHandler(constants.CmdTypeClose, cmd.NewCloseHandler(serventCacheHelper))
servent.AddCmdHandler(constants.CmdTypeInfo, cmd.NewInfoHandler(serventCacheHelper))
servent.AddCmdHandler(constants.CmdTypeFind, cmd.NewFindHandler(serventCacheHelper, fileHelper))
servent.AddCmdHandler(constants.CmdTypeGet, cmd.NewGetHandler(serventCacheHelper, fileHelper))
}
func addDescriptorHandlersTo(servant servent.Servent, descriptorHelper *descriptor.DescriptorHelper) {
servant.AddDescriptorHandler(constants.PingDescriptor, descriptor.NewPingDescriptor(descriptorHelper))
servant.AddDescriptorHandler(constants.PongDescriptor, descriptor.NewPongDescriptor(descriptorHelper))
servant.AddDescriptorHandler(constants.QueryDescriptor, descriptor.NewQueryDescriptor(descriptorHelper))
servant.AddDescriptorHandler(constants.QueryHitDescriptor, descriptor.NewQueryHitDescriptor(descriptorHelper))
}
func getCommandAndArgs(command string) (string, string) {
command = strings.TrimSuffix(command, "\n")
var cmd, arg string
cmd = command
split := strings.Split(command, " ")
if len(split) > 1 {
cmd = split[0]
arg = split[1]
}
return cmd, arg
}
func validateAndGetPort(args []string) (string, error) {
length := len(args)
if length == 0 {
return "", fmt.Errorf("Error: missing arguments servent port\n%s", constants.UsageText)
}
return args[0], nil
}
func startHTTPServer(port string, directory string) {
fs := http.FileServer(http.Dir(directory))
fmt.Printf("http server listening at port %s\n", port)
err := http.ListenAndServe(":"+port, fs)
if err != nil {
fmt.Print("\nerror in invoking http server\n")
os.Exit(1)
}
}