Ejemplo n.º 1
0
func main() {
	sys_user, err := user.Current()
	sys_username := ""
	if err == nil {
		sys_username = sys_user.Username
	}
	username := flag.String("u", sys_username, "Username on FrankController. Defaults to logged in user's username.")
	flag.Parse()

	ps := HashPass(*password)

	log.Printf("Connecting to: %s", *address)

	conn, err = frank.NewFrankConn(*address, *username, ps, *newAcc)
	if err != nil {
		log.Fatalf("Error on connect: %s", err)
	}
	log.Println("Connection established!")

	go func() {
		for sp := range conn.Incoming {
			switch sp.CommandType {
			case frank.KICKED:
				log.Printf("%s kicked!", sp.Data)
			case frank.BANNED:
				log.Printf("%s banned!", sp.Data)
			case frank.SERVER_SHUTDOWN:
				log.Println("Server is shutting down!")
				os.Exit(0)
			case frank.MESSAGE:
				messageParser(sp.Data)
			case frank.SONGUPDATE:
				state.Current_Update(sp.NowPlaying)
			case frank.CLEAR_PLAYLIST:
				state.Playlist = make([]string, 0, 25)
			case frank.USER_DISCONNECT:
				state.Users_Remove(sp.Data)
			case frank.USER_CONNECT:
				state.Users_Add(sp.Data)
			case frank.FULL_UPDATE:
				state.Full_Update(sp)
				log.Println("Full update recived!")
			case frank.ADDNEWSONG:
				state.Playlist = append(state.Playlist, sp.Data)
			case frank.YOURADMIN:
				log.Println("You are an Admin!")
			case frank.ADMINLOG:
				log.Printf("Admin: %s", sp.Data)
			case frank.PING:
				conn.SendClientPackage(conn.MakeClientPackage(frank.PONG, ""))
			case frank.STARTVOTE:
				state.SetVoting(sp)
			default:
				log.Printf("%d: %s", sp.CommandType, sp.Data)
			}
		}

	}()

	if *webserver != "" {
		err := ListenAndServe(*webserver, func() interface{} { return state })
		log.Printf("Error: %s\n", err)
	}

	time.Sleep(2 * time.Second)
	conn.Close()
}
Ejemplo n.º 2
0
func handleUser(user io.ReadWriteCloser) {
	closing := false
	defer func() {
		user.Write(clientDisconnectPackage)
		closing = true
		user.Close()
	}()

	dec := json.NewDecoder(user)
	username, password, newAcc := "", "", false

	for username == "" {
		cp := new(frank.ClientPacket)
		dec.Decode(cp)
		//fmt.Println("cp:", cp)
		username, password = cp.Username, cp.Password
		if cp.CommandType == frank.NEWACCOUNT {
			newAcc = true
		}
	}
	server, err := frank.NewFrankConn(*frankAddr, username, HashPass(password), newAcc)

	defer func() {
		if server != nil {
			server.Close()
		}
	}()

	if err != nil {
		return
	}

	go func() {
		for sp := range server.Incoming {
			if sp.CommandType == frank.PING {
				cp := server.MakeClientPackage(frank.PONG, "")
				server.SendClientPackage(cp)
				b, _ := json.Marshal(sp)
				_, err := user.Write(b)
				if err != nil {
					closing = true
					return
				}
				continue
			}
			b, _ := json.Marshal(sp)
			_, err := user.Write(b)
			if err != nil || closing {
				closing = true
				return
			}
		}
	}()

	for {
		if closing {
			return
		}
		cp := new(frank.ClientPacket)
		err := dec.Decode(cp)
		if err != nil {
			closing = true
			return
		}
		if cp.CommandType != 0 {
			cp = server.MakeClientPackage(cp.CommandType, cp.Data)
			server.SendClientPackage(cp)
		}
	}
}