Extract CreateFederationClient into demo

This commit is contained in:
Hilmar Gústafsson 2020-04-10 12:02:28 +02:00
parent 1de97144bf
commit 0aec6318c4
2 changed files with 20 additions and 19 deletions

View file

@ -24,6 +24,7 @@ import (
"time" "time"
gostream "github.com/libp2p/go-libp2p-gostream" gostream "github.com/libp2p/go-libp2p-gostream"
p2phttp "github.com/libp2p/go-libp2p-http"
p2pdisc "github.com/libp2p/go-libp2p/p2p/discovery" p2pdisc "github.com/libp2p/go-libp2p/p2p/discovery"
"github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/appservice"
"github.com/matrix-org/dendrite/clientapi" "github.com/matrix-org/dendrite/clientapi"
@ -78,6 +79,21 @@ func createKeyDB(
return db return db
} }
func createFederationClient(
base *basecomponent.BaseDendrite,
) *gomatrixserverlib.FederationClient {
fmt.Println("Running in libp2p federation mode")
fmt.Println("Warning: Federation with non-libp2p homeservers will not work in this mode yet!")
tr := &http.Transport{}
tr.RegisterProtocol(
"matrix",
p2phttp.NewTransport(base.LibP2P, p2phttp.ProtocolOption("/matrix")),
)
return gomatrixserverlib.NewFederationClientWithTransport(
base.Cfg.Matrix.ServerName, base.Cfg.Matrix.KeyID, base.Cfg.Matrix.PrivateKey, tr,
)
}
func main() { func main() {
instanceName := flag.String("name", "dendrite-p2p", "the name of this P2P demo instance") instanceName := flag.String("name", "dendrite-p2p", "the name of this P2P demo instance")
instancePort := flag.Int("port", 8080, "the port that the client API will listen on") instancePort := flag.Int("port", 8080, "the port that the client API will listen on")
@ -128,7 +144,7 @@ func main() {
accountDB := base.CreateAccountsDB() accountDB := base.CreateAccountsDB()
deviceDB := base.CreateDeviceDB() deviceDB := base.CreateDeviceDB()
keyDB := createKeyDB(base) keyDB := createKeyDB(base)
federation := base.CreateFederationClient() federation := createFederationClient(base)
keyRing := keydb.CreateKeyRing(federation.Client, keyDB) keyRing := keydb.CreateKeyRing(federation.Client, keyDB)
alias, input, query := roomserver.SetupRoomServerComponent(base) alias, input, query := roomserver.SetupRoomServerComponent(base)

View file

@ -30,7 +30,6 @@ import (
routing "github.com/libp2p/go-libp2p-core/routing" routing "github.com/libp2p/go-libp2p-core/routing"
host "github.com/libp2p/go-libp2p-core/host" host "github.com/libp2p/go-libp2p-core/host"
p2phttp "github.com/libp2p/go-libp2p-http"
dht "github.com/libp2p/go-libp2p-kad-dht" dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub" pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/common/keydb"
@ -259,23 +258,9 @@ func (b *BaseDendrite) CreateKeyDB() keydb.Database {
// CreateFederationClient creates a new federation client. Should only be called // CreateFederationClient creates a new federation client. Should only be called
// once per component. // once per component.
func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient { func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
if b.LibP2P != nil { return gomatrixserverlib.NewFederationClient(
fmt.Println("Running in libp2p federation mode") b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey,
fmt.Println("Warning: Federation with non-libp2p homeservers will not work in this mode yet!") )
tr := &http.Transport{}
tr.RegisterProtocol(
"matrix",
p2phttp.NewTransport(b.LibP2P, p2phttp.ProtocolOption("/matrix")),
)
return gomatrixserverlib.NewFederationClientWithTransport(
b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey, tr,
)
} else {
fmt.Println("Running in regular federation mode")
return gomatrixserverlib.NewFederationClient(
b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey,
)
}
} }
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on // SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on