src.dualinventive.com/go/devsim/transport/dinet.go

71 lines
1.5 KiB
Go

package transport
import (
"errors"
"src.dualinventive.com/go/devsim/simulator"
"src.dualinventive.com/go/dinet"
"src.dualinventive.com/go/dinet/rpc"
)
// Dinet is a dinet connection that satisfy the simulator transport
type Dinet struct {
conn dinet.DeviceConn
deviceUID string
destination string
}
var _ simulator.Transport = &Dinet{}
// NewDinet create a dinet/conn connection
func NewDinet(enc dinet.Transport, deviceUID, destination string) (*Dinet, error) {
conn, err := dinet.NewDeviceConn(enc)
if err != nil {
return nil, err
}
return &Dinet{
conn: conn,
deviceUID: deviceUID,
destination: destination,
}, nil
}
// Connect enstablish the connection to the secure multi proxy
func (d *Dinet) Connect() error {
if err := d.conn.Connect(d.destination); err != nil {
return err
}
if err := d.conn.DeviceHandshake(d.deviceUID); err != nil {
return err
}
return nil
}
// Disconnect closes the connection to the secure multi proxy
func (d *Dinet) Disconnect() error {
return d.conn.Close()
}
// Close closes the underlaying connection
func (d *Dinet) Close() error {
return d.conn.Close()
}
// Recv waits until it gets a rpc message from the tcp socket and decodes the message
func (d *Dinet) Recv() (*rpc.Msg, error) {
return d.conn.Recv()
}
// Send encodes a rpc message and sends it over the tcp socket
func (d *Dinet) Send(m *rpc.Msg) error {
m.DeviceUID = d.deviceUID
return d.conn.Send(m)
}
// SendBytes is not supported
func (d *Dinet) SendBytes(data []byte) error {
return errors.New("not supported")
}