Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit 5b94f81

Browse files
committed
all: first import
0 parents  commit 5b94f81

File tree

6 files changed

+446
-0
lines changed

6 files changed

+446
-0
lines changed

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright ©2020 The go-lpc Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are met:
5+
* Redistributions of source code must retain the above copyright
6+
notice, this list of conditions and the following disclaimer.
7+
* Redistributions in binary form must reproduce the above copyright
8+
notice, this list of conditions and the following disclaimer in the
9+
documentation and/or other materials provided with the distribution.
10+
* Neither the name of the go-lpc project nor the names of its authors and
11+
contributors may be used to endorse or promote products derived from this
12+
software without specific prior written permission.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# mim
2+
3+
[![GitHub release](https://img.shields.io/github/release/go-lpc/mim.svg)](https://github.com/go-lpc/mim/releases)
4+
[![Build Status](https://travis-ci.org/go-lpc/mim.svg?branch=master)](https://travis-ci.org/go-lpc/mim)
5+
[![codecov](https://codecov.io/gh/go-lpc/mim/branch/master/graph/badge.svg)](https://codecov.io/gh/go-lpc/mim)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/go-lpc/mim)](https://goreportcard.com/report/github.com/go-lpc/mim)
7+
[![GoDoc](https://godoc.org/go-lpc.org/x/hep?status.svg)](https://godoc.org/go-lpc.org/x/hep)
8+
[![License](https://img.shields.io/badge/License-BSD--3-blue.svg)](https://github.com/go-lpc/mim/blob/master/LICENSE)
9+
10+
11+
`mim` is the repository holding code for the MIM experiment.
12+
13+
## Installation
14+
15+
The `mim` package and its commands can be installed with `go get`:
16+
17+
```sh
18+
$> go get github.com/go-lpc/mim/...
19+
```
20+
21+
## Supported Go version
22+
23+
`mim` supports and tests on the two most recent Go releases.

cmd/mim-daq/main.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Copyright 2020 The go-lpc Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Command mim-daq starts a TDAQ run-control with MIM specificities.
6+
package main // import "github.com/go-lpc/mim/cmd/mim-daq"
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"io"
12+
"os"
13+
"strings"
14+
15+
"github.com/go-daq/tdaq"
16+
"github.com/go-daq/tdaq/config"
17+
"github.com/go-daq/tdaq/flags"
18+
"github.com/go-daq/tdaq/log"
19+
"github.com/peterh/liner"
20+
)
21+
22+
func main() {
23+
cmd := flags.NewRunControl()
24+
25+
run(cmd, os.Stdout)
26+
}
27+
28+
func run(cfg config.RunCtl, stdout io.Writer) {
29+
rc, err := tdaq.NewRunControl(cfg, os.Stdout)
30+
if err != nil {
31+
log.Errorf("could not create run control: %+v", err)
32+
os.Exit(1)
33+
}
34+
35+
if cfg.Interactive {
36+
term := newShell(cfg, rc)
37+
defer term.Close()
38+
}
39+
40+
err = rc.Run(context.Background())
41+
if err != nil {
42+
log.Errorf("could not run run-ctl: %+v", err)
43+
os.Exit(1)
44+
}
45+
}
46+
47+
func newShell(cfg config.RunCtl, rc *tdaq.RunControl) *liner.State {
48+
49+
fmt.Printf(`
50+
::::::::::::::::::::::::::
51+
::: RunControl shell :::
52+
::::::::::::::::::::::::::
53+
54+
- /config -> configure tdaq processes
55+
- /init -> initialize tdaq processes
56+
- /run -> start a new run
57+
- /stop -> stop current run
58+
- /reset -> reset tdaq processes
59+
- /status -> display status of all tdaq processes
60+
- /quit -> terminate tdaq processes (and quit)
61+
62+
`)
63+
64+
ps1 := cfg.Name + ">> "
65+
term := liner.NewLiner()
66+
term.SetWordCompleter(shellCompleter)
67+
term.SetTabCompletionStyle(liner.TabPrints)
68+
69+
go func() {
70+
quit := make(chan struct{})
71+
ctx := context.Background()
72+
defer func() {
73+
select {
74+
case <-quit:
75+
default:
76+
go rc.Do(ctx, tdaq.CmdQuit)
77+
}
78+
}()
79+
80+
for {
81+
o, err := term.Prompt(ps1)
82+
if err != nil {
83+
os.Stdout.Write([]byte("\n"))
84+
if err != io.EOF {
85+
log.Fatalf("error: %+v", err)
86+
}
87+
return
88+
}
89+
words := strings.Split(strings.TrimSpace(o), " ")
90+
if len(words) == 0 {
91+
continue
92+
}
93+
switch words[0] {
94+
case "":
95+
continue
96+
case "/config":
97+
term.AppendHistory(o)
98+
err = rc.Do(ctx, tdaq.CmdConfig)
99+
if err != nil {
100+
log.Errorf("could not run /config: %+v", err)
101+
continue
102+
}
103+
case "/init":
104+
term.AppendHistory(o)
105+
err = rc.Do(ctx, tdaq.CmdInit)
106+
if err != nil {
107+
log.Errorf("could not run /init: %+v", err)
108+
continue
109+
}
110+
case "/reset":
111+
term.AppendHistory(o)
112+
err = rc.Do(ctx, tdaq.CmdReset)
113+
if err != nil {
114+
log.Errorf("could not run /reset: %+v", err)
115+
continue
116+
}
117+
case "/run":
118+
term.AppendHistory(o)
119+
err = rc.Do(ctx, tdaq.CmdStart)
120+
if err != nil {
121+
log.Errorf("could not run /start: %+v", err)
122+
continue
123+
}
124+
case "/stop":
125+
term.AppendHistory(o)
126+
err = rc.Do(ctx, tdaq.CmdStop)
127+
if err != nil {
128+
log.Errorf("could not run /stop: %+v", err)
129+
continue
130+
}
131+
case "/quit":
132+
term.AppendHistory(o)
133+
err = rc.Do(ctx, tdaq.CmdQuit)
134+
if err != nil {
135+
log.Errorf("could not run /quit: %+v", err)
136+
continue
137+
}
138+
close(quit)
139+
return
140+
case "/status":
141+
term.AppendHistory(o)
142+
err = rc.Do(ctx, tdaq.CmdStatus)
143+
if err != nil {
144+
log.Errorf("could not run /status: %+v", err)
145+
continue
146+
}
147+
default:
148+
log.Errorf("invalid tdaq command %q", o)
149+
continue
150+
}
151+
}
152+
}()
153+
154+
return term
155+
}
156+
157+
func shellCompleter(line string, pos int) (prefix string, completions []string, suffix string) {
158+
if pos != len(line) {
159+
// TODO(sbinet): better mid-line matching...
160+
prefix, completions, suffix = shellCompleter(line[:pos], pos)
161+
return prefix, completions, suffix + line[pos:]
162+
}
163+
164+
if strings.TrimSpace(line) == "" {
165+
return line, nil, ""
166+
}
167+
168+
cmds := []string{
169+
"/config", "/init", "/reset",
170+
"/run", "/stop",
171+
"/quit",
172+
"/status",
173+
}
174+
175+
for _, cmd := range cmds {
176+
if strings.HasPrefix(cmd, line) {
177+
completions = append(completions, cmd[pos:])
178+
}
179+
}
180+
181+
return line, completions, ""
182+
}

cmd/mim-rpi/main.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2020 The go-lpc Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Command mim-rpi starts a TDAQ server on a RPi node.
6+
package main // import "github.com/go-lpc/mim/cmd/mim-rpi"
7+
8+
import (
9+
"context"
10+
"log"
11+
"math/rand"
12+
"os"
13+
"time"
14+
15+
"github.com/go-daq/tdaq"
16+
"github.com/go-daq/tdaq/flags"
17+
)
18+
19+
func main() {
20+
cmd := flags.New()
21+
22+
dev := rpi{
23+
name: cmd.Args[0],
24+
seed: 1234,
25+
}
26+
27+
srv := tdaq.New(cmd, os.Stdout)
28+
srv.CmdHandle("/config", dev.OnConfig)
29+
srv.CmdHandle("/init", dev.OnInit)
30+
srv.CmdHandle("/reset", dev.OnReset)
31+
srv.CmdHandle("/start", dev.OnStart)
32+
srv.CmdHandle("/stop", dev.OnStop)
33+
srv.CmdHandle("/quit", dev.OnQuit)
34+
35+
srv.OutputHandle("/adc", dev.adc)
36+
37+
srv.RunHandle(dev.run)
38+
39+
err := srv.Run(context.Background())
40+
if err != nil {
41+
log.Panicf("error: %+v", err)
42+
}
43+
}
44+
45+
type rpi struct {
46+
name string
47+
48+
seed int64
49+
rnd *rand.Rand
50+
51+
n int
52+
data chan []byte
53+
}
54+
55+
func (dev *rpi) OnConfig(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
56+
ctx.Msg.Debugf("received /config command...")
57+
return nil
58+
}
59+
60+
func (dev *rpi) OnInit(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
61+
ctx.Msg.Debugf("received /init command...")
62+
dev.rnd = rand.New(rand.NewSource(dev.seed))
63+
dev.data = make(chan []byte, 1024)
64+
dev.n = 0
65+
return nil
66+
}
67+
68+
func (dev *rpi) OnReset(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
69+
ctx.Msg.Debugf("received /reset command...")
70+
dev.rnd = rand.New(rand.NewSource(dev.seed))
71+
dev.data = make(chan []byte, 1024)
72+
dev.n = 0
73+
return nil
74+
}
75+
76+
func (dev *rpi) OnStart(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
77+
ctx.Msg.Debugf("received /start command...")
78+
return nil
79+
}
80+
81+
func (dev *rpi) OnStop(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
82+
n := dev.n
83+
ctx.Msg.Debugf("received /stop command... -> n=%d", n)
84+
return nil
85+
}
86+
87+
func (dev *rpi) OnQuit(ctx tdaq.Context, resp *tdaq.Frame, req tdaq.Frame) error {
88+
ctx.Msg.Debugf("received /quit command...")
89+
return nil
90+
}
91+
92+
func (dev *rpi) adc(ctx tdaq.Context, dst *tdaq.Frame) error {
93+
select {
94+
case <-ctx.Ctx.Done():
95+
dst.Body = nil
96+
return nil
97+
case data := <-dev.data:
98+
dst.Body = data
99+
}
100+
return nil
101+
}
102+
103+
func (dev *rpi) run(ctx tdaq.Context) error {
104+
for {
105+
select {
106+
case <-ctx.Ctx.Done():
107+
return nil
108+
default:
109+
raw := make([]byte, 1024)
110+
rand.Read(raw)
111+
select {
112+
case dev.data <- raw:
113+
dev.n++
114+
default:
115+
}
116+
}
117+
time.Sleep(100 * time.Millisecond)
118+
}
119+
120+
return nil
121+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/go-lpc/mim
2+
3+
go 1.14
4+
5+
require (
6+
github.com/go-daq/tdaq v0.13.0
7+
github.com/peterh/liner v1.1.0
8+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect
9+
golang.org/x/sys v0.0.0-20200217220822-9197077df867 // indirect
10+
)

0 commit comments

Comments
 (0)