Commit 1294aa56 authored by Jeromy's avatar Jeromy Committed by Juan Batiz-Benet

making connections between nodes get closer to working

parent 41f5c5c5
...@@ -55,6 +55,10 @@ func NewDHT(p *peer.Peer) (*IpfsDHT, error) { ...@@ -55,6 +55,10 @@ func NewDHT(p *peer.Peer) (*IpfsDHT, error) {
return dht, nil return dht, nil
} }
func (dht *IpfsDHT) Start() {
go dht.handleMessages()
}
// Connect to a new peer at the given address // Connect to a new peer at the given address
func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error { func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error {
peer := new(peer.Peer) peer := new(peer.Peer)
...@@ -65,24 +69,26 @@ func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error { ...@@ -65,24 +69,26 @@ func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error {
return err return err
} }
err = identify.Handshake(dht.self, conn) err = identify.Handshake(dht.self, peer, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
if err != nil { if err != nil {
return err return err
} }
dht.network.StartConn(conn.Peer.Key(), conn) dht.network.StartConn(conn)
// TODO: Add this peer to our routing table // TODO: Add this peer to our routing table
return nil return nil
} }
// Read in all messages from swarm and handle them appropriately // Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch // NOTE: this function is just a quick sketch
func (dht *IpfsDHT) handleMessages() { func (dht *IpfsDHT) handleMessages() {
u.DOut("Being message handling routine")
for { for {
select { select {
case mes := <-dht.network.Chan.Incoming: case mes := <-dht.network.Chan.Incoming:
u.DOut("recieved message from swarm.")
pmes := new(DHTMessage) pmes := new(DHTMessage)
err := proto.Unmarshal(mes.Data, pmes) err := proto.Unmarshal(mes.Data, pmes)
if err != nil { if err != nil {
...@@ -118,6 +124,8 @@ func (dht *IpfsDHT) handleMessages() { ...@@ -118,6 +124,8 @@ func (dht *IpfsDHT) handleMessages() {
dht.handleFindNode(mes.Peer, pmes) dht.handleFindNode(mes.Peer, pmes)
} }
case err := <-dht.network.Chan.Errors:
panic(err)
case <-dht.shutdown: case <-dht.shutdown:
return return
} }
...@@ -158,10 +166,6 @@ func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) { ...@@ -158,10 +166,6 @@ func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) {
} }
} }
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
panic("Not implemented.")
}
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) { func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) {
isResponse := true isResponse := true
resp := new(DHTMessage) resp := new(DHTMessage)
...@@ -172,6 +176,18 @@ func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) { ...@@ -172,6 +176,18 @@ func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) {
dht.network.Chan.Outgoing <-swarm.NewMessage(p, []byte(resp.String())) dht.network.Chan.Outgoing <-swarm.NewMessage(p, []byte(resp.String()))
} }
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
panic("Not implemented.")
}
func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *DHTMessage) {
panic("Not implemented.")
}
func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *DHTMessage) {
panic("Not implemented.")
}
// Register a handler for a specific message ID, used for getting replies // Register a handler for a specific message ID, used for getting replies
// to certain messages (i.e. response to a GET_VALUE message) // to certain messages (i.e. response to a GET_VALUE message)
...@@ -202,6 +218,8 @@ func (dht *IpfsDHT) Halt() { ...@@ -202,6 +218,8 @@ func (dht *IpfsDHT) Halt() {
// Ping a node, log the time it took // Ping a node, log the time it took
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) { func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) {
// Thoughts: maybe this should accept an ID and do a peer lookup? // Thoughts: maybe this should accept an ID and do a peer lookup?
u.DOut("Enter Ping.")
id := GenerateMessageID() id := GenerateMessageID()
mes_type := DHTMessage_PING mes_type := DHTMessage_PING
pmes := new(DHTMessage) pmes := new(DHTMessage)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment