Example #1
0
func main() {
	e := netchan.NewExporter()
	ch := make(chan string)
	err := e.Export("pingme", ch, netchan.Send)
	if err != nil {
		log.Fatal(err)
	}
	allDone := make(chan bool)
	go func() {
		for i := 0; i < 3; i++ {
			ch <- fmt.Sprintf("ping %d", i)
		}
		allDone <- true
	}()
	fmt.Printf("listening on localhost:8001\n")
	listener, err := net.Listen("tcp", ":8001")
	if err != nil {
		log.Fatal(err)
	}
	go e.Serve(listener)
	_ = <-allDone
	e.Drain(0)
	e.Hangup("pingme")
	time.Sleep(time.Second)
}
Example #2
0
func main() {
	fmt.Println("Started nctest")

	ready := make(chan bool)
	ready2 := make(chan bool)

	addr, _ := net.ResolveTCPAddr("127.0.0.1:8000")
	lstn, _ := net.ListenTCP(addr.Network(), addr)
	exp := netchan.NewExporter()
	go server("netchan1", exp, ready, ready2)
	go server("netchan2", exp, ready, ready2)

	<-ready
	<-ready

	go func() {
		ready2 <- true
		ready2 <- true
		exp.Serve(lstn)
	}()

	done := make(chan bool)
	go client("netchan1", "127.0.0.1:8000", done)
	go client("netchan2", "127.0.0.1:8000", done)
	<-done
	<-done
	lstn.Close()
}
Example #3
0
File: main.go Project: qrush/go
func main() {
	fmt.Println("LOL")
	exp, eerr := netchan.NewExporter("tcp", ":9292")
	fmt.Println(eerr)
	c := make(chan os.SyscallError)
	err := exp.Export("octocator", c, netchan.Send, new(os.SyscallError))
	fmt.Println(err)
	c <- os.NewSyscallError("foo", 1)
}
Example #4
0
func main() {
	exporter := netchan.NewExporter()
	in := make(chan string)
	out := make(chan string)
	exporter.Export("input", in, netchan.Recv)
	exporter.Export("output", out, netchan.Send)
	exporter.ListenAndServe("tcp", "localhost:1234")
	runner(in, out)
}
Example #5
0
//Creates a new LocalWorker. If export is true, than
//the LocalWorker exports its input channel in the network address
//provided by workerAddr
func NewLocalWorker(mode, hostAddr *string) (w *LocalWorker) {
	defer func() {
		if e := recover(); e != nil {
			log.Fatalf("Panic starting the worker!!!!", e)
		}
	}()
	w = new(LocalWorker)
	w.channel = make(chan Task, 10)
	w.mode = mode
	//exports the channels
	if *mode == WORKER {
		e := netchan.NewExporter()
		e.Export("workerChannel", w.channel, netchan.Recv)
		e.ListenAndServe("tcp", *hostAddr)
	}
	return
}
Example #6
0
//  NewServer creates a new RPC-over-netchan server.  It returns a new
//  Server instance containing a netchan.Exporter and an rpc.Server which
//  is listening on a channel within it.  It reserves the use of netchan
//  channels with the prefix "ncrpc".
//
//  If acceptClientRPC is true, the server will accept incoming client
//  RPC registrations made by Client.Serve.
//
//  Conventionally Register is called on the rpc.Server to export some
//  server RPC methods, and ListenAndServe is then called on the
//  netchan.Exporter to listen on the network.
func NewServer(acceptClientRPC bool) (*Server, error) {
	rpcsrv := rpc.NewServer()
	exp := netchan.NewExporter()
	nclis, err := ncnet.Listen(exp, "ncrpc.ctl")
	if err != nil {
		return nil, err
	}
	srv := &Server{
		clients:   make(map[string]*rpc.Client),
		Exporter:  exp,
		RPCServer: rpcsrv,
	}
	rpcsrv.RegisterName("Ncnet-publisher", publisher{acceptClientRPC, srv})
	go func() {
		rpcsrv.Accept(nclis)
		nclis.Close()
	}()
	return srv, nil
}
Example #7
0
func inChanFactory() (chan value, os.Error) {
	//Create and initialize the export channel
	exp, err := netchan.NewExporter("tcp", ":2346")
	lt()
	fmt.Println("exp.Addr().String(): ", exp.Addr().String())
	if err != nil {
		return nil, err
	}
	lt()
	fmt.Println("incoming exportFactory Channel Made")
	//Make the communication channel for this program
	ch := make(chan value)
	//Link the ch channel to the export channel
	err = exp.Export("exportChannelRecv", ch, netchan.Recv, new(value))
	if err != nil {
		return nil, err
	}
	return ch, err
}
Example #8
0
//New Master returned. If mode is master, attempts to export the
//master channel for workers.
//Timout is also considere.
func NewMaster(mode, hostAddr *string, timeout int64) *Master {
	log.Print("Starting Master...")
	masterChan := make(chan WorkSummary, 10)
	m := new(Master)
	m.session = newSession(timeout)

	log.Printf("TEST SESSION %v", m.session)
	if *mode == MASTER {
		m.exptr = netchan.NewExporter()
		m.exptr.Export("masterChannel", masterChan, netchan.Recv)
		m.exptr.ListenAndServe("tcp", *hostAddr)
	}

	m.channel = masterChan
	m.mode = mode
	m.summary = &Summary{Min: -1}
	return m

}
Example #9
0
func NewServer() *Server {

	exp := netchan.NewExporter()

	//in := make(chan []byte)
	//err := exp.Export("broadcaster", in, netchan.Send)
	//if err != nil { log.Fatal(err) }

	//out := make(chan []byte)
	//err = exp.Export("receiver", out, netchan.Recv)
	//if err != nil { log.Fatal(err) }
	reqChan := make(chan []byte)
	resChan := make(chan int64)
	s := &Server{tickets: 0,
		exp:       exp,
		ticketReq: reqChan,
		ticketRes: resChan,
		clients:   make(map[int64]*client)}
	return s
}
Example #10
0
func RunChannel() *chan int {

	exporter, err := netchan.NewExporter("tcp", "127.0.0.1:7000")
	s := make(chan int)
	if err != nil {
		panic(err.String())
	}
	srv_netchan := make(chan Message)
	err = exporter.Export("LCIMessenger", srv_netchan, netchan.Recv)
	if err != nil {
		panic(err.String())
	}
	go func() {
		for {
			msg := <-srv_netchan
			fmt.Printf("Received message from master, type = %d\n", msg.Type)
			msg_resp := Message{10}
			srv_netchan <- msg_resp

		}
	}()
	return &s
}
Example #11
0
File: games.go Project: qrush/go
// Factory to make a proxy view implementation
func NewProxyView(name, local, remote string) (view View) {
	ls := strings.Split(local, ":", 0)
	rs := strings.Split(remote, ":", 0)

	view = new(ProxyView)
	view.(*ProxyView).name = name
	view.(*ProxyView).exp, _ = netchan.NewExporter("tcp", local)
	view.(*ProxyView).gotMove = make(chan bool)
	view.(*ProxyView).in = make(chan Move)
	view.(*ProxyView).out = make(chan Move)

	view.(*ProxyView).exp.Export(ls[1], view.(*ProxyView).out, netchan.Send, new(Move))
	for {
		var err os.Error
		if view.(*ProxyView).imp, err = netchan.NewImporter("tcp", remote); err != nil {
			time.Sleep(Second)
		} else {
			break
		}
	}
	view.(*ProxyView).imp.Import(rs[1], view.(*ProxyView).in, netchan.Recv, new(Move))
	return
}
Example #12
0
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
}
Example #13
0
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
}
Example #14
0
File: main.go Project: qrush/go
func main() {
	exp, _ := netchan.NewExporter("tcp", ":9292")
	imp, _ := netchan.NewImporter("tcp", ":9292")
	go exportSend(exp)
	importReceive(imp)
}