Browse Source

Ganz neuer Ansatz: Ein Objekt für alle Fälle

Sackgasse,-später-evtl.-weitermachen
Klaus Wendel 2 years ago
parent
commit
277e3b4f0d
  1. 81
      nhooyrioWebsocketAddons/doc.go
  2. 193
      nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

81
nhooyrioWebsocketAddons/doc.go

@ -1,85 +1,30 @@
package nhooyrioWebsocketAddons
//Funktionierender Mustercode
/* //Alt: Ohne Pointer
package main
/*
import (
"encoding/json"
"fmt"
"zmpTmp/nh"
)
var buffer nh.GosmAdapter
var container nh.GosmAdapter
func main() {
container = &nh.GosmContainer{nh.GosmContainerSection{"A", "BB", "CCC", true, "DDDD"}, nh.GosmContainerSection{"D", "EE", "FFF", false, "GGGG"}}
buffer, _ = container.Convert()
fmt.Printf("%T\n", buffer)
//buffer.Revalue()
fmt.Println(buffer)
newContainer, _ := buffer.Convert()
//newContainer.Revalue()
fmt.Printf("%T\n", newContainer)
fmt.Println(newContainer)
j, err := json.Marshal(container)
if err != nil {
panic(err)
}
fmt.Println(j)
}
*/
/* //Neu: Mit Pointer
package main
import (
"encoding/json"
"fmt"
//"zmpTmp/nh"
nh "git.archium.org/archium_public/ebkTools/nhooyrioWebsocketAddons"
)
var buffer nh.GosmAdapter
var container nh.GosmAdapter
var xronta nh.GosmAdapter
func main() {
container = &nh.GosmContainer{nh.GosmContainerSection{"A", "BB", "CCC", true, "DDDD"}, nh.GosmContainerSection{"D", "EE", "FFF", false, "GGGG"}}
fmt.Println(container)
xronta = &nh.GosmContainer{CarrierJson: nh.CarrierJson{"A", "BB", "CCC", "DDDD"}, Status: nh.Upstream}
fmt.Println(xronta)
xronta.ConvertBuffer2Json()
xronta.(*nh.GosmContainer).Status = nh.Downstream
container.Revalue(func(interface{}) interface{} {
return nh.GosmContainer{nh.GosmContainerSection{"Aneu", "BBneu", "CCCneu", true, "DDDDneu"}, nh.GosmContainerSection{"Dneu", "EEneu", "FFFneu", false, "GGGGneu"}}
})
buffer, _ = container.Convert()
fmt.Printf("%T\n", buffer)
puffer := buffer.(*nh.GosmBuffer)
fmt.Println(*puffer)
newContainer, _ := buffer.Convert()
fmt.Printf("%T\n", newContainer)
fmt.Println(newContainer)
fmt.Println(container)
j, err := json.Marshal(container)
if err != nil {
panic(err)
}
fmt.Println(j)
fmt.Println(xronta)
xronta.ConvertJson2Buffer()
xronta.(*nh.GosmContainer).CarrierJson = nh.CarrierJson{"Aa", "BBb", "CCCc", "DDDDd"}
xronta.ConvertJson2Buffer()
fmt.Println(xronta)
xronta.ConvertBuffer2Json()
fmt.Println(xronta)
}
*/

193
nhooyrioWebsocketAddons/nhooyrioWebsocketAddons.go

