Developing lightweight computation at the DSG edge

Unverified Commit 74b9e0e9 authored by Cameron Sparr's avatar Cameron Sparr Committed by GitHub
Browse files

Update sample code, README, and circle v2 (#42)

Update sample code, README, and circle v2
parent 667d0a66
version: 2.0
jobs:
build:
docker:
- image: circleci/golang:1.11
working_directory: /go/src/github.com/sparrc/go-ping
steps:
- checkout
- run:
name: Build and run some basic tests
command: |
go get ./...
go build -race -o ping_linux ./cmd/ping/ping.go
sudo ./ping_linux --privileged -c 2 www.google.com
sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com
sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com
GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go
- store_artifacts:
path: ./ping_linux
- store_artifacts:
path: ./ping_darwin
...@@ -25,6 +25,15 @@ if err != nil { ...@@ -25,6 +25,15 @@ if err != nil {
panic(err) panic(err)
} }
// listen for ctrl-C signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
pinger.Stop()
}
}()
pinger.OnRecv = func(pkt *ping.Packet) { pinger.OnRecv = func(pkt *ping.Packet) {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
...@@ -75,13 +84,19 @@ use setcap to allow your binary using go-ping to bind to raw sockets ...@@ -75,13 +84,19 @@ use setcap to allow your binary using go-ping to bind to raw sockets
(or just run as super-user): (or just run as super-user):
``` ```
setcap cap_net_raw=+ep /bin/goping-binary setcap cap_net_raw=+ep /bin/go-ping
``` ```
See [this blog](https://sturmflut.github.io/linux/ubuntu/2015/01/17/unprivileged-icmp-sockets-on-linux/)
and [the Go icmp library](https://godoc.org/golang.org/x/net/icmp) for more details.
## Note on Windows Support: ## Note on Windows Support:
You have to use "SetPrivileged(true)" like "pinger.SetPrivileged(true)", otherwise you will receive an error: You must use `pinger.SetPrivileged(true)`, otherwise you will receive an error:
"Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists." This even works without admin privileges. Tested on Windows 10
```
Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists.
```
This should without admin privileges. Tested on Windows 10.
See [this blog](https://sturmflut.github.io/linux/ubuntu/2015/01/17/unprivileged-icmp-sockets-on-linux/)
and [the Go icmp library](https://godoc.org/golang.org/x/net/icmp) for more details.
test:
post:
- go build -race -o ping_linux ./cmd/ping/ping.go
- sudo ./ping_linux --privileged -c 2 www.google.com
- sudo ./ping_linux --privileged -c 3 -i 200ms www.google.com
- sudo ./ping_linux --privileged -c 10 -i 100ms -t 1s www.google.com
- GOOS=darwin go build -o ping_darwin ./cmd/ping/ping.go
- mv ping_linux $CIRCLE_ARTIFACTS
- mv ping_darwin $CIRCLE_ARTIFACTS
...@@ -3,6 +3,8 @@ package main ...@@ -3,6 +3,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"os/signal"
"time" "time"
"github.com/sparrc/go-ping" "github.com/sparrc/go-ping"
...@@ -53,6 +55,15 @@ func main() { ...@@ -53,6 +55,15 @@ func main() {
return return
} }
// listen for ctrl-C signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
pinger.Stop()
}
}()
pinger.OnRecv = func(pkt *ping.Packet) { pinger.OnRecv = func(pkt *ping.Packet) {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt) pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment