Skip to content

Commit f73ac2c

Browse files
authored
V2 (#122)
- protocols are separated to packages - observe work's natively (#12, #99) - support's separate message - don't propagate reset message to client(#110) - body as ReadSeeker for easy transfer files - ability to set source port (#90) - (un)marshal udp/tcp coap message without allocation - added support for middleware to mux #73
1 parent db6048a commit f73ac2c

File tree

136 files changed

+13015
-10269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+13015
-10269
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
language: go
2-
31
language: minimal
42

53
services:
@@ -27,5 +25,5 @@ jobs:
2725
--mount type=bind,source="$(pwd)",target=/shared
2826
--network=host
2927
go-coap:build
30-
go test ./... -covermode=atomic -coverprofile=/shared/coverage.txt
28+
go test -race ./... -covermode=atomic -coverprofile=/shared/coverage.txt
3129
- bash <(curl -s https://codecov.io/bash)

Dockerfile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
FROM golang:1.13.5-alpine3.10 AS build
2-
RUN apk add --no-cache curl git build-base
3-
WORKDIR $GOPATH/src/github.com/go-ocf/go-coap
1+
FROM ubuntu:20.04 AS build
2+
RUN apt-get update \
3+
&& apt-get install -y gcc make git curl file
4+
RUN git clone https://github.com/udhos/update-golang.git \
5+
&& cd update-golang \
6+
&& ./update-golang.sh \
7+
&& ln -s /usr/local/go/bin/go /usr/bin/go
8+
WORKDIR $GOPATH/src/github.com/go-ocf/cloud
49
COPY go.mod go.sum ./
510
RUN go mod download
6-
COPY . .
7-
11+
COPY . .

README.md

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
[![codecov](https://codecov.io/gh/go-ocf/go-coap/branch/master/graph/badge.svg)](https://codecov.io/gh/go-ocf/go-coap)
33
[![Go Report](https://goreportcard.com/badge/github.com/go-ocf/go-coap)](https://goreportcard.com/report/github.com/go-ocf/go-coap)
44
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap?ref=badge_shield)
5+
[![backer](https://opencollective.com/go-coap/backers/badge.svg)](https://opencollective.com/go-coap#backer)
6+
[![sponsors](https://opencollective.com/go-coap/sponsors/badge.svg)](https://opencollective.com/go-coap#sponsors)
7+
[![contributors](https://img.shields.io/github/contributors/go-ocf/go-coap)](https://github.com/go-ocf/go-coap/graphs/contributors)
8+
[![GitHub stars](https://img.shields.io/github/stars/go-ocf/go-coap)](https://github.com/go-ocf/go-coap/stargazers)
9+
[![GitHub license](https://img.shields.io/github/license/go-ocf/go-coap)](https://github.com/go-ocf/go-coap/blob/master/LICENSE)
10+
[![GoDoc](https://godoc.org/github.com/go-ocf/go-coap?status.svg)](https://godoc.org/github.com/go-ocf/go-coap)
11+
[![Sourcegraph](https://sourcegraph.com/github.com/go-ocf/go-coap/-/badge.svg)](https://sourcegraph.com/github.com/go-ocf/go-coap?badge)
512

6-
# CoAP Client and Server for go
13+
# Go-CoAP
714

8-
Features supported:
15+
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things.
16+
The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.
17+
18+
The go-coap provides servers and clients for DTLS, TCP-TLS, UDP, TCP in golang language.
19+
20+
## Features
921
* CoAP over UDP [RFC 7252][coap].
1022
* CoAP over TCP/TLS [RFC 8232][coap-tcp]
1123
* Observe resources in CoAP [RFC 7641][coap-observe]
@@ -30,88 +42,73 @@ Features supported:
3042
```go
3143
// Server
3244
// See /examples/simple/server/main.go
33-
func handleA(w coap.ResponseWriter, req *coap.Request) {
34-
log.Printf("Got message in handleA: path=%q: %#v from %v", req.Msg.Path(), req.Msg, req.Client.RemoteAddr())
35-
w.SetContentFormat(coap.TextPlain)
36-
log.Printf("Transmitting from A")
37-
ctx, cancel := context.WithTimeout(req.Ctx, time.Second)
38-
defer cancel()
39-
if _, err := w.WriteWithContext(ctx, []byte("hello world")); err != nil {
40-
log.Printf("Cannot send response: %v", err)
45+
func handleA(w mux.ResponseWriter, req *message.Message) {
46+
log.Printf("got message in handleA: %+v from %v\n", req, w.Client().RemoteAddr())
47+
err := w.SetResponse(codes.GET, message.TextPlain, bytes.NewReader([]byte("hello world")))
48+
if err != nil {
49+
log.Printf("cannot set response: %v", err)
4150
}
4251
}
4352

4453
func main() {
45-
mux := coap.NewServeMux()
46-
mux.Handle("/a", coap.HandlerFunc(handleA))
54+
m := mux.NewRouter()
55+
m.Handle("/a", mux.HandlerFunc(handleA))
56+
m.Handle("/b", mux.HandlerFunc(handleB))
4757

48-
listenerErrorHandler := func(err error) bool {
49-
log.Printf("Error occurred on listener: %v", err)
50-
return true
51-
}
58+
log.Fatal(coap.ListenAndServe("udp", ":5688", m))
5259

53-
log.Fatal(coap.ListenAndServe("udp", ":5688", mux, listenerErrorHandler))
5460

5561
// for tcp
56-
// log.Fatal(coap.ListenAndServe("tcp", ":5688", mux, listenerErrorHandler))
62+
// log.Fatal(coap.ListenAndServe("tcp", ":5688", m))
5763

5864
// for tcp-tls
59-
// log.Fatal(coap.ListenAndServeTLS("tcp-tls", ":5688", &tls.Config{...}, mux, listenerErrorHandler))
65+
// log.Fatal(coap.ListenAndServeTLS("tcp", ":5688", &tls.Config{...}, m))
6066

6167
// for udp-dtls
62-
// log.Fatal(coap.ListenAndServeDTLS("udp-dtls", ":5688", &dtls.Config{...}, mux, listenerErrorHandler))
68+
// log.Fatal(coap.ListenAndServeDTLS("udp", ":5688", &dtls.Config{...}, m))
6369
}
6470
```
6571
#### Client
6672
```go
6773
// Client
6874
// See /examples/simpler/client/main.go
6975
func main() {
70-
co, err := coap.Dial("udp", "localhost:5688")
76+
co, err := udp.Dial("localhost:5688")
7177

7278
// for tcp
73-
// co, err := coap.Dial("tcp", "localhost:5688")
79+
// co, err := tcp.Dial("localhost:5688")
7480

7581
// for tcp-tls
76-
// co, err := coap.DialTLS("tcp-tls", localhost:5688", &tls.Config{...})
82+
// co, err := tcp.Dial("localhost:5688", tcp.WithTLS(&tls.Config{...}))
7783

78-
// for udp-dtls
79-
// co, err := coap.DialDTLS("udp-dtls", "localhost:5688", &dtls.Config{...}, mux))
84+
// for dtls
85+
// co, err := dtls.Dial("localhost:5688", &dtls.Config{...}))
8086

8187
if err != nil {
8288
log.Fatalf("Error dialing: %v", err)
8389
}
84-
8590
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
8691
defer cancel()
87-
resp, err := co.GetWithContext(ctx, path)
88-
89-
92+
resp, err := co.Get(ctx, "/a")
9093
if err != nil {
91-
log.Fatalf("Error sending request: %v", err)
94+
log.Fatalf("Cannot get response: %v", err)
95+
return
9296
}
93-
94-
log.Printf("Response payload: %v", resp.Payload())
97+
log.Printf("Response: %+v", resp)
9598
}
9699
```
97100

98-
99101
### Observe / Notify
100102

101-
#### Server
102-
Look to examples/observe/server/main.go
103-
104-
#### Client
105-
Look to examples/observe/client/main.go
103+
[Server](examples/observe/server/main.go) example.
106104

105+
[Client](examples/observe/client/main.go) example.
107106

108107
### Multicast
109108

110-
#### Server
111-
Look to examples/mcast/server/main.go
109+
[Server](examples/mcast/server/main.go) example.
112110

113-
#### Client
114-
Look to examples/mcast/client/main.go
111+
[Client](examples/mcast/client/main.go) example.
115112

116113
## Contributing
117114

@@ -126,3 +123,19 @@ $ docker run --mount type=bind,source="$(pwd)",target=/shared,readonly --network
126123
Apache 2.0
127124

128125
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-ocf%2Fgo-coap?ref=badge_large)
126+
127+
<h2 align="center">Sponsors</h2>
128+
129+
[Become a sponsor](https://opencollective.com/go-coap#sponsor) and get your logo on our README on Github with a link to your site.
130+
131+
<div align="center">
132+
133+
<a href="https://opencollective.com/go-coap/sponsor/0/website?requireActive=false" target="_blank"><img src="https://opencollective.com/go-coap/sponsor/0/avatar.svg?requireActive=false"></a>
134+
135+
</div>
136+
137+
<h2 align="center">Backers</h2>
138+
139+
[Become a backer](https://opencollective.com/go-coap#backer) and get your image on our README on Github with a link to your site.
140+
141+
<a href="https://opencollective.com/go-coap/backer/0/website?requireActive=false" target="_blank"><img src="https://opencollective.com/go-coap/backer/0/avatar.svg?requireActive=false"></a>

0 commit comments

Comments
 (0)