Skip to content

Commit 3ca75a5

Browse files
authored
Merge pull request #52 from Akilan1999/serverports
Added possibility to run server on any port
2 parents c3647c2 + 4e0fd09 commit 3ca75a5

File tree

10 files changed

+99
-23
lines changed

10 files changed

+99
-23
lines changed

client/Iptable.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// UpdateIpTable Does the following to update it's IP table
16-
func UpdateIpTable(IpAddress string) error {
16+
func UpdateIpTable(IpAddress string, serverPort string) error {
1717

1818
config, err := config.ConfigInit()
1919
if err != nil {
@@ -22,16 +22,17 @@ func UpdateIpTable(IpAddress string) error {
2222

2323
client := http.Client{}
2424

25+
2526
var resp []byte
2627

2728
version := p2p.Ip4or6(IpAddress)
2829
if version == "version 6" {
29-
resp, err = UploadMultipartFile(client,"http://["+IpAddress+"]:8088/IpTable","json",config.IPTable)
30+
resp, err = UploadMultipartFile(client,"http://[" + IpAddress + "]:" + serverPort + "/IpTable","json",config.IPTable)
3031
if err != nil {
3132
return err
3233
}
3334
} else {
34-
resp, err = UploadMultipartFile(client,"http://"+IpAddress+":8088/IpTable","json",config.IPTable)
35+
resp, err = UploadMultipartFile(client,"http://" + IpAddress + ":" + serverPort + "/IpTable","json",config.IPTable)
3536
if err != nil {
3637
return err
3738
}
@@ -114,9 +115,9 @@ func UpdateIpTableListClient() error {
114115
//}
115116

116117
if Addresses.IpAddress[j].Ipv6 != "" {
117-
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6)
118+
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
118119
} else {
119-
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4)
120+
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
120121
}
121122

122123
if err != nil {

client/ServerSpecs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
func GetSpecs(IP string)(*server.SysInfo,error) {
1616
var URL string
1717
version := p2p.Ip4or6(IP)
18+
19+
//Get port number of the server
20+
serverPort, err := GetServerPort(IP)
21+
if err != nil {
22+
return nil, err
23+
}
24+
1825
if version == "version 6" {
1926
URL = "http://[" + IP + "]:" + serverPort + "/server_info"
2027
} else {

client/container.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import (
1010
"strconv"
1111
)
1212

13-
const (
13+
var (
1414
serverPort = "8088"
15+
client = http.Client{}
1516
)
1617

17-
18-
var client = http.Client{}
19-
2018
// StartContainer Start container using REST api Implementation
2119
// From the selected server IP address
2220
// TODO: Test cases for this function
@@ -25,6 +23,13 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d
2523
// Passes URL with number of TCP ports to allocated and to give GPU access to the docker container
2624
var URL string
2725
version := p2p.Ip4or6(IP)
26+
27+
//Get port number of the server
28+
serverPort, err := GetServerPort(IP)
29+
if err != nil {
30+
return nil, err
31+
}
32+
2833
if version == "version 6" {
2934
URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName
3035
} else {
@@ -64,6 +69,13 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d
6469
func RemoveContianer(IP string,ID string) error {
6570
var URL string
6671
version := p2p.Ip4or6(IP)
72+
73+
//Get port number of the server
74+
serverPort, err := GetServerPort(IP)
75+
if err != nil {
76+
return err
77+
}
78+
6779
if version == "version 6" {
6880
URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID
6981
} else {
@@ -99,6 +111,13 @@ func ViewContainers(IP string)(*docker.DockerContainers, error){
99111
// Passes URL with route /ShowImages
100112
var URL string
101113
version := p2p.Ip4or6(IP)
114+
115+
//Get port number of the server
116+
serverPort, err := GetServerPort(IP)
117+
if err != nil {
118+
return nil, err
119+
}
120+
102121
if version == "version 6" {
103122
URL = "http://[" + IP + "]:" + serverPort + "/ShowImages"
104123
} else {
@@ -127,6 +146,28 @@ func ViewContainers(IP string)(*docker.DockerContainers, error){
127146
return &Result, nil
128147
}
129148

149+
// GetServerPort Helper function to do find out server port information
150+
func GetServerPort(IpAddress string) (string,error) {
151+
// Getting information from the clients ip table
152+
ipTable, err := p2p.ReadIpTable()
153+
if err != nil {
154+
return "",err
155+
}
156+
157+
// Iterate thorough ip table struct and find
158+
// out which port is for the ip address provided
159+
// in the parameter of the function
160+
for _, address := range ipTable.IpAddress {
161+
// If we found a match then return a port
162+
if address.Ipv4 == IpAddress || address.Ipv6 == IpAddress {
163+
return address.ServerPort, nil
164+
}
165+
}
166+
167+
// We return default port
168+
return "8088",nil
169+
}
170+
130171
// PrintStartContainer Prints results Generated container
131172
//func PrintStartContainer(d *docker.DockerVM){
132173
// fmt.Println("ID : " + fmt.Sprint(d.ID))
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
{
2-
"TrackContainer": []
3-
}

cmd/action.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@ var CliAction = func(ctx *cli.Context) error {
2323
if err != nil {
2424
fmt.Print(err)
2525
}
26-
27-
p2p.PrintIpTable()
26+
// Reads from ip table and passes it
27+
// on to struct print function
28+
Servers, err := p2p.ReadIpTable()
29+
if err != nil {
30+
return err
31+
}
32+
client.PrettyPrint(Servers)
2833
}
2934

3035
// Displays the IP table
3136
if ServerList {
32-
p2p.PrintIpTable()
37+
// Reads from ip table and passes it
38+
// on to struct print function
39+
Servers, err := p2p.ReadIpTable()
40+
if err != nil {
41+
return err
42+
}
43+
client.PrettyPrint(Servers)
3344
}
3445

3546
// Add provided IP to the IP table
@@ -51,6 +62,13 @@ var CliAction = func(ctx *cli.Context) error {
5162
} else {
5263
IpAddr.Ipv4 = AddServer
5364
}
65+
66+
// If a server port is provided then set it
67+
if Ports != "" {
68+
IpAddr.ServerPort = Ports
69+
} else {
70+
IpAddr.ServerPort = "8088"
71+
}
5472
// Append IP address to variable result which
5573
// is a list
5674
res.IpAddress = append(res.IpAddress, IpAddr)

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Config struct {
2828
IPV6Address string
2929
PluginPath string
3030
TrackContainersPath string
31+
ServerPort string
3132
//NetworkInterface string
3233
//NetworkInterfaceIPV6Index int
3334
}
@@ -96,6 +97,7 @@ func SetDefaults() error {
9697
defaults["IPV6Address"] = ""
9798
defaults["PluginPath"] = defaultPath + "plugin/deploy"
9899
defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json"
100+
defaults["ServerPort"] = "8088"
99101
//defaults["NetworkInterface"] = "wlp0s20f3"
100102
//defaults["NetworkInterfaceIPV6Index"] = "2"
101103

p2p/ip_table.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"ip_address": [
33
{
4-
"ipv4": "172.104.44.195",
4+
"ipv4": "139.162.246.221",
55
"ipv6": "",
66
"latency": 0,
77
"download": 0,
8-
"upload": 0
8+
"upload": 0,
9+
"serverport": "8088"
910
}
1011
]
1112
}

p2p/iptable.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type IpAddress struct {
2323
Latency time.Duration `json:"latency"`
2424
Download float64 `json:"download"`
2525
Upload float64 `json:"upload"`
26+
ServerPort string `json:"serverport"`
2627
}
2728

2829
type IP struct {
@@ -69,6 +70,7 @@ func ReadIpTable()(*IpAddresses ,error){
6970
}
7071
PublicIP.Ipv4 = ip
7172
PublicIP.Ipv6 = ipv6
73+
PublicIP.ServerPort = config.ServerPort
7274

7375
// Updates current machine IP address to the IP table
7476
ipAddresses.IpAddress = append(ipAddresses.IpAddress, PublicIP)
@@ -120,6 +122,7 @@ func PrintIpTable() error {
120122
"-----------------\n",table.IpAddress[i].Ipv4,table.IpAddress[i].Ipv6,
121123
table.IpAddress[i].Latency)
122124
}
125+
123126
return nil
124127
}
125128

p2p/testingMetrics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (s *IpAddress)UploadSpeed() error {
105105

106106
b, w := createMultipartFormData("file",config.SpeedTestFile)
107107

108-
req, err := http.NewRequest("GET", "http://" + s.Ipv4 + ":8088/upload", &b)
108+
req, err := http.NewRequest("GET", "http://" + s.Ipv4 + ":" + s.ServerPort + "/upload", &b)
109109
if err != nil {
110110
return err
111111
}
@@ -152,10 +152,10 @@ func (s *IpAddress) PingTest() error {
152152
//pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt"
153153
var pingURL string
154154
if s.Ipv6 != "" {
155-
pingURL = "http://[" + s.Ipv6 + "]:8088/server_info"
155+
pingURL = "http://[" + s.Ipv6 + "]:" + s.ServerPort + "/server_info"
156156
s.Ipv4 = ""
157157
} else {
158-
pingURL = "http://" + s.Ipv4 + ":8088/server_info"
158+
pingURL = "http://" + s.Ipv4 + ":" + s.ServerPort + "/server_info"
159159
}
160160

161161
l := time.Duration(100000000000) // 10sec

server/server.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func Server() error{
122122
c.String(http.StatusOK, "success")
123123
})
124124

125-
//Show images avaliable
125+
//Show images available
126126
r.GET("/ShowImages", func(c *gin.Context) {
127127
resp, err := docker.ViewAllContainers()
128128
if err != nil {
@@ -131,8 +131,14 @@ func Server() error{
131131
c.JSON(http.StatusOK, resp)
132132
})
133133

134-
//Port running on
135-
err := r.Run(":8088")
134+
//Get Server port based on the config file
135+
config, err := config.ConfigInit()
136+
if err != nil {
137+
return err
138+
}
139+
140+
// Run gin server on the specified port
141+
err = r.Run(":" + config.ServerPort)
136142
if err != nil {
137143
return err
138144
}

0 commit comments

Comments
 (0)