This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dione.go
192 lines (151 loc) · 4.31 KB
/
dione.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package dione
import (
ctx "context"
"fmt"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
dht "github.com/libp2p/go-libp2p-kad-dht"
"io"
)
type DioneHost struct {
internalHost host.Host
internalDht *dht.IpfsDHT
internalInputs chan<- GeneralCommand
}
type DioneTunnel struct {
connection io.ReadWriteCloser
}
type DioneInterface interface {
Closest(key string) []peer.ID
Put(key string, value []byte)
Get(key string) []byte
Connect(p peer.ID) *DioneTunnel
}
func NewDioneHost(port int) DioneHost {
h, DHT := NewHost(port)
mdnsFound := initMdns(h, "dione")
go connectMdns(h, DHT, mdnsFound)
dhtHan, inputs := newDhtHandler(DHT)
go dhtHan.handle()
handler := &Handler{}
handler.h = h
handler.dhtInput = inputs
go h.SetStreamHandler(requestId, handler.handleStream)
result := DioneHost{}
result.internalHost = h
result.internalDht = DHT
result.internalInputs = inputs
return result
}
func (d DioneHost) GetPeers() []peer.ID {
return d.internalHost.Network().Peers()
}
func (dh DioneHost) Closest(key string) []peer.ID {
resp := make(chan []peer.ID, 1)
command := new(GeneralCommand)
closestCommand := new(ClosestCommand)
closestCommand.key = key
closestCommand.response = resp
command.closest = closestCommand
dh.internalInputs <- *command
closestPeers := <-resp
return closestPeers
}
func (dh DioneHost) Put(key string, value []byte) {
generalCommand := new(GeneralCommand)
put := newPutCommand(key, value)
generalCommand.put = &put
dh.internalInputs <- *generalCommand
}
func (dh DioneHost) Get(key string) []byte {
generalCommand := new(GeneralCommand)
get, response := newGetCommand(key)
generalCommand.get = &get
dh.internalInputs <- *generalCommand
valueData := <-response
return valueData
}
func (dh DioneHost) Connect(p peer.ID) *DioneTunnel {
peers := dh.GetPeers()
var streampeer peer.ID
for _, ip := range peers {
if ip != p {
streampeer = ip
break
}
}
stream, err := dh.internalHost.NewStream(ctx.TODO(), streampeer, requestId)
if err != nil {
panic(err)
}
tun := newTunnel(stream, p)
ret := new(DioneTunnel)
ret.connection = tun
return ret
}
func (d *DioneTunnel) Closest(key string) []peer.ID {
generalRequest := new(GeneralRequest)
generalClosestRequest := new(GeneralRequest_ClosestProviderRequest)
closestRequest := new(ClosestProviderRequest)
closestRequest.Key = key
generalClosestRequest.ClosestProviderRequest = closestRequest
generalRequest.GeneralRequestKind = generalClosestRequest
handler := &Handler{}
handler.writeMessage(d.connection, generalRequest)
generalResponse := new(GeneralResponse)
err := handler.readMessage(d.connection, generalResponse)
if err != nil {
panic(err)
}
providers := generalResponse.GetClosestProviderResponse().Provider
peers := make([]peer.ID, 0)
for _, provider := range providers {
id, err := peer.Decode(provider)
if err != nil {
panic(err)
}
peers = append(peers, id)
}
return peers
}
func (d *DioneTunnel) Put(key string, value []byte) {
generalRequest := new(GeneralRequest)
generalPutRequest := new(GeneralRequest_PutKadRequest)
putRequest := new(PutKadRequest)
putRequest.Key = key
putRequest.Value = value
generalPutRequest.PutKadRequest = putRequest
generalRequest.GeneralRequestKind = generalPutRequest
handle := &Handler{}
handle.writeMessage(d.connection, generalRequest)
generalResponse := new(GeneralResponse)
err := handle.readMessage(d.connection, generalResponse)
if err != nil {
panic(err)
}
putKadResponse := generalResponse.GetPutKadResponse()
fmt.Printf("Response put kad %v\n", putKadResponse.Status)
}
func (d *DioneTunnel) Get(key string) []byte {
generalRequest := new(GeneralRequest)
generalGetRequest := new(GeneralRequest_GetKadRequest)
getRequest := new(GetKadRequest)
getRequest.Key = key
generalGetRequest.GetKadRequest = getRequest
generalRequest.GeneralRequestKind = generalGetRequest
handler := &Handler{}
handler.writeMessage(d.connection, generalRequest)
generalResponse := new(GeneralResponse)
err := handler.readMessage(d.connection, generalResponse)
if err != nil {
panic(err)
}
getResponse := generalResponse.GetGetKadResponse()
return getResponse.Value
}
func (d *DioneTunnel) Connect(peer peer.ID) *DioneTunnel {
tun := newTunnel(d.connection, peer)
nt := new(DioneTunnel)
nt.connection = tun
return nt
}