Skip to content

Commit

Permalink
BREAKING CHANGE: To the Transport API. The "method" argument to Trans…
Browse files Browse the repository at this point in the history
…port.RPC has been changed to an enum from a string.

To update your code, all action strings like "Pickup" become api.Pickup.  "AddPeer" becomes api.AddPeer and so on.

I apologize for the breakage, but I think everyone will agree that this is worth it.  Less strings in the binary and now
you can autocomplete action names in your favorite IDE without typos or guessing.  All known transports have been updated.
  • Loading branch information
awgh committed Jan 15, 2021
1 parent e108e3d commit 3f160c0
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 228 deletions.
139 changes: 139 additions & 0 deletions api/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package api

// Action - API Call ID numbers
type Action uint16

const (
Null Action = 0
ID Action = 1
Dropoff Action = 2
Pickup Action = 3
CID Action = 16
GetContact Action = 17
GetContacts Action = 18
AddContact Action = 19
DeleteContact Action = 20
GetChannel Action = 21
GetChannels Action = 22
AddChannel Action = 23
DeleteChannel Action = 24
GetProfile Action = 25
GetProfiles Action = 26
AddProfile Action = 27
DeleteProfile Action = 28
LoadProfile Action = 29
GetPeer Action = 30
GetPeers Action = 31
AddPeer Action = 32
DeletePeer Action = 33
Send Action = 34
SendChannel Action = 35
)

/*
// ActionToUint16 - returns the integer code for a string action name
func ActionToUint16(action string) uint16 {
switch action {
case "ID":
return ID
case "Dropoff":
return Dropoff
case "Pickup":
return Pickup
case "CID":
return CID
case "GetContact":
return GetContact
case "GetContacts":
return GetContacts
case "AddContact":
return AddContact
case "DeleteContact":
return DeleteContact
case "GetChannel":
return GetChannel
case "GetChannels":
return GetChannels
case "AddChannel":
return AddChannel
case "DeleteChannel":
return DeleteChannel
case "GetProfile":
return GetProfile
case "GetProfiles":
return GetProfiles
case "AddProfile":
return AddProfile
case "DeleteProfile":
return DeleteProfile
case "LoadProfile":
return LoadProfile
case "GetPeer":
return GetPeer
case "GetPeers":
return GetPeers
case "AddPeer":
return AddPeer
case "DeletePeer":
return DeletePeer
case "Send":
return Send
case "SendChannel":
return SendChannel
}
return Null
}
// ActionFromUint16 - returns the string name for an integer action code
func ActionFromUint16(action uint16) string {
switch action {
case ID:
return "ID"
case Dropoff:
return "Dropoff"
case Pickup:
return "Pickup"
case CID:
return "CID"
case GetContact:
return "GetContact"
case GetContacts:
return "GetContacts"
case AddContact:
return "AddContact"
case DeleteContact:
return "DeleteContact"
case GetChannel:
return "GetChannel"
case GetChannels:
return "GetChannels"
case AddChannel:
return "AddChannel"
case DeleteChannel:
return "DeleteChannel"
case GetProfile:
return "GetProfile"
case GetProfiles:
return "GetProfiles"
case AddProfile:
return "AddProfile"
case DeleteProfile:
return "DeleteProfile"
case LoadProfile:
return "LoadProfile"
case GetPeer:
return "GetPeer"
case GetPeers:
return "GetPeers"
case AddPeer:
return "AddPeer"
case DeletePeer:
return "DeletePeer"
case Send:
return "Send"
case SendChannel:
return "SendChannel"
}
return ""
}
*/
140 changes: 3 additions & 137 deletions api/remoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,6 @@ import (
"github.com/awgh/bencrypt/rsa"
)

// API Call ID numbers
const (
APINull = 0
APIID = 1
APIDropoff = 2
APIPickup = 3
APICID = 16
APIGetContact = 17
APIGetContacts = 18
APIAddContact = 19
APIDeleteContact = 20
APIGetChannel = 21
APIGetChannels = 22
APIAddChannel = 23
APIDeleteChannel = 24
APIGetProfile = 25
APIGetProfiles = 26
APIAddProfile = 27
APIDeleteProfile = 28
APILoadProfile = 29
APIGetPeer = 30
APIGetPeers = 31
APIAddPeer = 32
APIDeletePeer = 33
APISend = 34
APISendChannel = 35
)

