Skip to content

Commit

Permalink
Merge pull request #568 from cmackenzie1/cole/issue-566
Browse files Browse the repository at this point in the history
example: fix `hashes` example
  • Loading branch information
lthibault authored Jun 12, 2024
2 parents ce7c84a + 7353e94 commit b3fcee6
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 94 deletions.
1 change: 1 addition & 0 deletions example/hashes/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/hashtest
hashes.capnp.go
19 changes: 15 additions & 4 deletions example/hashes/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
cd to wherever you installed the source:
# RPC Example: Hashses

`cd /path/to/go-capnp/example/hashes`
## Running the example

Navigate to the example directory:

```bash
cd /path/to/go-capnp/example/hashes`
````
Compile the capnp file:
Expand All @@ -9,6 +15,11 @@ Compile the capnp file:
Build your code, and run it:
```
go build .
./hashtest
go run cmd/hashesserver.go
```

The output of the example should be the following

```bash
sha1: 0a0a9f2a6772942557ab5355d76af442f8f65e01
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"crypto/sha1"
"fmt"
"hash"
hashes "hashtest"
"io"
"log"
"net"
"os"

"context"
"hashes"

"capnproto.org/go/capnp/v3"
"capnproto.org/go/capnp/v3/rpc"
)

Expand All @@ -33,6 +35,7 @@ func (hf hashFactory) NewSha1(_ context.Context, call hashes.HashFactory_newSha1

// hashServer is a local implementation of Hash.
type hashServer struct {
// The state of the server
h hash.Hash
}

Expand All @@ -58,11 +61,11 @@ func (hs hashServer) Sum(_ context.Context, call hashes.Hash_sum) error {

func serveHash(ctx context.Context, rwc io.ReadWriteCloser) error {
// Create a new locally implemented HashFactory.
main := hashes.HashFactory_ServerToClient(hashFactory{}, nil)
client := hashes.HashFactory_ServerToClient(hashFactory{})

// Listen for calls, using the HashFactory as the bootstrap interface.
conn := rpc.NewConn(rpc.NewStreamTransport(rwc), &rpc.Options{
BootstrapClient: main.Client,
BootstrapClient: capnp.Client(client),
})
defer conn.Close()

Expand All @@ -75,20 +78,19 @@ func serveHash(ctx context.Context, rwc io.ReadWriteCloser) error {
}
}

func client(ctx context.Context, rwc io.ReadWriteCloser) error {
func callRpc(ctx context.Context, rwc io.ReadWriteCloser) error {
// Create a connection that we can use to get the HashFactory.
conn := rpc.NewConn(rpc.NewStreamTransport(rwc), nil) // nil sets default options
defer conn.Close()

// Get the "bootstrap" interface. This is the capability set with
// rpc.MainInterface on the remote side.
hf := hashes.HashFactory{Client: conn.Bootstrap(ctx)}
hf := hashes.HashFactory(conn.Bootstrap(ctx))

// Now we can call methods on hf, and they will be sent over c.
// The NewSha1 method does not have any parameters we can set, so we
// pass a nil function.
h, free := hf.NewSha1(ctx, nil)

defer free()

// 'NewSha1' returns a future, which allows us to pipeline calls to
Expand All @@ -109,11 +111,9 @@ func client(ctx context.Context, rwc io.ReadWriteCloser) error {

// Get the sum, waiting for the result.
sumFuture, free := s.Sum(ctx, nil)

defer free()

result, err := sumFuture.Struct()

if err != nil {
return err
}
Expand All @@ -127,33 +127,45 @@ func client(ctx context.Context, rwc io.ReadWriteCloser) error {
return nil
}

func chkfatal(err error) {
if err != nil {
panic(err)
}
}

func main() {

err := os.RemoveAll(SOCK_ADDR)
chkfatal(err)
if err != nil {
log.Fatal(err)
}

l, err := net.Listen("unix", SOCK_ADDR)
chkfatal(err)
if err != nil {
log.Fatal(err)
}

defer l.Close()
defer func(l net.Listener) {
err := l.Close()
if err != nil {
log.Fatal(err)
}
}(l)

ctx := context.Background()

go func() {
c1, err := l.Accept()
chkfatal(err)

serveHash(ctx, c1)
if err != nil {
log.Fatal(err)
}

err = serveHash(ctx, c1)
if err != nil {
log.Println(err)
}
}()

c2, err := net.Dial("unix", SOCK_ADDR)
chkfatal(err)
if err != nil {
log.Fatal(err)
}

client(ctx, c2)
err = callRpc(ctx, c2)
if err != nil {
log.Fatal(err)
}
}
11 changes: 6 additions & 5 deletions example/hashes/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module hashtest

go 1.17
go 1.18

require capnproto.org/go/capnp/v3 v3.0.0-alpha.4
require capnproto.org/go/capnp/v3 v3.0.0-alpha.30

require hashes v1.0.0 // indirect

replace hashes v1.0.0 => ./hashes
require (
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5 // indirect
)
35 changes: 9 additions & 26 deletions example/hashes/go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
capnproto.org/go/capnp/v3 v3.0.0-alpha.1 h1:lA/zWC1XgaFez/UdqcWi5Bz1grsqPQDY5Ki4lgYrQ1o=
capnproto.org/go/capnp/v3 v3.0.0-alpha.1/go.mod h1:izbWjXlvObqHwvhxwDBFLpY9lXSXIxDKRtnuktKHVsU=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
capnproto.org/go/capnp/v3 v3.0.0-alpha.30 h1:iABQan/YiHFCgSXym5aNj27osapnEgAk4WaWYqb4sQM=
capnproto.org/go/capnp/v3 v3.0.0-alpha.30/go.mod h1:+ysMHvOh1EWNOyorxJWs1omhRFiDoKxKkWQACp54jKM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0=
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
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=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/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-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5 h1:yksDCGMVzyn3vlyf0GZ3huiF5FFaMGQpQ3UJvR0EoGA=
zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5/go.mod h1:1LtNdPAs8WH+BTcQiZAOo2MIKD/5jyK/u7sZ9ZPe5SE=
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Go = import "/go.capnp";
@0xdb8274f9144abc7e;

using Go = import "/go.capnp";

$Go.package("hashes");
$Go.import("foo/hashes");

Expand Down
1 change: 0 additions & 1 deletion example/hashes/hashes/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions example/hashes/hashes/go.mod

This file was deleted.

29 changes: 0 additions & 29 deletions example/hashes/hashes/go.sum

This file was deleted.

0 comments on commit b3fcee6

Please sign in to comment.