Commits

Miki Tebeka committed 6084e43

Playing with netchan

Comments (0)

Files changed (1)

+package main
+
+import (
+    "flag"
+    "fmt"
+    "net"
+    "netchan"
+    "os"
+    "time"
+)
+
+
+func server() {
+    out := make(chan int64)
+
+    exporter := netchan.NewExporter()
+    exporter.Export("seconds", out, netchan.Send)
+    exporter.ListenAndServe("tcp", ":5959")
+
+    for {
+        out <- time.Seconds()
+        time.Sleep(1e9)
+    }
+
+}
+
+func client() {
+    conn, err := net.Dial("tcp", "localhost:5959")
+    if err != nil {
+        panic(fmt.Sprintf("can't connect"))
+    }
+
+    importer := netchan.NewImporter(conn)
+    in := make(chan int64)
+    if err := importer.Import("seconds", in, netchan.Recv, -1); err != nil {
+        panic("can't import")
+    }
+
+    for {
+        value := <- in
+        fmt.Println(value)
+    }
+}
+
+func main() {
+    defer func() {
+        if err := recover(); err != nil {
+            fmt.Fprintf(os.Stderr, "error: %s\n", err)
+            os.Exit(1)
+        }
+    }()
+
+    flag.Parse()
+    switch flag.Arg(0) {
+    case "server":
+        server()
+    case "client":
+        client()
+    default:
+        panic(fmt.Sprintf("unknown command %s", flag.Arg(0)))
+    }
+}