add dispatch(net.Conn, *upgrader.T) to dispatch client into controller

This commit is contained in:
xdrm-brackets 2018-04-26 23:39:43 +02:00
parent 844bdf14c6
commit 3be3b5e156
1 changed files with 78 additions and 0 deletions

78
ws/private.go Normal file
View File

@ -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
}