Skip to content

Commit

Permalink
Refactor skychat app
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Oct 23, 2019
1 parent e78f45d commit 829f638
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
12 changes: 6 additions & 6 deletions cmd/apps/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/skycoin/skywire/pkg/routing"
)

const (
netType = appnet.TypeDMSG
)

func main() {
clientConfig, err := app2.ClientConfigFromEnv()
if err != nil {
Expand All @@ -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")
Expand Down
49 changes: 34 additions & 15 deletions cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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
}

Expand All @@ -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)
}
}
}
Expand All @@ -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()
Expand Down

0 comments on commit 829f638

Please sign in to comment.