Skip to content

Commit 87739b3

Browse files
Jeromy Johnsonjbenet
Jeromy Johnson
authored andcommitted
a little more work on message handling stuff
1 parent 3444d41 commit 87739b3

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

routing/dht/dht.go

+65-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package dht
22

33
import (
4+
"sync"
5+
6+
peer "github.com/jbenet/go-ipfs/peer"
47
swarm "github.com/jbenet/go-ipfs/swarm"
58
u "github.com/jbenet/go-ipfs/util"
69
"code.google.com/p/goprotobuf/proto"
7-
"sync"
810
)
911

1012
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
@@ -16,6 +18,9 @@ type IpfsDHT struct {
1618

1719
network *swarm.Swarm
1820

21+
// local data (TEMPORARY: until we formalize data storage with datastore)
22+
data map[string][]byte
23+
1924
// map of channels waiting for reply messages
2025
listeners map[uint64]chan *swarm.Message
2126
listenLock sync.RWMutex
@@ -38,22 +43,28 @@ func (dht *IpfsDHT) handleMessages() {
3843
}
3944

4045
// Note: not sure if this is the correct place for this
41-
dht.listenLock.RLock()
42-
ch, ok := dht.listeners[pmes.GetId()]
43-
dht.listenLock.RUnlock()
44-
if ok {
45-
ch <- mes
46+
if pmes.GetResponse() {
47+
dht.listenLock.RLock()
48+
ch, ok := dht.listeners[pmes.GetId()]
49+
dht.listenLock.RUnlock()
50+
if ok {
51+
ch <- mes
52+
}
53+
54+
// this is expected behaviour during a timeout
55+
u.DOut("Received response with nobody listening...")
56+
continue
4657
}
4758
//
4859

49-
// Do something else with the messages?
5060
switch pmes.GetType() {
51-
case DHTMessage_ADD_PROVIDER:
61+
case DHTMessage_GET_VALUE:
62+
dht.handleGetValue(mes.Peer, pmes)
63+
case DHTMessage_PUT_VALUE:
5264
case DHTMessage_FIND_NODE:
65+
case DHTMessage_ADD_PROVIDER:
5366
case DHTMessage_GET_PROVIDERS:
54-
case DHTMessage_GET_VALUE:
5567
case DHTMessage_PING:
56-
case DHTMessage_PUT_VALUE:
5768
}
5869

5970
case <-dht.shutdown:
@@ -62,6 +73,44 @@ func (dht *IpfsDHT) handleMessages() {
6273
}
6374
}
6475

76+
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) {
77+
val, found := dht.data[pmes.GetKey()]
78+
if found {
79+
isResponse := true
80+
resp := new(DHTMessage)
81+
resp.Response = &isResponse
82+
resp.Id = pmes.Id
83+
resp.Key = pmes.Key
84+
resp.Value = val
85+
} else {
86+
// Find closest node(s) to desired key and reply with that info
87+
// TODO: this will need some other metadata in the protobuf message
88+
// to signal to the querying node that the data its receiving
89+
// is actually a list of other nodes
90+
}
91+
}
92+
93+
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) {
94+
panic("Not implemented.")
95+
}
96+
97+
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
98+
panic("Not implemented.")
99+
}
100+
101+
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) {
102+
isResponse := true
103+
resp := new(DHTMessage)
104+
resp.Id = pmes.Id
105+
resp.Response = &isResponse
106+
107+
mes := new(swarm.Message)
108+
mes.Peer = p
109+
mes.Data = []byte(resp.String())
110+
dht.network.Chan.Outgoing <- mes
111+
}
112+
113+
65114
// Register a handler for a specific message ID, used for getting replies
66115
// to certain messages (i.e. response to a GET_VALUE message)
67116
func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message {
@@ -71,3 +120,9 @@ func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message {
71120
dht.listenLock.Unlock()
72121
return lchan
73122
}
123+
124+
// Stop all communications from this node and shut down
125+
func (dht *IpfsDHT) Halt() {
126+
dht.shutdown <- struct{}{}
127+
dht.network.Close()
128+
}

routing/dht/messages.pb.go

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routing/dht/messages.proto

+5
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ message DHTMessage {
1515
required MessageType type = 1;
1616
optional string key = 2;
1717
optional bytes value = 3;
18+
19+
// Unique ID of this message, used to match queries with responses
1820
required uint64 id = 4;
21+
22+
// Signals whether or not this message is a response to another message
23+
optional bool response = 5;
1924
}

routing/dht/routing.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package dht
22

33
import (
4+
"math/rand"
5+
"time"
6+
47
peer "github.com/jbenet/go-ipfs/peer"
58
swarm "github.com/jbenet/go-ipfs/swarm"
69
u "github.com/jbenet/go-ipfs/util"
7-
"time"
810
)
911

1012
// TODO: determine a way of creating and managing message IDs
1113
func GenerateMessageID() uint64 {
12-
return 4
14+
return uint64(rand.Uint32()) << 32 & uint64(rand.Uint32())
1315
}
1416

1517
// This file implements the Routing interface for the IpfsDHT struct.
@@ -68,9 +70,6 @@ func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
6870
case resp := <-response_chan:
6971
return resp.Data, nil
7072
}
71-
72-
// Should never be hit
73-
return nil, nil
7473
}
7574

7675
// Value provider layer of indirection.

0 commit comments

Comments
 (0)