Skip to content

Instantly share code, notes, and snippets.

@kenenbek
Last active December 7, 2017 11:08
Show Gist options
  • Save kenenbek/c651b921470f088f53fbc1ab60bdb622 to your computer and use it in GitHub Desktop.
Save kenenbek/c651b921470f088f53fbc1ab60bdb622 to your computer and use it in GitHub Desktop.
Sender-Receiver example by simgo library
package main
import (
"fmt"
"github.com/bgmerrell/simgo"
)
var link = make(chan int)
type Host struct {
env *simgo.Environment
action *simgo.Process
}
func NewSender(env *simgo.Environment) *Host {
c := &Host{}
c.env = env
c.action = simgo.NewProcess(env, simgo.ProcWrapper(env, c.send))
c.action.Init()
return c
}
func NewReceiver(env *simgo.Environment) *Host {
c := &Host{}
c.env = env
c.action = simgo.NewProcess(env, simgo.ProcWrapper(env, c.receive))
c.action.Init()
return c
}
func (c *Host) send(env *simgo.Environment, pc *simgo.ProcComm) interface{} {
for{
fmt.Printf("Start sending at %d\n", env.Now)
x := 5
link <- x
to := simgo.NewTimeout(env, uint64(x / 1), nil)
to.Schedule(env)
pc.Yield(to.Event)
fmt.Printf("End sending at %d\n", env.Now)
}
}
func (c *Host) receive(env *simgo.Environment, pc *simgo.ProcComm) interface{} {
for{
fmt.Printf("Start executing at %d\n", env.Now)
x := <-link
to := simgo.NewTimeout(env, uint64(x / 5), nil)
to.Schedule(env)
pc.Yield(to.Event)
fmt.Printf("End executing at %d\n", env.Now)
}
}
func main() {
env := simgo.NewEnvironment()
NewSender(env)
NewReceiver(env)
env.Run(6)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment