Skip to content

Commit d8c9365

Browse files
committed
Working put, small bugs remaining in get and key comparisons in network
1 parent fd9e6b3 commit d8c9365

File tree

6 files changed

+47
-50
lines changed

6 files changed

+47
-50
lines changed

cmd/main/main.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package main
22

33
import (
4-
"fmt"
4+
//"fmt"
55
"strings"
6-
76
"github.com/Grumme2/D7024E/internal/d7024e"
87
)
98

109
func main() {
1110
meid := d7024e.NewRandomKademliaID()
1211
ip := d7024e.GetLocalIP()
1312
splitIP := strings.Split(ip, ".")
14-
fmt.Println(splitIP)
1513
if splitIP[3] == "3" {
1614
mestr := "2111111300000000000000000000123000000000"
1715
meid = d7024e.NewKademliaID(mestr)
@@ -25,10 +23,6 @@ func main() {
2523
go network.CheckNodesAwaitingResponse()
2624
kademlia.JoinNetwork()
2725

28-
fmt.Println(me.Address)
29-
// go network.Listen()
30-
// go network.CheckNodesAwaitingResponse()
31-
3226
cli := d7024e.NewCli(&kademlia)
3327
cli.AwaitCommand()
3428
}

internal/d7024e/cli.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"strings"
99
"time"
10+
"encoding/json"
1011
)
1112

1213
type cli struct {
@@ -31,12 +32,6 @@ func (cli *cli) AwaitCommand() {
3132
case "EXIT":
3233
fmt.Println("EXIT command detected")
3334
return //Exits the function and terminates the node
34-
case "PRINT":
35-
if len(inputSplit) > 1 {
36-
fmt.Println("Printing test: " + inputSplit[1])
37-
} else {
38-
fmt.Println("Error! Invalid arguments!")
39-
}
4035
case "PUT":
4136
if len(inputSplit) == 2 {
4237
fileUpload := inputSplit[1]
@@ -45,14 +40,21 @@ func (cli *cli) AwaitCommand() {
4540
cli.kademlia.Store(fileUpload) //File upload works (well atleast the RPC is sent and received properly)
4641
//See if file is uploaded
4742

48-
time.Sleep(3000 * time.Millisecond) //Sleep for 3s
43+
time.Sleep(300 * time.Millisecond) //Sleep for 0.3s
4944

5045
hashedUpload := cli.kademlia.network.MakeHash(fileUpload)
51-
dataFound, data, node := cli.kademlia.LookupData(fileUpload)
46+
47+
hashedJson, err := json.Marshal(hashedUpload)
48+
if err != nil {
49+
fmt.Println(err)
50+
}
51+
52+
dataFound, data, node := cli.kademlia.LookupData(string(hashedJson))
5253
_ = data //Prevent data declared and not used compilation error
5354
_ = node //Prevent data declared and not used compilation error
5455
if dataFound {
55-
fmt.Println("File upload successfully! Hashed upload: " + string(hashedUpload[:]))
56+
fmt.Println("File upload successfully! Hashed upload: ")
57+
fmt.Println(hashedUpload)
5658
} else {
5759
fmt.Println(dataFound)
5860
fmt.Println(data)
@@ -64,9 +66,18 @@ func (cli *cli) AwaitCommand() {
6466
fmt.Println("Error! Invalid arguments!")
6567
}
6668
case "GET":
67-
if len(inputSplit) == 2 {
69+
if len(inputSplit) > 2 {
70+
inputSplit = space.Split(inputText, 2)
6871
hash := inputSplit[1]
69-
dataFound, data, node := cli.kademlia.LookupData(hash)
72+
73+
hashedJson, err := json.Marshal(hash)
74+
if err != nil {
75+
fmt.Println(err)
76+
}
77+
78+
fmt.Println("Hashedjson: " + string(hashedJson))
79+
80+
dataFound, data, node := cli.kademlia.LookupData(string(hashedJson))
7081
_ = data //Prevent data declared and not used compilation error
7182
if dataFound {
7283
//Also return which node it was retrieved from

internal/d7024e/contact.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (contact *Contact) CalcDistance(target *KademliaID) {
2727

2828
// Less returns true if contact.distance < otherContact.distance
2929
func (contact *Contact) Less(otherContact *Contact) bool {
30-
fmt.Println(contact.distance)
31-
fmt.Println(otherContact.distance)
30+
//fmt.Println(contact.distance)
31+
//fmt.Println(otherContact.distance)
3232
return contact.distance.Less(otherContact.distance)
3333
}
3434

internal/d7024e/kademlia.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (Kademlia *Kademlia) JoinNetwork() {
3030
bootStrapNode := NewContact(&bootStrapKademID, bootStrapIP)
3131

3232
if x == "3" { // if Bootstrap node nothing needs to be done
33-
fmt.Println("bootstrapnode")
33+
//fmt.Println("bootstrapnode")
3434
return
3535
} else {
3636
i := 0
@@ -95,7 +95,7 @@ func (kademlia *Kademlia) LookupContact(target *Contact) string {
9595
}
9696

9797
if j == 10 {
98-
fmt.Printf("hej")
98+
//fmt.Printf("hej")
9999
return "ERROR! Did not get response in time"
100100
}
101101
}
@@ -127,8 +127,8 @@ func (kademlia *Kademlia) LookupContact(target *Contact) string {
127127

128128
}
129129
KTrJson := kademlia.network.KTriplesJSON(shortlist.contacts)
130-
fmt.Println(KTrJson)
131-
fmt.Println(kademlia.network.routingTable.me)
130+
//fmt.Println(KTrJson)
131+
//fmt.Println(kademlia.network.routingTable.me)
132132
return KTrJson
133133

134134
}

internal/d7024e/kademliaid.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"crypto/sha1"
55
"encoding/hex"
66
"math/rand"
7-
"fmt"
7+
//"fmt"
88
)
99

1010
// the static number of bytes in a KademliaID
@@ -25,8 +25,6 @@ func NewKademliaID(data string) KademliaID {
2525
newKademliaID[i] = decoded[i]
2626
}*/
2727
hash := sha1.Sum([]byte(data))
28-
fmt.Println("HASH------")
29-
fmt.Println(hash)
3028

3129
return hash
3230
}

internal/d7024e/network.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ func (network *Network) CheckNodesAwaitingResponse() {
7171

7272
for e := network.awaitingResponseList.Front(); e != nil; e = e.Next() {
7373
nodeTimestamp := e.Value.(AwaitingResponseObject).timestamp
74-
fmt.Println(nodeTimestamp)
75-
fmt.Println(currentTime)
76-
fmt.Println(currentTime - nodeTimestamp)
77-
fmt.Println(e.Value.(AwaitingResponseObject).oldNode)
78-
fmt.Println(e.Value.(AwaitingResponseObject).newNode)
7974
if (currentTime - nodeTimestamp) >= 5 { //If 5 seconds or more have passed
8075
network.routingTable.RemoveContact(e.Value.(AwaitingResponseObject).oldNode)
8176
network.routingTable.AddContact(e.Value.(AwaitingResponseObject).newNode)
@@ -178,9 +173,8 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
178173
case "OK":
179174
responseType = "NONE"
180175
case "STORE":
181-
key := network.AddToStore(decodedData.Content)
176+
network.AddToStore(decodedData.Content)
182177
responseType = "OK"
183-
responseContent = string(key[:])
184178
case "FINDVALUE":
185179
dataFound, data := network.LookForData(decodedData.Content)
186180
if dataFound {
@@ -207,7 +201,7 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
207201

208202
case "FINDNODE_RESPONSE":
209203
network.lookUpContactResponse = LookUpContactResponse{decodedData.Content}
210-
fmt.Println(network.lookUpContactResponse)
204+
//fmt.Println(network.lookUpContactResponse)
211205
responseType = "NONE"
212206

213207
case "PONG":
@@ -224,15 +218,24 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
224218
}
225219
}
226220

227-
func (network *Network) AddToStore(message string) KademliaID {
221+
func (network *Network) AddToStore(message string) {
228222
hxMsg := network.MakeHash(message)
229-
network.routingTable.me.KeyValueStore[string(hxMsg[:])] = message
230-
return hxMsg
223+
224+
hxMsgJSON, err := json.Marshal(hxMsg)
225+
if err != nil {
226+
fmt.Println(err)
227+
}
228+
229+
network.routingTable.me.KeyValueStore[string(hxMsgJSON)] = message
231230
}
232231

233232
func (network *Network) LookForData(hash string) (bool, string) {
234233
for key, element := range network.routingTable.me.KeyValueStore {
235-
if string(key[:]) == hash {
234+
fmt.Println("LookForData loop")
235+
fmt.Println("Key: " + key)
236+
fmt.Println("Hash: " + hash)
237+
238+
if key == hash {
236239
fmt.Println("LookForData found element: " + element)
237240
return true, element
238241
}
@@ -241,23 +244,14 @@ func (network *Network) LookForData(hash string) (bool, string) {
241244
}
242245

243246
func (network *Network) MakeHash(message string) KademliaID {
244-
hash := sha1.Sum([]byte(message))
245-
fmt.Println("MAKEHASH------")
246-
fmt.Println(hash)
247-
248-
return hash
247+
hash := sha1.Sum([]byte(message))
248+
return hash
249249
}
250250

251251
func (network *Network) storeRPC(message RPC) {
252-
hash := network.MakeHash(message.Content)
253-
fmt.Printf(string(hash[:]))
254252
network.SendMessage(message)
255253
}
256254

257-
func (network *Network) idToString(id KademliaID) string {
258-
return string(id[:])
259-
}
260-
261255
func (network *Network) JSONEncodeLookUpDataResponse(unencodedResponse LookUpDataResponse) string {
262256
encoded, err := json.Marshal(unencodedResponse)
263257
if err != nil {

0 commit comments

Comments
 (0)