Skip to content

Commit

Permalink
Merge pull request #182 from Bitspark/spec-ext
Browse files Browse the repository at this point in the history
Spec ext
  • Loading branch information
jm9e authored Dec 6, 2018
2 parents e85e4f5 + ac560b0 commit 23b6ef5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/slangd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (e *EnvironPaths) loadLocalComponents() {
func (e *EnvironPaths) loadDaemonServices(srv *daemon.Server) {
srv.AddRedirect("/", "/app/")
srv.AddAppServer("/app", http.Dir(e.SLANG_UI))
srv.AddAppServer("/studio", http.Dir(filepath.Join(filepath.Dir(e.SLANG_UI), "studio")))
srv.AddService("/operator", daemon.DefinitionService)
srv.AddService("/run", daemon.RunnerService)
srv.AddService("/share", daemon.SharingService)
Expand Down
19 changes: 14 additions & 5 deletions pkg/daemon/runner_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package daemon
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,11 +58,11 @@ var RunnerService = &Service{map[string]*Endpoint{
for portUsed {
port++
portUsed = false
for _, ri := range runningInstances {
if ri.port == port {
portUsed = true
break
}
ln, err := net.Listen("tcp", ":" + strconv.Itoa(port))
if err != nil {
portUsed = true
} else {
ln.Close()
}
}

Expand Down Expand Up @@ -98,14 +100,21 @@ var RunnerService = &Service{map[string]*Endpoint{
op *core.Operator
}{port, op}

op.Main().Out().Bufferize()
op.Start()
log.Printf("operator %s (port: %d, id: %s) started", packagedOperator, port, strconv.FormatInt(handle, 16))
op.Main().In().Push(nil) // Start server

data.Status = "success"
data.Handle = strconv.FormatInt(handle, 16)
data.URL = "/instance/" + strconv.FormatInt(handle, 16)

writeJSON(w, &data)

go func() {
oprlt := op.Main().Out().Pull()
log.Printf("operator %s (port: %d, id: %s) terminated: %v", packagedOperator, port, strconv.FormatInt(handle, 16), oprlt)
}()
} else if r.Method == "DELETE" {
type stopInstructionJSON struct {
Handle string `json:"handle"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/sharing_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
)

type manifest struct {
SlangVersion string `yaml:slangVersion`
TimeUnix int64 `yaml:timeUnix`
SlangVersion string `yaml:"slangVersion"`
TimeUnix int64 `yaml:"timeUnix"`
}

var suffixes = []string{"_visual.yaml"}
Expand Down
2 changes: 1 addition & 1 deletion pkg/elem/encoding_xlsx_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var encodingXLSXReadCfg = &builtinConfig{
Stream: &core.TypeDef{
Type: "stream",
Stream: &core.TypeDef{
Type: "string",
Type: "primitive",
},
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/elem/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func init() {
Register("slang.stream.WindowRelease", streamWindowReleaseCfg)
Register("slang.stream.MapToStream", streamMapToStreamCfg)
Register("slang.stream.StreamToMap", streamStreamToMapCfg)
Register("slang.stream.Slice", streamSliceCfg)

// Miscellaneous operators
Register("slang.net.HTTPServer", netHTTPServerCfg)
Expand Down
86 changes: 86 additions & 0 deletions pkg/elem/stream_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package elem

import (
"github.com/Bitspark/slang/pkg/core"
)

var streamSliceCfg = &builtinConfig{
opDef: core.OperatorDef{
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"offset": {
Type: "stream",
Stream: &core.TypeDef{
Type: "number",
},
},
"length": {
Type: "stream",
Stream: &core.TypeDef{
Type: "number",
},
},
"step": {
Type: "stream",
Stream: &core.TypeDef{
Type: "number",
},
},
"stream": {
Type: "stream",
Stream: &core.TypeDef{
Type: "generic",
Generic: "itemType",
},
},
},
},
Out: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"stream": {
Type: "stream",
Stream: &core.TypeDef{
Type: "generic",
Generic: "itemType",
},
},
},
},
},
},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
for !op.CheckStop() {
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}

im := i.(map[string]interface{})

stream := im["stream"].([]interface{})
offset := int(im["offset"].(float64))
length := int(im["length"].(float64))
step := int(im["step"].(float64))

until := len(stream)
if until > offset + length {
until = offset + length
}

out.Map("stream").PushBOS()
outStream := out.Map("stream").Stream()
for i := offset; i < until; i += step {
outStream.Push(stream[i])
}
out.Map("stream").PushEOS()
}
},
}

0 comments on commit 23b6ef5

Please sign in to comment.