diff --git a/cmd/apps/helloworld/helloworld.go b/cmd/apps/helloworld/helloworld.go index dba9c89241..1f4e220cc5 100644 --- a/cmd/apps/helloworld/helloworld.go +++ b/cmd/apps/helloworld/helloworld.go @@ -15,6 +15,10 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) +const ( + netType = appnet.TypeDMSG +) + func main() { clientConfig, err := app2.ClientConfigFromEnv() if err != nil { @@ -25,17 +29,13 @@ func main() { if err != nil { log.Fatalf("Error creating app client: %v\n", err) } - defer func() { - app.Close() - }() - - netType := appnet.TypeDMSG + defer app.Close() if len(os.Args) == 1 { port := routing.Port(1024) l, err := app.Listen(netType, port) if err != nil { - log.Fatalf("Error listening network %v on port %d\n", netType, port) + log.Fatalf("Error listening network %v on port %d: %v\n", netType, port, err) } log.Println("listening for incoming connections") diff --git a/cmd/apps/skychat/chat.go b/cmd/apps/skychat/chat.go index 6d98729852..73600357d2 100644 --- a/cmd/apps/skychat/chat.go +++ b/cmd/apps/skychat/chat.go @@ -14,6 +14,8 @@ import ( "sync" "time" + "github.com/skycoin/skywire/pkg/app2/appnet" + "github.com/skycoin/skywire/pkg/app2" "github.com/skycoin/dmsg/cipher" @@ -23,6 +25,11 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) +const ( + netType = appnet.TypeDMSG + port = routing.Port(1024) +) + var addr = flag.String("addr", ":8000", "address to bind") var r = netutil.NewRetrier(50*time.Millisecond, 5, 2) @@ -36,18 +43,20 @@ var ( func main() { appName := "skychat" - log = app.NewLogger(appName) + log = app2.NewLogger(appName) flag.Parse() - a, err := app.Setup(&app.Config{AppName: appName, AppVersion: "1.0", ProtocolVersion: "0.0.1"}) + clientConfig, err := app2.ClientConfigFromEnv() + if err != nil { + log.Fatalf("Error getting client config: %v\n", err) + } + + // TODO: pass `log`? + a, err := app2.NewClient(logging.MustGetLogger(fmt.Sprintf("app_%s", appName)), clientConfig) if err != nil { log.Fatal("Setup failure: ", err) } - defer func() { - if err := a.Close(); err != nil { - log.Println("Failed to close app:", err) - } - }() + defer a.Close() chatApp = a @@ -66,14 +75,20 @@ func main() { } func listenLoop() { + l, err := chatApp.Listen(netType, port) + if err != nil { + log.Printf("Error listening network %v on port %d: %v\n", netType, port) + return + } + for { - conn, err := chatApp.Accept() + conn, err := l.Accept() if err != nil { - log.Println("failed to accept conn:", err) + log.Println("Failed to accept conn:", err) return } - raddr := conn.RemoteAddr().(routing.Addr) + raddr := conn.RemoteAddr().(appnet.Addr) connsMu.Lock() chatConns[raddr.PubKey] = conn connsMu.Unlock() @@ -83,12 +98,12 @@ func listenLoop() { } func handleConn(conn net.Conn) { - raddr := conn.RemoteAddr().(routing.Addr) + raddr := conn.RemoteAddr().(appnet.Addr) for { buf := make([]byte, 32*1024) n, err := conn.Read(buf) if err != nil { - log.Println("failed to read packet:", err) + log.Println("Failed to read packet:", err) return } @@ -98,9 +113,9 @@ func handleConn(conn net.Conn) { } select { case clientCh <- string(clientMsg): - log.Printf("received and sent to ui: %s\n", clientMsg) + log.Printf("Received and sent to ui: %s\n", clientMsg) default: - log.Printf("received and trashed: %s\n", clientMsg) + log.Printf("Received and trashed: %s\n", clientMsg) } } } @@ -118,7 +133,11 @@ func messageHandler(w http.ResponseWriter, req *http.Request) { return } - addr := routing.Addr{PubKey: pk, Port: 1} + addr := appnet.Addr{ + Net: netType, + PubKey: pk, + Port: 1, + } connsMu.Lock() conn, ok := chatConns[pk] connsMu.Unlock()