@@ -2,11 +2,13 @@ package d7024e
2
2
3
3
import (
4
4
"container/list"
5
- "encoding/hex"
5
+ // "encoding/hex"
6
6
"encoding/json"
7
7
"fmt"
8
8
"net"
9
9
"time"
10
+ "crypto/sha1"
11
+ "regexp"
10
12
)
11
13
12
14
type Network struct {
@@ -70,11 +72,6 @@ func (network *Network) CheckNodesAwaitingResponse() {
70
72
71
73
for e := network .awaitingResponseList .Front (); e != nil ; e = e .Next () {
72
74
nodeTimestamp := e .Value .(AwaitingResponseObject ).timestamp
73
- fmt .Println (nodeTimestamp )
74
- fmt .Println (currentTime )
75
- fmt .Println (currentTime - nodeTimestamp )
76
- fmt .Println (e .Value .(AwaitingResponseObject ).oldNode )
77
- fmt .Println (e .Value .(AwaitingResponseObject ).newNode )
78
75
if (currentTime - nodeTimestamp ) >= 5 { //If 5 seconds or more have passed
79
76
network .routingTable .RemoveContact (e .Value .(AwaitingResponseObject ).oldNode )
80
77
network .routingTable .AddContact (e .Value .(AwaitingResponseObject ).newNode )
@@ -177,9 +174,8 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
177
174
case "OK" :
178
175
responseType = "NONE"
179
176
case "STORE" :
180
- key := network .AddToStore (decodedData .Content )
177
+ network .AddToStore (decodedData .Content )
181
178
responseType = "OK"
182
- responseContent = key
183
179
case "FINDVALUE" :
184
180
dataFound , data := network .LookForData (decodedData .Content )
185
181
if dataFound {
@@ -206,7 +202,7 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
206
202
207
203
case "FINDNODE_RESPONSE" :
208
204
network .lookUpContactResponse = LookUpContactResponse {decodedData .Content }
209
- fmt .Println (network .lookUpContactResponse )
205
+ // fmt.Println(network.lookUpContactResponse)
210
206
responseType = "NONE"
211
207
212
208
case "PONG" :
@@ -223,14 +219,31 @@ func (network *Network) ListenHandler(receivedData []byte, connection *net.UDPCo
223
219
}
224
220
}
225
221
226
- func (network * Network ) AddToStore (message string ) string {
222
+ func (network * Network ) AddToStore (message string ) {
227
223
hxMsg := network .MakeHash (message )
228
- network .routingTable .me .KeyValueStore [hxMsg ] = message
229
- return hxMsg
224
+
225
+ hxMsgJSON , err := json .Marshal (hxMsg )
226
+ if err != nil {
227
+ fmt .Println (err )
228
+ }
229
+
230
+ network .routingTable .me .KeyValueStore [string (hxMsgJSON )] = message
230
231
}
231
232
232
233
func (network * Network ) LookForData (hash string ) (bool , string ) {
234
+
235
+ //Corrects hash if it is formatted wrong
236
+ if (string (hash [0 ]) == "\" " ){
237
+ hash = hash [1 :len (hash )- 1 ]
238
+ var re = regexp .MustCompile (`[ ]` )
239
+ hash = re .ReplaceAllString (hash , `,` )
240
+ }
241
+
233
242
for key , element := range network .routingTable .me .KeyValueStore {
243
+ //fmt.Println("LookForData loop")
244
+ //fmt.Println("Key: " + key)
245
+ //fmt.Println("Hash: " + hash)
246
+
234
247
if key == hash {
235
248
fmt .Println ("LookForData found element: " + element )
236
249
return true , element
@@ -239,14 +252,12 @@ func (network *Network) LookForData(hash string) (bool, string) {
239
252
return false , ""
240
253
}
241
254
242
- func (network * Network ) MakeHash (message string ) string {
243
- hx := hex . EncodeToString ([]byte (message ))
244
- return hx
255
+ func (network * Network ) MakeHash (message string ) KademliaID {
256
+ hash := sha1 . Sum ([]byte (message ))
257
+ return hash
245
258
}
246
259
247
260
func (network * Network ) storeRPC (message RPC ) {
248
- hash := network .MakeHash (message .Content )
249
- fmt .Printf (hash )
250
261
network .SendMessage (message )
251
262
}
252
263
0 commit comments