func ListenAndServer(host string, ns *notification.Sender) { notificationSender = ns l, err := net.Listen("tcp", host) if err != nil { log.Println("TcpSocket Error listening:", err.Error()) os.Exit(1) } println("Starting tcp server...") defer l.Close() defer close(*events.GetNewVoteChan()) defer close(*events.GetVoteUpdateChan()) defer close(*events.GetNewSocketsChan()) defer close(*events.GetClosedSocketsChan()) go ListenToEvents() for { conn, err := l.Accept() if err != nil { log.Println("TcpSocket Error accepting: ", err.Error()) os.Exit(1) } go HandleNewConnection(conn) } }
func ListenToEvents() { clients := make([]*events.SomeSocket, 0, 1024) clientsCounter := 0 for { select { case newVoteEvent := <-*events.GetNewVoteChan(): for _, client := range clients { client.NewVoteEvent <- &newVoteEvent } case voteUpdateEvent := <-*events.GetVoteUpdateChan(): for _, client := range clients { client.VoteUpdEvent <- &voteUpdateEvent } case newSocketEvent := <-*events.GetNewSocketsChan(): clientsCounter++ newSocketEvent.Socket.Id = strconv.Itoa(clientsCounter) clients = append(clients, newSocketEvent.Socket) case socketClosedEvent := <-*events.GetClosedSocketsChan(): id := socketClosedEvent.Socket.Id index := 0 for i, socket := range clients { if socket.Id == id { index = i } } clients = append(clients[:index], clients[index+1:]...) } } }
func ListenAndServer(host string, ns *notification.Sender, dbconn string) { notificationSender = ns dbConnectionAddress = dbconn l, err := net.Listen("tcp", host) if err != nil { log.Error.Printf("TcpSocket Error listening: %s\n", err.Error()) os.Exit(1) } defer l.Close() defer close(*events.GetNewVoteChan()) defer close(*events.GetVoteUpdateChan()) defer close(*events.GetNewSocketsChan()) defer close(*events.GetClosedSocketsChan()) go ListenToEvents() for { conn, err := l.Accept() if err != nil { log.Error.Printf("TcpSocket Error accepting: %s\n", err.Error()) os.Exit(1) } go handleNewConnection(conn) } }
func doVote(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { funcPrefix := "Processing voting" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) log.Debug.Printf("%s: authenticating user...\n", funcPrefix) user, error := authenticate(r.Header.Get("auth_token")) if error != nil { log.Error.Printf("%s: user authentication failed\n", funcPrefix) w.WriteHeader(400) return } id := ps.ByName("id") log.Debug.Printf("%s: getting vote with id '%s' from storage...\n", funcPrefix, id) vote, err := storage.GetVote(id) if err != nil { log.Error.Printf("%s: getting vote with id '%s' from storage failed: %s\n", funcPrefix, id, err.Error()) w.WriteHeader(400) return } var params DoVotePrm log.Debug.Printf("%s: decoding params...\n", funcPrefix) err = json.NewDecoder(r.Body).Decode(¶ms) if err != nil { log.Error.Printf("%s: decoding params failed: %s\n", funcPrefix, err.Error()) w.WriteHeader(400) return } log.Debug.Printf("%s: modifying vote...\n", funcPrefix) storage.VoteProcessing(*vote, *user, params.Value) log.Debug.Printf("%s: getting vote result status...\n", funcPrefix) res := storage.GetVoteResultStatus(*vote, *user) log.Debug.Printf("%s: sending vote update event...\n", funcPrefix) *events.GetVoteUpdateChan() <- events.VoteUpdateEvent{res} log.Info.Printf("%s: vote '%s' has been succesfully updated!\n", funcPrefix, vote.Name) w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "*") if json.NewEncoder(w).Encode(res) != nil { log.Error.Printf("%s: encoding response failed\n", funcPrefix) w.WriteHeader(500) } }
func (s *TcpSocket) handleVoteFor(packet *PulsePacket) { funcPrefix := "Processing voting" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) storageConnection := storage.NewStorageConnection(dbConnectionAddress) defer storageConnection.CloseStorageConnection() var params CSVoteForRequest log.Debug.Printf("%s: unmarshaling params...\n", funcPrefix) err := json.Unmarshal(packet.content, ¶ms) if err != nil { log.Error.Printf("%s: unmarshaling params failed: %s\n", funcPrefix, err.Error()) return } log.Debug.Printf("%s: getting vote with id '%s' from storage...\n", funcPrefix, params.Id) vote, err := storageConnection.GetVote(params.Id) if err != nil { log.Error.Printf("%s: getting vote with id '%s' from storage failed: %s\n", funcPrefix, params.Id, err.Error()) return } if storageConnection.IsVotedByUser(*vote, s.user) { log.Warning.Printf("%s: user has already voted!\n", funcPrefix) return } log.Debug.Printf("%s: modifying vote...\n", funcPrefix) storageConnection.VoteProcessing(*vote, s.user, params.ColorId) log.Debug.Printf("%s: getting vote result status...\n", funcPrefix) res, err1 := storageConnection.GetVoteResultStatus(*vote, s.user, storageConnection.UsersCount()) if err1 != nil { log.Error.Printf("%s: getting vote result status failed: %s\n", funcPrefix, err.Error()) return } log.Debug.Printf("%s: sending vote update event...\n", funcPrefix) *events.GetVoteUpdateChan() <- events.VoteUpdateEvent{res} log.Info.Printf("%s: vote '%s' has been succesfully updated!\n", funcPrefix, vote.Name) }
func (s *TcpSocket) handleVoteFor(packet *PulsePucket) { var params CSVoteForRequest err := json.Unmarshal(packet.content, ¶ms) if err != nil { fmt.Println(err) return } vote, err := storage.GetVote(params.Id) if err != nil { fmt.Println(err) return } storage.VoteProcessing(*vote, s.user, params.ColorId) res := storage.GetVoteResultStatus(*vote, s.user) *events.GetVoteUpdateChan() <- events.VoteUpdateEvent{res} }
func ListenToEvents() { funcPrefix := "Processing socket managing events" log.Debug.Printf("%s: start\n", funcPrefix) defer log.Debug.Printf("%s: end\n", funcPrefix) clients := make([]*events.SomeSocket, 0, 1024) clientsCounter := 0 for { select { case newVoteEvent := <-*events.GetNewVoteChan(): log.Debug.Printf("%s: handling vote create event\n", funcPrefix) for _, client := range clients { client.NewVoteEvent <- &newVoteEvent } case voteUpdateEvent := <-*events.GetVoteUpdateChan(): log.Debug.Printf("%s: handling vote update event\n", funcPrefix) for _, client := range clients { client.VoteUpdEvent <- &voteUpdateEvent } case newSocketEvent := <-*events.GetNewSocketsChan(): log.Debug.Printf("%s: handling socket connection create event\n", funcPrefix) clientsCounter++ newSocketEvent.Socket.Id = strconv.Itoa(clientsCounter) clients = append(clients, newSocketEvent.Socket) case socketClosedEvent := <-*events.GetClosedSocketsChan(): log.Debug.Printf("%s: handling socket connection close event\n", funcPrefix) id := socketClosedEvent.Socket.Id index := 0 for i, socket := range clients { if socket.Id == id { index = i } } clients = append(clients[:index], clients[index+1:]...) } } }