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)
	}
}
示例#3
0
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() {
	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:]...)
		}
	}
}
示例#5
0
func ConnectionClosed(s *TcpSocket) {
	*events.GetClosedSocketsChan() <- events.SocketClosedEvent{&s.SomeSocket}
	s.SomeSocket.CloseEvent <- &events.SocketClosedEvent{&s.SomeSocket}
}