From 3be3b5e1565d528a39b9dfa74677ddd100dba60a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 26 Apr 2018 23:39:43 +0200 Subject: [PATCH] add dispatch(net.Conn, *upgrader.T) to dispatch client into controller --- ws/private.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ws/private.go diff --git a/ws/private.go b/ws/private.go new file mode 100644 index 0000000..2ff3a9f --- /dev/null +++ b/ws/private.go @@ -0,0 +1,78 @@ +package ws + +import ( + "git.xdrm.io/gws/upgrader" + "net" +) + + +// dispatch finds a controller for a client +func (s *Server) dispatch(sock net.Conn, u *upgrader.T){ + + /* (1) Build client + ---------------------------------------------------------*/ + /* (1) Add socket */ + client := new(Client) + client.soc = sock + + /* (2) Add uri */ + uri := u.Request.GetURI() + client.arg = make([][]string, 1) + client.arg[0] = []string{ uri } + + /* (3) Add protocol */ + client.pro = string(u.Response.GetProtocol()) + + + /* (2) Search for a controller (from URI) + ---------------------------------------------------------*/ + var controller *Controller = nil + + /* (1) Iterate over URI controllers */ + for _, c := range s.ctl { + + /* (2) If matches */ + if c.uri.Match(uri) { + + /* (3) Extract matches */ + match := c.uri.GetAllMatch() + + /* (4) Add them to the 'arg' attribute */ + for _, m := range match { + client.arg = append(client.arg, m) + } + + /* (5) Mark that we have a controller */ + controller = c + break + + } + + } + + /* (6) If no controller found */ + if controller == nil && s.def != nil { + controller = s.def + } + + /* (7) If no controller -> close socket */ + if controller == nil { + sock.Close() + return + } + + + /* (3) Manage client+controller + ---------------------------------------------------------*/ + /* (1) Add client to server */ + s.cli = append(s.cli, client) + + /* (2) Add controller to client */ + client.ctl = controller + + /* (3) Launch controller routine */ + go controller.fun(client, nil) + + return + +} \ No newline at end of file