|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type hub struct { |
| 10 | + // Registered connections. |
| 11 | + connections map[*connection]bool |
| 12 | + |
| 13 | + // Inbound messages from the connections. |
| 14 | + broadcast chan []byte |
| 15 | + |
| 16 | + // Inbound messages from the system |
| 17 | + broadcastSys chan []byte |
| 18 | + |
| 19 | + // Register requests from the connections. |
| 20 | + register chan *connection |
| 21 | + |
| 22 | + // Unregister requests from connections. |
| 23 | + unregister chan *connection |
| 24 | +} |
| 25 | + |
| 26 | +var h = hub{ |
| 27 | + broadcast: make(chan []byte), |
| 28 | + broadcastSys: make(chan []byte), |
| 29 | + register: make(chan *connection), |
| 30 | + unregister: make(chan *connection), |
| 31 | + connections: make(map[*connection]bool), |
| 32 | +} |
| 33 | + |
| 34 | +func (h *hub) run() { |
| 35 | + for { |
| 36 | + select { |
| 37 | + case c := <-h.register: |
| 38 | + h.connections[c] = true |
| 39 | + case c := <-h.unregister: |
| 40 | + delete(h.connections, c) |
| 41 | + close(c.send) |
| 42 | + case m := <-h.broadcast: |
| 43 | + log.Print("Got a broadcast") |
| 44 | + log.Print(m) |
| 45 | + //log.Print(h.broadcast) |
| 46 | + checkCmd(m) |
| 47 | + log.Print("-----") |
| 48 | + |
| 49 | + for c := range h.connections { |
| 50 | + select { |
| 51 | + case c.send <- m: |
| 52 | + log.Print("did broadcast to ") |
| 53 | + log.Print(c.ws.RemoteAddr()) |
| 54 | + //c.send <- []byte("hello world") |
| 55 | + default: |
| 56 | + delete(h.connections, c) |
| 57 | + close(c.send) |
| 58 | + go c.ws.Close() |
| 59 | + } |
| 60 | + } |
| 61 | + case m := <-h.broadcastSys: |
| 62 | + log.Print("Got a system broadcast") |
| 63 | + log.Print(m) |
| 64 | + log.Print("-----") |
| 65 | + |
| 66 | + for c := range h.connections { |
| 67 | + select { |
| 68 | + case c.send <- m: |
| 69 | + log.Print("did broadcast to ") |
| 70 | + log.Print(c.ws.RemoteAddr()) |
| 71 | + //c.send <- []byte("hello world") |
| 72 | + default: |
| 73 | + delete(h.connections, c) |
| 74 | + close(c.send) |
| 75 | + go c.ws.Close() |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func checkCmd(m []byte) { |
| 83 | + log.Print("Inside checkCmd") |
| 84 | + s := string(m[:]) |
| 85 | + log.Print(s) |
| 86 | + |
| 87 | + sl := strings.ToLower(s) |
| 88 | + |
| 89 | + if strings.HasPrefix(sl, "open") { |
| 90 | + |
| 91 | + args := strings.Split(s, " ") |
| 92 | + if len(args) < 3 { |
| 93 | + go spErr("You did not specify a port and baud rate in your open cmd") |
| 94 | + return |
| 95 | + } |
| 96 | + if len(args[1]) < 1 { |
| 97 | + go spErr("You did not specify a serial port") |
| 98 | + return |
| 99 | + } |
| 100 | + baud, err := strconv.Atoi(args[2]) |
| 101 | + if err != nil { |
| 102 | + go spErr("Problem converting baud rate " + args[2]) |
| 103 | + return |
| 104 | + } |
| 105 | + go spHandlerOpen(args[1], baud) |
| 106 | + |
| 107 | + } else if strings.HasPrefix(sl, "close") { |
| 108 | + |
| 109 | + args := strings.Split(s, " ") |
| 110 | + go spClose(args[1]) |
| 111 | + |
| 112 | + } else if strings.HasPrefix(sl, "send ") { |
| 113 | + |
| 114 | + //args := strings.Split(s, "send ") |
| 115 | + go spWrite(s) |
| 116 | + |
| 117 | + } else if s == "list" { |
| 118 | + go spList() |
| 119 | + //go getListViaWmiPnpEntity() |
| 120 | + } |
| 121 | + |
| 122 | + log.Print("Done with checkCmd") |
| 123 | +} |
0 commit comments