//Helper function to import the Master channel from masterAddr func importMasterChan(t Task) (c chan WorkSummary, err os.Error) { mu.Lock() defer mu.Unlock() if c, present := _sessions[t.Session.Id]; present { log.Printf("Cached Session %v", t.Session.Id) return c, nil } imp, err := netchan.Import("tcp", t.MasterAddr) if err != nil { log.Printf("Failed to create importer for %v", t.MasterAddr) return nil, err } c = make(chan WorkSummary, 10) imp.Import("masterChannel", c, netchan.Send, 10) go func() { err := <-imp.Errors() log.Print(err) }() _sessions[t.Session.Id] = c go cacheWatcher(t.Session) return c, nil }
func main() { host := flag.String("host", "localhost:8001", "remote host") flag.Parse() fmt.Printf("attaching to %s\n", host) imp, err := netchan.Import("tcp", *host) if err != nil { log.Fatal(err) } ch := make(chan string) err = imp.ImportNValues("pingme", ch, netchan.Recv, 1, -1) if err != nil { log.Fatal(err) } errors := imp.Errors() loop: for { select { case t, ok := <-ch: if !ok { fmt.Printf("channel closed\n") break loop } fmt.Printf("got %v\n", t) case e := <-errors: fmt.Printf("error %v\n", e) break loop } } }
//Creates a new Proxy importing 'workerChannel' from Worker running //on workerAddr func NewProxyWorker(workerAddr string) (p *ProxyWorker, err os.Error) { log.Printf("Setting up a ProxyWorker for %s", workerAddr) p = new(ProxyWorker) imp, err := netchan.Import("tcp", workerAddr) if err != nil { return } p.importer = imp return }
func client(name string, port string, done chan bool) { l := log.New(os.Stdout, fmt.Sprintf("client [%v]: ", name), 0) ch_recv := make(chan chan_type) imp, err := netchan.Import("tcp", port) if err != nil { l.Fatal(err) } err = imp.Import(name, ch_recv, netchan.Recv, 1) if err != nil { l.Fatal(err) } for v := range ch_recv { l.Println(v) } done <- true }
func main() { var r *bool var t *bool r = flag.Bool("rps", false, "Play a game of Rock-Paper-Scissors") t = flag.Bool("ttt", false, "Play a game of Tick-Tack-Toe") flag.Parse() args := flag.Args() var port string if len(args) == 1 { port = args[0] } else { fmt.Fprint(os.Stderr, "You must provide a port to connect to.") return } if *r && *t { fmt.Fprintln(os.Stderr, "You can only play one game at a time.") return } else if !(*r || *t) { // connext to an exporter with an importer // run the importer loop v2 := games.NewView() importer, err := netchan.Import("tcp", "localhost"+port) if err != nil { fmt.Fprintln(os.Stderr, err.String()) return } err = importer.Import("Request", v2.Request, netchan.Recv, 1) if err != nil { fmt.Fprintln(os.Stderr, err.String()) return } err = importer.Import("Responce", v2.Response, netchan.Send, 1) if err != nil { fmt.Fprintln(os.Stderr, err.String()) return } ov2 := &games.ViewView{v2, os.Stdin, os.Stdout} ov2.Loop() return } var game games.Igame if *r { game = rps.NewGame() } else if *t { game = ttt.NewGame() } v1 := games.NewView() v2 := games.NewView() // <- this will be the exported/inported thingy ref := games.NewReferee(game, v1, v2) exporter := netchan.NewExporter() exporter.Export("Request", v2.Request, netchan.Send) exporter.Export("Responce", v2.Response, netchan.Recv) exporter.ListenAndServe("tcp", port) ov1 := &games.ViewView{v1, os.Stdin, os.Stdout} go ov1.Loop() ref.Loop() return }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "usage: share [options] tcp-addr\n") flag.PrintDefaults() os.Exit(2) } flag.Parse() if flag.NArg() != 1 { flag.Usage() return } addr := flag.Arg(0) if *server { exp := netchan.NewExporter() if err := exp.ListenAndServe("tcp", addr); err != nil { log.Fatal("listen failed: ", err) } listener, err := ncnet.Listen(exp, "ctl") if err != nil { log.Fatal("ncnet listen failed: ", err) } srv := &Server{ exp: exp, clients: make(map[string]*rpc.Client), } rpcsrv := rpc.NewServer() if err := rpcsrv.Register(srv); err != nil { log.Fatal("rpcsrv register failed: ", err) } rpcsrv.Accept(listener) listener.Close() return } imp, err := netchan.Import("tcp", addr) if err != nil { log.Fatal("netchan import failed: ", err) } srvconn, err := ncnet.Dial(imp, "ctl") if err != nil { log.Fatal("ncnet dial failed: ", err) } srv := rpc.NewClient(srvconn) var clientId string if err := srv.Call("Server.Publish", clientName, &clientId); err != nil { log.Fatal("publish failed: %v", err) } clientsrv := rpc.NewServer() if err := clientsrv.Register(Client{}); err != nil { log.Fatal("clientsrv register failed: ", err) } clientconn, err := ncnet.Dial(imp, clientId) if err != nil { log.Fatalf("ncnet dial %q failed: %v", clientId, err) } go clientsrv.ServeConn(clientconn) interact(srv) clientconn.Close() time.Sleep(0.1e9) // wait for close to propagate }