server can send messages to clients i suppose

master
Jordan Orelli 10 years ago
parent 24c2a2aec2
commit d1ef0f0597

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"code.google.com/p/go.crypto/ssh/terminal" "code.google.com/p/go.crypto/ssh/terminal"
"crypto/rsa" "crypto/rsa"
"encoding/json"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -50,6 +51,44 @@ func (c *Client) dial() error {
c.info("connected to %s", addr) c.info("connected to %s", addr)
c.conn = conn c.conn = conn
c.prompt = fmt.Sprintf("%s> ", addr) c.prompt = fmt.Sprintf("%s> ", addr)
go c.handleMessages()
return nil
}
func (c *Client) handleMessages() {
messages := make(chan Envelope)
errors := make(chan error)
done := make(chan interface{})
go stream(c.conn, messages, errors, done)
for {
select {
case message := <-messages:
if err := c.handleMessage(message); err != nil {
c.err("error handling message from server: %v", err)
}
case err := <-errors:
c.err("server error: %v", err)
case <-done:
return
}
}
}
func (c *Client) handleMessage(m Envelope) error {
switch m.Kind {
case "meta":
return c.handleMeta(m.Body)
default:
return fmt.Errorf("received message of unsupported type: %v", m.Kind)
}
}
func (c *Client) handleMeta(body json.RawMessage) error {
var meta Meta
if err := json.Unmarshal(body, &meta); err != nil {
return fmt.Errorf("unable to unmarshal meta message: %v", err)
}
c.info("message from server: %v", meta)
return nil return nil
} }

@ -0,0 +1,9 @@
package main
import ()
type Meta string
func (m Meta) Kind() string {
return "meta"
}

@ -1,6 +1,7 @@
package main package main
import ( import (
"crypto/rsa"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -24,38 +25,58 @@ func stream(r io.Reader, c chan Envelope, e chan error, done chan interface{}) {
} }
} }
func handleAuthRequest(conn net.Conn, body json.RawMessage) error { type serverConnection struct {
var auth Auth conn net.Conn
if err := json.Unmarshal(body, &auth); err != nil { nick string
return fmt.Errorf("bad auth request: %v", err) key rsa.PublicKey
}
func (s *serverConnection) sendMeta(template string, args ...interface{}) {
m := Meta(fmt.Sprintf(template, args...))
if err := s.sendRequest(m); err != nil {
error_log.Printf("error sending message to client: %v", err)
} }
info_log.Printf("authenticated user %s", auth.Nick)
return nil
} }
func handleRequest(conn net.Conn, request Envelope) error { func (s *serverConnection) sendRequest(r request) error {
return writeRequest(s.conn, r)
}
func (s *serverConnection) handleRequest(request Envelope) error {
switch request.Kind { switch request.Kind {
case "auth": case "auth":
return handleAuthRequest(conn, request.Body) return s.handleAuthRequest(request.Body)
default: default:
return fmt.Errorf("no such request type: %v", request.Kind) return fmt.Errorf("no such request type: %v", request.Kind)
} }
} }
func handleConnection(conn net.Conn) { func (s *serverConnection) handleAuthRequest(body json.RawMessage) error {
var auth Auth
if err := json.Unmarshal(body, &auth); err != nil {
return fmt.Errorf("bad auth request: %v", err)
}
s.nick = auth.Nick
s.key = auth.Key
s.sendMeta("hello, %s", auth.Nick)
info_log.Printf("authenticated user %s", auth.Nick)
return nil
}
func (s *serverConnection) run() {
defer func() { defer func() {
conn.Close() s.conn.Close()
info_log.Printf("connection ended: %v", conn.RemoteAddr()) info_log.Printf("connection ended: %v", s.conn.RemoteAddr())
}() }()
info_log.Printf("connection start: %v", conn.RemoteAddr()) info_log.Printf("connection start: %v", s.conn.RemoteAddr())
requests := make(chan Envelope) requests := make(chan Envelope)
errors := make(chan error) errors := make(chan error)
done := make(chan interface{}) done := make(chan interface{})
go stream(conn, requests, errors, done) go stream(s.conn, requests, errors, done)
for { for {
select { select {
case request := <-requests: case request := <-requests:
if err := handleRequest(conn, request); err != nil { if err := s.handleRequest(request); err != nil {
error_log.Printf("client error: %v", err) error_log.Printf("client error: %v", err)
} }
case err := <-errors: case err := <-errors:
@ -78,6 +99,9 @@ func serve() {
error_log.Printf("error accepting new connection: %v", err) error_log.Printf("error accepting new connection: %v", err)
continue continue
} }
go handleConnection(conn) srvConn := serverConnection{
conn: conn,
}
go srvConn.run()
} }
} }

Loading…
Cancel
Save