@ -3,42 +3,129 @@ package nhooyrioWebsocketAddons
import (
"context"
"encoding/json"
"fmt"
"errors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
// GosmContainerSection is the type for one single information segment
type GosmContainerSection struct {
type CarrierJson struct {
TagID string `json:"tagid"`
Attribute string `json:"attribute"` // might left empty
Content string `json:"content"`
Upstream bool `json:"streamdirection"` // up = true / down = false
Status string `json:"status"` // reserved for status message; type errors is out of bounds
Status string `json:"status"` // reserved for status message; type errors is out of bounds
}
// GosmContainer is an accumulation of information segments
type GosmContainer []GosmContainerSection
type GosmStatus string
// GosmBuffer is the GosmContainer as byte-array
type GosmBuffer []byte
const (
Upstream GosmStatus = "->"
Downstream GosmStatus = "<-"
Silent GosmStatus = "--"
)
type WsCommunicationMethod string
const (
SendText WsCommunicationMethod = "sendtext"
SendBinary WsCommunicationMethod = "sendbinary"
SendJson WsCommunicationMethod = "sendjson"
ReceiveText WsCommunicationMethod = "receivetext"
ReceiveBinary WsCommunicationMethod = "receivebinary"
ReceiveJson WsCommunicationMethod = "receivejson"
)
type GosmContainer struct {
CarrierJson
CarrierBuffer []byte
Status GosmStatus
Error error
Ctx context.Context
Conn *websocket.Conn
}
//GosmAdapter is the master type for sending and receiving data between websocket client and server
type GosmAdapter interface {
RevalueJson(func(CarrierJson) CarrierJson)
ConvertJson2Buffer() error
ConvertBuffer2Json() error
//WsServerAccept
//WsServerClose
//WsClientDial
WsContainerSend(method WsCommunicationMethod) error
WsContainerReceive(method WsCommunicationMethod, dosth func(interface{}) interface{}) (error, websocket.MessageType)
}
func (r *GosmContainer) RevalueJson(dosth func(CarrierJson) CarrierJson) {
(*r).CarrierJson = dosth((*r).CarrierJson)
}
func (r *GosmContainer) ConvertJson2Buffer() (err error) {
(*r).CarrierBuffer, err = json.Marshal((*r).CarrierJson)
return
}
func (r *GosmContainer) ConvertBuffer2Json() (err error) {
var tmp CarrierJson
err = json.Unmarshal((*r).CarrierBuffer, &tmp)
(*r).CarrierJson = tmp
//err = json.Unmarshal((*r).CarrierBuffer, &((*r).CarrierJson))
return
}
func (r *GosmContainer) WsContainerSend(method WsCommunicationMethod) (err error) {
switch method {
case SendJson:
var tmpreceiveval interface{}
err, tmpreceiveval = WsjsonSend((*r).Ctx, (*r).Conn, (*r).CarrierJson)
(*r).CarrierJson = tmpreceiveval.(CarrierJson)
case SendText:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageText)
case SendBinary:
err, (*r).CarrierBuffer = WsSend((*r).Ctx, (*r).Conn, (*r).CarrierBuffer, websocket.MessageBinary)
default:
err = errors.New("Wrong communicator method")
}
return
}
func (r *GosmContainer) WsContainerReceive(method WsCommunicationMethod, dosth func(interface{}) interface{}) (err error, mtr websocket.MessageType) {
/*
dosthI := func(i interface{}) (o interface{}) {
o = dosth(i.(CarrierJson))
return
}
*/
switch method {
case ReceiveText:
err, (*r).CarrierBuffer, mtr = WsReceive((*r).Ctx, (*r).Conn, dosth)
return err, mtr
case ReceiveBinary:
err, (*r).CarrierBuffer, mtr = WsReceive((*r).Ctx, (*r).Conn, dosth)
return err, mtr
case ReceiveJson:
var tmpreceiveval interface{}
err, tmpreceiveval = WsjsonReceive((*r).Ctx, (*r).Conn, dosth)
(*r).CarrierJson = tmpreceiveval.(CarrierJson)
return err, websocket.MessageText
default:
err = errors.New("Wrong communicator method")
}
return
}
/*
type GosmAtapder interface {
Revalue(func(interface{}) interface{})
Convert() (GosmAdapter, error)
Convert() (GosmAtapder, error)
//WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType)
WsCommunicator(string, context.Context, *websocket.Conn, ...func(interface{}) interface{}) (error, websocket.MessageType)
}
//Convert method turns GosmContainer-Data to GosmBuffer and reverse
func (r *GosmContainer) Convert() (GosmAdapter, error) {
func (r *GosmContratainer) Convert() (GosmAtapder, error) {
buffer, err := json.Marshal(*r)
/* LÖSCHEN
fmt.Println("Hier", *r)
*r = GosmContainer{GosmContainerSection{"E", "FF", "GGG", true, "Hallo"}}
fmt.Println("Hier2", *r)*/
if err != nil {
panic(err)
}
@ -46,85 +133,40 @@ func (r *GosmContainer) Convert() (GosmAdapter, error) {
return &pbuffer, err
}
//Convert method turns GosmContainer-Data to GosmBuffer and reverse
func (r *GosmBuffer) Convert() (GosmAdapter, error) {
var gosm GosmContainer
func (r *GosmBuffer) Convert() (GosmAtapder, error) {
var gosm GosmContratainer
err := json.Unmarshal(*r, &gosm)
return &gosm, err
}
//Revalue alters data
func (r *GosmContainer) Revalue(dosth func(interface{}) interface{}) {
*r = dosth(*r).(GosmContainer)
func (r *GosmContratainer) Revalue(dosth func(interface{}) interface{}) {
*r = dosth(*r).(GosmContratainer)
}
//Revalue alters data
func (r *GosmBuffer) Revalue(dosth func(interface{}) interface{}) {
//*r = dosth(*r).([]byte)
*r = dosth(*r).(GosmBuffer)
}
/*
func (r *GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType) {
func (r *GosmContratainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "sendbinary" || method == "send") && len(dosth) == 0:
err, receiveval := WsjsonSend(ctx, conn, *r)
return receiveval.(GosmAdapter), err, websocket.MessageText
*r = receiveval.(GosmContratainer)
return err, websocket.MessageText
case method == "receive" && len(dosth) == 1:
err, receiveval := WsjsonReceive(ctx, conn, dosth[0])
return receiveval.(GosmAdapter), err, websocket.MessageText
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
func (r *GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (GosmAdapter, error, websocket.MessageType) {
switch {
case (method == "sendtext" || method == "send") && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageText)
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, websocket.MessageText
case method == "sendbinary" && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageBinary)
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, websocket.MessageBinary
case method == "receive" && len(dosth) == 1:
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0])
pbuffer := GosmBuffer(receiveval)
return &pbuffer, err, mtr
*r = receiveval.(GosmContratainer)
return err, websocket.MessageText
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
}
*/
//WsCommunicator sends and receivs data to/from websocket partner
func (r *GosmContainer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (error, websocket.MessageType) {
fmt.Println("ICH BIN HIER UND HIER GEHT WAS SCHIEF 0")
/*
switch {
case (method == "sendtext" || method == "sendbinary" || method == "send") && len(dosth) == 0:
err, receiveval := WsjsonSend(ctx, conn, *r)
*r = receiveval.(GosmContainer)
return err, websocket.MessageText
case method == "receive" && len(dosth) == 1:
fmt.Print("ICH BIN HIER UND HIER GEHT WAS SCHIEF 1")
err, receiveval := WsjsonReceive(ctx, conn, dosth[0])
fmt.Print("ICH BIN HIER UND HIER GEHT WAS SCHIEF 2")
//*r = receiveval.(GosmContainer)
_ = receiveval
return err, websocket.MessageText
default:
panic("Wrong communicator method or invalid number of dosth-functions")
}
*/
}
//WsCommunicator sends and receivs data to/from websocket partner
func (r *GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *websocket.Conn, dosth ...func(interface{}) interface{}) (error, websocket.MessageType) {
fmt.Print("ICH BIN HIER UND HIER GEHT WAS SCHIEF 10")
switch {
case (method == "sendtext" || method == "send") && len(dosth) == 0:
err, receiveval := WsSend(ctx, conn, *r, websocket.MessageText)
@ -135,7 +177,7 @@ func (r *GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *we
*r = GosmBuffer(receiveval)
return err, websocket.MessageBinary
case method == "receive" && len(dosth) == 1:
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0]) //
err, receiveval, mtr := WsReceive(ctx, conn, dosth[0])
*r = GosmBuffer(receiveval)
return err, mtr
default:
@ -143,8 +185,8 @@ func (r *GosmBuffer) WsCommunicator(method string, ctx context.Context, conn *we
}
}
*/
//WsjsonSend sends data to websocket partner in json text format
func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{}) (err error, receiveval interface{}) {
err = nil
@ -163,7 +205,6 @@ func WsjsonSend(ctx context.Context, conn *websocket.Conn, sendval interface{})
return
}
//WsjsonReceive receives data from websocket partner in json text format
func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval interface{}) {
var sendval interface{}
err = nil
@ -188,7 +229,6 @@ func WsjsonReceive(ctx context.Context, conn *websocket.Conn, dosth func(interfa
return
}
//WsSend sends data to websocket partner as byte array
func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts websocket.MessageType) (err error, receiveval []byte) {
var mtr websocket.MessageType
err = nil
@ -212,7 +252,6 @@ func WsSend(ctx context.Context, conn *websocket.Conn, sendval []byte, mts webso
return
}
//WsReceive receives data from websocket partner as byte array
func WsReceive(ctx context.Context, conn *websocket.Conn, dosth func(interface{}) interface{}) (err error, receiveval []byte, mtr websocket.MessageType) {
var sendval []byte
err = nil

Loading…
Cancel
Save