-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add online * update NewDHT * update node
- Loading branch information
Showing
4 changed files
with
251 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
Copyright (C) CESS. All rights reserved. | ||
Copyright (C) Cumulus Encrypted Storage System. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package core | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"sync" | ||
"time" | ||
|
||
"github.com/CESSProject/p2p-go/pb" | ||
|
||
"github.com/google/uuid" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/protocol" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// pattern: /protocol-name/request-or-response-message/version | ||
const OnlineRequest = "/online/req/v0" | ||
const OnlineResponse = "/online/resp/v0" | ||
|
||
type onlineResp struct { | ||
ch chan bool | ||
*pb.MessageData | ||
} | ||
|
||
type OnlineProtocol struct { // local host | ||
*PeerNode | ||
*sync.Mutex | ||
requests map[string]*onlineResp // determine whether it is your own response | ||
} | ||
|
||
func (n *PeerNode) NewOnlineProtocol() *OnlineProtocol { | ||
e := OnlineProtocol{PeerNode: n, Mutex: new(sync.Mutex), requests: make(map[string]*onlineResp)} | ||
n.SetStreamHandler(protocol.ID(n.protocolPrefix+OnlineResponse), e.onOnlineResponse) | ||
return &e | ||
} | ||
|
||
func (e *protocols) OnlineAction(id peer.ID) error { | ||
var err error | ||
var ok bool | ||
// create message data | ||
req := &pb.MessageData{ | ||
Id: uuid.New().String(), | ||
NodeId: e.OnlineProtocol.ID().String(), | ||
} | ||
|
||
// store request so response handler has access to it | ||
respChan := make(chan bool, 1) | ||
|
||
e.OnlineProtocol.Lock() | ||
for { | ||
if _, ok := e.OnlineProtocol.requests[req.Id]; ok { | ||
req.Id = uuid.New().String() | ||
continue | ||
} | ||
e.OnlineProtocol.requests[req.Id] = &onlineResp{ | ||
ch: respChan, | ||
MessageData: &pb.MessageData{ | ||
Id: req.Id, | ||
}, | ||
} | ||
break | ||
} | ||
e.OnlineProtocol.Unlock() | ||
|
||
defer func() { | ||
e.OnlineProtocol.Lock() | ||
delete(e.OnlineProtocol.requests, req.Id) | ||
close(respChan) | ||
e.OnlineProtocol.Unlock() | ||
}() | ||
|
||
timeout := time.NewTicker(P2PWriteReqRespTime) | ||
defer timeout.Stop() | ||
|
||
err = e.OnlineProtocol.SendProtoMessage(id, protocol.ID(e.ProtocolPrefix+OnlineRequest), req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// wait response | ||
timeout.Reset(P2PWriteReqRespTime) | ||
select { | ||
case ok = <-respChan: | ||
if !ok { | ||
return errors.New(ERR_RespFailure) | ||
} | ||
return nil | ||
case <-timeout.C: | ||
return errors.New(ERR_RespTimeOut) | ||
} | ||
} | ||
|
||
// remote peer response handler | ||
func (e *OnlineProtocol) onOnlineResponse(s network.Stream) { | ||
defer s.Close() | ||
|
||
data := &pb.MessageData{} | ||
buf, err := io.ReadAll(s) | ||
if err != nil { | ||
s.Reset() | ||
return | ||
} | ||
|
||
// unmarshal it | ||
err = proto.Unmarshal(buf, data) | ||
if err != nil { | ||
s.Reset() | ||
return | ||
} | ||
|
||
if data.NodeId == "" { | ||
s.Reset() | ||
return | ||
} | ||
|
||
// locate request data and remove it if found | ||
e.OnlineProtocol.Lock() | ||
defer e.OnlineProtocol.Unlock() | ||
|
||
_, ok := e.requests[data.Id] | ||
if ok { | ||
e.requests[data.Id].ch <- true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright (C) CESS. All rights reserved. | ||
Copyright (C) Cumulus Encrypted Storage System. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
p2pgo "github.com/CESSProject/p2p-go" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
sourcePort1 := flag.Int("p1", 15000, "Source port number") | ||
// To construct a simple host with all the default settings, just use `New` | ||
h1, err := p2pgo.New( | ||
ctx, | ||
p2pgo.PrivatekeyFile(".private1"), | ||
p2pgo.ListenPort(*sourcePort1), // regular tcp connections | ||
p2pgo.Workspace("."), | ||
p2pgo.BootPeers([]string{"_dnsaddr.boot-bucket-devnet.cess.cloud"}), | ||
) | ||
if err != nil { | ||
log.Println("[p2pgo.New]", err) | ||
return | ||
} | ||
defer h1.Close() | ||
|
||
log.Println("node1:", h1.Addrs(), h1.ID()) | ||
|
||
remote := "/ip4/127.0.0.1/tcp/8001/p2p/12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S" | ||
|
||
maddr, err := ma.NewMultiaddr(remote) | ||
if err != nil { | ||
log.Println("[ma.NewMultiaddr]", err) | ||
return | ||
} | ||
// Extract the peer ID from the multiaddr. | ||
info, err := peer.AddrInfoFromP2pAddr(maddr) | ||
if err != nil { | ||
log.Println("[peer.AddrInfoFromP2pAddr]", err) | ||
return | ||
} | ||
h1.Peerstore().AddAddr(info.ID, maddr, time.Hour) | ||
|
||
err = h1.OnlineAction(info.ID) | ||
fmt.Println(err) | ||
} |