dbus replaced by unifiedpush.org/go/np2p_dbus/distributor
This commit is contained in:
parent
49e2b06b00
commit
ffbc834ada
|
@ -1,11 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
const (
|
|
||||||
DBUSName = "org.unifiedpush.Distributor.xmpp"
|
|
||||||
|
|
||||||
DBUSDistributorPath = "/org/unifiedpush/Distributor"
|
|
||||||
DBUSDistributorInterface = "org.unifiedpush.Distributor1"
|
|
||||||
|
|
||||||
DBUSConnectorPath = "/org/unifiedpush/Connector"
|
|
||||||
DBUSConnectorInterface = "org.unifiedpush.Connector1"
|
|
||||||
)
|
|
|
@ -1,65 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/godbus/dbus/v5"
|
|
||||||
"github.com/bdlm/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type Distributor struct {
|
|
||||||
dbus *dbus.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDistributor(dbus *dbus.Conn) *Distributor {
|
|
||||||
return &Distributor{
|
|
||||||
dbus: dbus,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d Distributor) Register(name, token string) (thing, reason string, err *dbus.Error) {
|
|
||||||
logger:= log.WithFields(map[string]interface{}{
|
|
||||||
"name": name,
|
|
||||||
"token": token,
|
|
||||||
})
|
|
||||||
|
|
||||||
endpoint := "https://up.chat.sum7.eu/UP?appid="+name+"&token="+token
|
|
||||||
|
|
||||||
c := NewConector(d.dbus, name)
|
|
||||||
if err := c.NewEndpoint(token, endpoint); err != nil {
|
|
||||||
logger.Errorf("distributor-register error on NewEndpoint: %v", err)
|
|
||||||
return "REGISTRATION_FAILED", err.Error(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info("distributor-register")
|
|
||||||
return "NEW_ENDPOINT", "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d Distributor) Unregister(token string) *dbus.Error {
|
|
||||||
log.WithFields(map[string]interface{}{
|
|
||||||
"token": token,
|
|
||||||
}).Info("distributor-unregister")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Connector struct {
|
|
||||||
obj dbus.BusObject
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConector(dbus *dbus.Conn,appid string) *Connector {
|
|
||||||
obj := dbus.Object(appid, DBUSConnectorPath)
|
|
||||||
return &Connector{
|
|
||||||
obj: obj,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Connector) Message(token, contents, id string) error {
|
|
||||||
return c.obj.Call(DBUSConnectorInterface+".Message", dbus.FlagNoReplyExpected, token, contents, id).Err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Connector) NewEndpoint(token, endpoint string) error {
|
|
||||||
return c.obj.Call(DBUSConnectorInterface+".NewEndpoint", dbus.FlagNoReplyExpected, token, endpoint).Err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Connector) Unregistered(token string) error {
|
|
||||||
return c.obj.Call(DBUSConnectorInterface+".Unregistered", dbus.FlagNoReplyExpected, token).Err
|
|
||||||
}
|
|
|
@ -3,9 +3,14 @@ module dev.sum7.eu/genofire/unified-push-xmpp/distributor
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bdlm/log v0.1.20 // indirect
|
github.com/bdlm/log v0.1.20
|
||||||
|
unifiedpush.org/go/np2p_dbus v0.0.0-20210906135733-42e59cd82376
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
github.com/bdlm/std v1.0.1 // indirect
|
github.com/bdlm/std v1.0.1 // indirect
|
||||||
github.com/godbus/dbus/v5 v5.0.4 // indirect
|
github.com/godbus/dbus/v5 v5.0.4 // indirect
|
||||||
|
github.com/stretchr/testify v1.7.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
|
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
|
||||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
|
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
|
||||||
|
|
|
@ -2,12 +2,40 @@ github.com/bdlm/log v0.1.20 h1:fSxBuBSHz+DkxPSFlaVcPiep20mCYUJZ5azUynkjhfA=
|
||||||
github.com/bdlm/log v0.1.20/go.mod h1:30V5Zwc5Vt5ePq5rd9KJ6JQ/A5aFUcKzq5fYtO7c9qc=
|
github.com/bdlm/log v0.1.20/go.mod h1:30V5Zwc5Vt5ePq5rd9KJ6JQ/A5aFUcKzq5fYtO7c9qc=
|
||||||
github.com/bdlm/std v1.0.1 h1:USdxays+0tgB3BJCEQ9z942tmTWmzpVPC7jCvczsj/I=
|
github.com/bdlm/std v1.0.1 h1:USdxays+0tgB3BJCEQ9z942tmTWmzpVPC7jCvczsj/I=
|
||||||
github.com/bdlm/std v1.0.1/go.mod h1:dittT3gnvbHQ4P+1UbkdSwkHFHVl1gx8qYu4zIFyB+Q=
|
github.com/bdlm/std v1.0.1/go.mod h1:dittT3gnvbHQ4P+1UbkdSwkHFHVl1gx8qYu4zIFyB+Q=
|
||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
|
||||||
|
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
|
||||||
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
|
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
|
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
|
||||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg=
|
golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg=
|
||||||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
||||||
|
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
|
gorm.io/gorm v1.21.14/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
|
||||||
|
unifiedpush.org/go/np2p_dbus v0.0.0-20210906135733-42e59cd82376 h1:fmedHMlfVgVnIbVHux48A5dtylXRIHfD9xiScfx7Z4Y=
|
||||||
|
unifiedpush.org/go/np2p_dbus v0.0.0-20210906135733-42e59cd82376/go.mod h1:sNyBXQNpWeAlLUUGwWJWWIsOjXfl8DIWhSHst8pQVPM=
|
||||||
|
|
|
@ -4,32 +4,17 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/bdlm/log"
|
"github.com/bdlm/log"
|
||||||
"github.com/godbus/dbus/v5"
|
"unifiedpush.org/go/np2p_dbus/distributor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dbus *distributor.DBus
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conn, err := dbus.ConnectSessionBus()
|
dbus = distributor.NewDBus("org.unifiedpush.Distributor.xmpp")
|
||||||
if err != nil {
|
dbus.StartHandling(handler{})
|
||||||
log.Panicf("on dbus connection: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
log.Debug("connect to dbus")
|
|
||||||
|
|
||||||
rp, err := conn.RequestName(DBUSName, dbus.NameFlagReplaceExisting)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("register name on dbus: %v", err)
|
|
||||||
}
|
|
||||||
if rp != dbus.RequestNameReplyPrimaryOwner {
|
|
||||||
log.Panicf("a other dbus service is running with %s: %v", DBUSName, rp)
|
|
||||||
}
|
|
||||||
log.Debug("register name to dbus")
|
|
||||||
|
|
||||||
d := NewDistributor(conn)
|
|
||||||
if err := conn.ExportAll(d, DBUSDistributorPath, DBUSDistributorInterface); err != nil {
|
|
||||||
log.Panicf("export distributor on %s: %v", DBUSDistributorPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("startup")
|
log.Info("startup")
|
||||||
|
|
||||||
|
@ -39,3 +24,25 @@ func main() {
|
||||||
sig := <-sigs
|
sig := <-sigs
|
||||||
log.Infof("received %s", sig)
|
log.Infof("received %s", sig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type handler struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h handler) Register(appName, token string) (string,string,error) {
|
||||||
|
log.WithFields(map[string]interface{}{
|
||||||
|
"name": appName,
|
||||||
|
"token": token,
|
||||||
|
}).Info("distributor-register")
|
||||||
|
endpoint := "https://up.chat.sum7.eu/UP?appid="+appName+"&token="+token
|
||||||
|
if endpoint != "" {
|
||||||
|
return endpoint, "", nil
|
||||||
|
}
|
||||||
|
return "", "reason to app", errors.New("Unknown error")
|
||||||
|
}
|
||||||
|
func (h handler) Unregister(token string) {
|
||||||
|
log.WithFields(map[string]interface{}{
|
||||||
|
"token": token,
|
||||||
|
}).Info("distributor-unregister")
|
||||||
|
appID := ""
|
||||||
|
_ = dbus.NewConnector(appID).Unregistered(token)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue