From 70a94abbdc0c9f323d790823509acdb5b1a33a2d Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Thu, 1 Aug 2019 15:19:32 +0100 Subject: go implemenntation --- go/mixed-server.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/go/mixed-server.go b/go/mixed-server.go index fd44591..9476cc2 100644 --- a/go/mixed-server.go +++ b/go/mixed-server.go @@ -1,15 +1,18 @@ package main import ( + "bufio" "fmt" + "io" + "net" "net/http" "net/url" + "regexp" "os" "os/signal" "sort" "strings" "sync" - "io" ) type wordsByCount struct { @@ -109,7 +112,7 @@ func NewHttpController(m Model, v View) *http.ServeMux { strings.NewReader(table + "\n").WriteTo(w) } else if req.Method == "POST" { var body strings.Builder - io.Copy(&body,req.Body) + io.Copy(&body, req.Body) words := v.WordsFromInput(body.String()) m.AddWords(words) } @@ -117,12 +120,81 @@ func NewHttpController(m Model, v View) *http.ServeMux { return controller } +type LineController struct {m Model; v View} +func NewLineController(m Model, v View) LineController { + return LineController{m,v} +} +func (l *LineController) OnCommand(w io.Writer, command string, args string) { + if command == "get" { + l.getWords(w,args) + } else if command == "put" { + l.putWords(w,args) + } else { + l.reply(w,"bad command, only 'get' and 'put'") + } +} +func (l *LineController) reply(w io.Writer, text string) { + strings.NewReader(text + "\n").WriteTo(w) +} +func (l *LineController) getWords(w io.Writer, args string) { + var how_many uint + fmt.Sscanf(args, "%d", &how_many) + if how_many == 0 { + how_many = 10 + } + most_common_pairs := l.m.GetMostCommonPairs(how_many) + table := l.v.TableFromRankedPairs(most_common_pairs) + l.reply(w,table) +} +func (l *LineController) putWords(w io.Writer, args string) { + words := l.v.WordsFromInput(args) + l.m.AddWords(words) + l.reply(w,"ok") +} + +type TextServer struct { + c LineController + l net.Listener +} +func TextListenAndServe(host string, controller LineController) error { + l, err := net.Listen("tcp",host) + if err != nil { + return err + } + ts := TextServer{controller,l} + ts.Serve() + return nil +} +func (ts *TextServer) Serve() { + for conn,err := ts.l.Accept(); err == nil; conn,err = ts.l.Accept() { + go ts.serveOne(conn) + } +} +func (ts *TextServer) serveOne(conn net.Conn) { + split := regexp.MustCompile(`\s+`) + s := bufio.NewScanner(conn) + for s.Scan() { + parts := split.Split(s.Text(),2) + if len(parts)>1 { + ts.c.OnCommand(conn,parts[0],parts[1]) + } else if len(parts)==1 { + ts.c.OnCommand(conn,parts[0],"") + } + } + + conn.Close() + return +} + func main() { model := NewModel() view := NewView() httpcontroller := NewHttpController(model, view) go http.ListenAndServe("localhost:8080", httpcontroller) + linecontroller := NewLineController(model,view) + go TextListenAndServe("localhost:2020",linecontroller) + sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt) <-sigs -- cgit v1.2.3