Example #1
0
File: input.go Project: postfix/fwk
func (input *InputStreamer) Read(ctx fwk.Context) error {
	var err error
	store := ctx.Store()
	recs := make(map[string]struct{}, len(input.ports))
	for i := 0; i < len(input.ports); i++ {
		if !input.scan.Scan() {
			err = input.scan.Err()
			if err == nil {
				return io.EOF
			}
		}
		rec := input.scan.Record()
		blk := rec.Block(rec.Name())
		obj := reflect.New(input.ports[rec.Name()].Type).Elem()
		err := blk.Read(obj.Addr().Interface())
		if err != nil {
			return fwk.Errorf("block-read error: %v", err)
		}
		err = store.Put(rec.Name(), obj.Interface())
		if err != nil {
			return fwk.Errorf("store-put error: %v", err)
		}
		recs[rec.Name()] = struct{}{}
	}

	if len(recs) != len(input.ports) {
		return fwk.Errorf("fwk.rio: expected inputs: %d. got: %d.", len(input.ports), len(recs))
	}

	return err
}
Example #2
0
func (o *OutputStreamer) Write(ctx fwk.Context) error {
	var err error
	store := ctx.Store()

	for i, rec := range o.recs {
		port := o.ports[i]

		n := rec.Name()
		blk := rec.Block(n)
		obj, err := store.Get(n)
		if err != nil {
			return err
		}

		rt := reflect.TypeOf(obj)
		if rt != port.Type {
			return fwk.Errorf("record[%s]: got type=%q. want type=%q.",
				rec.Name(),
				rt.Name(),
				port.Type,
			)
		}

		err = blk.Write(obj)
		if err != nil {
			return err
		}

		err = rec.Write()
		if err != nil {
			return err
		}
	}
	return err
}
Example #3
0
func (tsk *task3) Process(ctx fwk.Context) error {
	msg := ctx.Msg()
	msg.Infof("proc...\n")
	store := ctx.Store()

	parts := make([]int, 0)
	err := store.Put(tsk.parts, parts)
	if err != nil {
		return err
	}

	return nil
}
Example #4
0
func (stream *InputStream) Read(ctx fwk.Context) error {
	var err error
	store := ctx.Store()
	var data int64
	_, err = fmt.Fscanf(stream.R, "%d\n", &data)
	if err != nil {
		return err
	}

	err = store.Put(stream.output, data)
	if err != nil {
		return err
	}

	return err
}
Example #5
0
func (out *OutputStream) Write(ctx fwk.Context) error {
	var err error
	store := ctx.Store()
	v, err := store.Get(out.input)
	if err != nil {
		return err
	}

	data := v.(int64)
	_, err = out.W.Write([]byte(fmt.Sprintf("%d\n", data)))
	if err != nil {
		return err
	}

	return err
}
Example #6
0
func (tsk *task2) Process(ctx fwk.Context) error {
	store := ctx.Store()
	msg := ctx.Msg()
	v, err := store.Get(tsk.input)
	if err != nil {
		return err
	}
	i := v.(int64)
	o := tsk.fct(i)
	err = store.Put(tsk.output, o)
	if err != nil {
		return err
	}

	msg.Infof("proc... (id=%d|%d) => [%d -> %d]\n", ctx.ID(), ctx.Slot(), i, o)
	return nil
}
Example #7
0
func (tsk *task1) Process(ctx fwk.Context) error {
	var err error
	msg := ctx.Msg()
	msg.Infof("proc... (id=%d|%d) => [%d, %d]\n", ctx.ID(), ctx.Slot(), tsk.i1, tsk.i2)
	store := ctx.Store()

	err = store.Put(tsk.i1prop, tsk.i1)
	if err != nil {
		return err
	}

	err = store.Put(tsk.i2prop, tsk.i2)
	if err != nil {
		return err
	}

	return nil
}
Example #8
0
func (tsk *CPUCruncher) Process(ctx fwk.Context) error {
	var err error
	store := ctx.Store()
	for _, input := range tsk.inputs {
		_, err = store.Get(input)
		if err != nil {
			return err
		}
	}

	cpu := <-tsk.cpuch
	time.Sleep(time.Duration(cpu) * time.Microsecond)

	for _, output := range tsk.outputs {
		err = store.Put(output, cpu)
		if err != nil {
			return err
		}
	}

	return err
}
Example #9
0
func (tsk *reducer) Process(ctx fwk.Context) error {
	var err error

	tsk.mux.Lock()
	tsk.nevts += 1
	tsk.mux.Unlock()

	store := ctx.Store()
	v, err := store.Get(tsk.input)
	if err != nil {
		return err
	}

	val := v.(int64)
	tsk.mux.Lock()
	tsk.sum += val
	sum := tsk.sum
	tsk.mux.Unlock()

	msg := ctx.Msg()
	msg.Infof("sum=%d (id=%d|%d)\n", sum, ctx.ID(), ctx.Slot())
	return err
}