init distributor dbus

This commit is contained in:
Geno 2021-09-05 19:20:27 +02:00
parent f7ccd08b2e
commit 49e2b06b00
5 changed files with 132 additions and 2 deletions

11
distributor/const.go Normal file
View File

@ -0,0 +1,11 @@
package main
const (
DBUSName = "org.unifiedpush.Distributor.xmpp"
DBUSDistributorPath = "/org/unifiedpush/Distributor"
DBUSDistributorInterface = "org.unifiedpush.Distributor1"
DBUSConnectorPath = "/org/unifiedpush/Connector"
DBUSConnectorInterface = "org.unifiedpush.Connector1"
)

65
distributor/dbus.go Normal file
View File

@ -0,0 +1,65 @@
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
}

View File

@ -1,3 +1,12 @@
module dev.sum7.eu/genofire/unified-push-xmpp/distributor module dev.sum7.eu/genofire/unified-push-xmpp/distributor
go 1.17 go 1.17
require (
github.com/bdlm/log v0.1.20 // indirect
github.com/bdlm/std v1.0.1 // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
)

13
distributor/go.sum Normal file
View File

@ -0,0 +1,13 @@
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/std v1.0.1 h1:USdxays+0tgB3BJCEQ9z942tmTWmzpVPC7jCvczsj/I=
github.com/bdlm/std v1.0.1/go.mod h1:dittT3gnvbHQ4P+1UbkdSwkHFHVl1gx8qYu4zIFyB+Q=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

View File

@ -1,9 +1,41 @@
package main package main
import ( import (
"fmt" "os"
"os/signal"
"syscall"
"github.com/bdlm/log"
"github.com/godbus/dbus/v5"
) )
func main() { func main() {
fmt.Println("startup") conn, err := dbus.ConnectSessionBus()
if err != nil {
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")
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Infof("received %s", sig)
} }