// API Parameter Data types
const (
APITypeInvalid byte = 0x0
Expand Down Expand Up @@ -70,7 +42,7 @@ const (

// RemoteCall : defines a Remote Procedure Call
type RemoteCall struct {
Action string
Action Action
Args []interface{}
}

Expand All @@ -86,112 +58,6 @@ func (r *RemoteResponse) IsNil() bool { return r.Value == nil }
// IsErr - is this response an error?
func (r *RemoteResponse) IsErr() bool { return r.Error != "" }

// ActionToUint16 - returns the integer code for a string action name
func ActionToUint16(action string) uint16 {
switch action {
case "ID":
return APIID
case "Dropoff":
return APIDropoff
case "Pickup":
return APIPickup
case "CID":
return APICID
case "GetContact":
return APIGetContact
case "GetContacts":
return APIGetContacts
case "AddContact":
return APIAddContact
case "DeleteContact":
return APIDeleteContact
case "GetChannel":
return APIGetChannel
case "GetChannels":
return APIGetChannels
case "AddChannel":
return APIAddChannel
case "DeleteChannel":
return APIDeleteChannel
case "GetProfile":
return APIGetProfile
case "GetProfiles":
return APIGetProfiles
case "AddProfile":
return APIAddProfile
case "DeleteProfile":
return APIDeleteProfile
case "LoadProfile":
return APILoadProfile
case "GetPeer":
return APIGetPeer
case "GetPeers":
return APIGetPeers
case "AddPeer":
return APIAddPeer
case "DeletePeer":
return APIDeletePeer
case "Send":
return APISend
case "SendChannel":
return APISendChannel
}
return APINull
}

// ActionFromUint16 - returns the string name for an integer action code
func ActionFromUint16(action uint16) string {
switch action {
case APIID:
return "ID"
case APIDropoff:
return "Dropoff"
case APIPickup:
return "Pickup"
case APICID:
return "CID"
case APIGetContact:
return "GetContact"
case APIGetContacts:
return "GetContacts"
case APIAddContact:
return "AddContact"
case APIDeleteContact:
return "DeleteContact"
case APIGetChannel:
return "GetChannel"
case APIGetChannels:
return "GetChannels"
case APIAddChannel:
return "AddChannel"
case APIDeleteChannel:
return "DeleteChannel"
case APIGetProfile:
return "GetProfile"
case APIGetProfiles:
return "GetProfiles"
case APIAddProfile:
return "AddProfile"
case APIDeleteProfile:
return "DeleteProfile"
case APILoadProfile:
return "LoadProfile"
case APIGetPeer:
return "GetPeer"
case APIGetPeers:
return "GetPeers"
case APIAddPeer:
return "AddPeer"
case APIDeletePeer:
return "DeletePeer"
case APISend:
return "Send"
case APISendChannel:
return "SendChannel"
}
return ""
}

// ArgsToBytes - converts an interface array to a byte array
func ArgsToBytes(args []interface{}) []byte {
b := bytes.NewBuffer([]byte{})
Expand Down Expand Up @@ -219,7 +85,7 @@ func RemoteCallToBytes(call *RemoteCall) *[]byte {
b := bytes.NewBuffer([]byte{})
w := bufio.NewWriter(b)
// Action - bytes [0-1] uint16
binary.Write(w, binary.BigEndian, ActionToUint16(call.Action))
binary.Write(w, binary.BigEndian, call.Action)
// Args - everything else
binary.Write(w, binary.BigEndian, ArgsToBytes(call.Args))
w.Flush()
Expand All @@ -234,7 +100,7 @@ func RemoteCallFromBytes(input *[]byte) (*RemoteCall, error) {
}
call := new(RemoteCall)
action := binary.BigEndian.Uint16((*input)[:2])
call.Action = ActionFromUint16(action)
call.Action = Action(action)
args, err := ArgsFromBytes((*input)[2:])
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions api/remoting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_ArgsRoundTrip_1(t *testing.T) {

func Test_RoundTrip_1(t *testing.T) {
var call RemoteCall
call.Action = ActionFromUint16(APIID)
call.Action = ID
b := RemoteCallToBytes(&call)
recall, err := RemoteCallFromBytes(b)
if err != nil {
Expand All @@ -39,7 +39,7 @@ func Test_RoundTrip_1(t *testing.T) {

func Test_RoundTrip_2(t *testing.T) {
var call RemoteCall
call.Action = ActionFromUint16(APIAddProfile)
call.Action = AddProfile
var x uint64
x = 1234
y := "abcd1234"
Expand Down
2 changes: 1 addition & 1 deletion api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package api
type Transport interface {
Listen(listen string, adminMode bool)
Name() string
RPC(host string, method string, args ...interface{}) (interface{}, error)
RPC(host string, method Action, args ...interface{}) (interface{}, error)
Stop()
MarshalJSON() (b []byte, e error)

Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ github.com/templexxx/xorsimd v0.4.1/go.mod h1:W+ffZz8jJMH2SXwuKu9WhygqBMbFnp14G2
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/tjfoc/gmsm v1.4.0 h1:8nbaiZG+iVdh+fXVw0DZoZZa7a4TGm3Qab+xdrdzj8s=
github.com/tjfoc/gmsm v1.4.0/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/upper/db/v4 v4.0.2 h1:tx15rM0T74LFjkoFM99EN1S/7Qn0N4hReR/Bqvg8jiM=
github.com/upper/db/v4 v4.0.2/go.mod h1:irV6ipP9Ss3FBPDeVeHisLK4SnDqosjnyK0zO629alU=
github.com/upper/db/v4 v4.1.0 h1:mtVXgrPLTBuloykr9VjgQyAD4V4nAbDdXDdyd9MJcus=
github.com/upper/db/v4 v4.1.0/go.mod h1:irV6ipP9Ss3FBPDeVeHisLK4SnDqosjnyK0zO629alU=
github.com/xtaci/kcp-go/v5 v5.6.1 h1:Pwn0aoeNSPF9dTS7IgiPXn0HEtaIlVb6y5UKWPsx8bI=
Expand Down Expand Up @@ -121,7 +119,6 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -161,8 +158,6 @@ golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad h1:MCsdmFSdEd4UEa5TKS5JztCRHK/WtvNei1edOj5RSRo=
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 h1:nVuTkr9L6Bq62qpUqKo/RnZCFfzDBL0bYo6w9OJUqZY=
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand All @@ -178,14 +173,11 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200808161706-5bf02b21f123 h1:4JSJPND/+4555t1HfXYF4UEqDqiSKCgeV0+hbA8hMs4=
golang.org/x/tools v0.0.0-20200808161706-5bf02b21f123/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee h1:5xKxdl/RhlelmSPaxyVeq5PYSmJ4H14yeQT58qP1F6o=
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down Expand Up @@ -236,8 +228,6 @@ modernc.org/mathutil v1.2.1 h1:PSIN4RdyeB6MbFsNLSkFCzDjnEVEMS3H/hFHcJtAJ9g=
modernc.org/mathutil v1.2.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/ql v1.3.1 h1:gAXr1jn9w/WObUo12A1I5xMx/A8SrbRRBNzoBtCraPI=
modernc.org/ql v1.3.1/go.mod h1:0su3LZVtXgxr5HnJc3DWIj4tb8Zx08BjZAzs//CUT6o=
modernc.org/ql v1.3.2-0.20201221110958-fea8432768cf h1:SFJBwgNzIfxsdGs9Y+iKOVziUDaavSUF77vPExLHpQ0=
modernc.org/ql v1.3.2-0.20201221110958-fea8432768cf/go.mod h1:0su3LZVtXgxr5HnJc3DWIj4tb8Zx08BjZAzs//CUT6o=
modernc.org/sortutil v1.1.0 h1:oP3U4uM+NT/qBQcbg/K2iqAX0Nx7B1b6YZtq3Gk/PjM=
modernc.org/sortutil v1.1.0/go.mod h1:ZyL98OQHJgH9IEfN71VsamvJgrtRX9Dj2gX+vH86L1k=
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
Expand Down
Loading

0 comments on commit 3f160c0

Please sign in to comment.