Ejemplo n.º 1
0
func main() {
	Server := new(server.Server)
	myDb := new(db.Env)

	if len(os.Args) != 5 {
		fmt.Println("./Usage 'name bdd' 'pass bdd' 'user bdd' 'port bdd'")
		return
	}
	er := Server.InitServer()
	if er != nil {
		fmt.Println("Error on InitServer: ", er)
		return
	}
	Db, er := myDb.OpenCo(os.Args)
	if er != nil {
		Server.Logger.Println("OpenCo error: ", er)
		return
	}
	er = Server.Init_Data(myDb)
	if er != nil {
		Server.Logger.Println("Init_Data error: ", er)
		return
	}
	go ManageSignal(Server, myDb)
	go Manage_goroutines(Server, myDb)
	go sock.Listen(Server, Db)

	for {
		time.Sleep(time.Second * 60)
	}
}
Ejemplo n.º 2
0
func (Lstb *All_ball) SetFollowerBalls(curr_b *Ball, base *db.Env) {
	var idB int64
	base.Db.QueryRow("SELECT id FROM container WHERE ianix=$1;", curr_b.Id_ball).Scan(&idB)
	row, er := base.Db.Query("DELETE FROM followed WHERE container_id=$1", idB)
	if er != nil {
		Lstb.Logger.Println("Error on Delete followed: ", er)
	} else {
		defer row.Close()
	}
	for f := curr_b.Followers.Front(); f != nil; f = f.Next() {
		err := base.Transact(base.Db, func(tx *sql.Tx) error {
			stm, err := tx.Prepare("INSERT INTO followed(container_id, iduser) values($1,$2)")
			if err != nil {
				Lstb.Logger.Println(err)
				return err
			}
			defer stm.Close()
			_, err = stm.Exec(idB, f.Value.(*list.Element).Value.(*users.User).Id)
			if err != nil {
				Lstb.Logger.Println(err)
				return err
			}
			return nil
		})
		if err != nil {
			Lstb.Logger.Println(err)
		}
	}
}
Ejemplo n.º 3
0
func (ball *Ball) addMessage(base *db.Env) error {
	var idB int64
	base.Db.QueryRow("SELECT id FROM container WHERE ianix=$1;", ball.Id_ball).Scan(&idB)
	idMessageMax, er := getIdMessageMax(idB, base)
	if er != nil {
		return er
	}
	for f := ball.Messages.Front(); f != nil; f = f.Next() {
		mes := f.Value.(Message)
		if mes.Id > idMessageMax {
			err := base.Transact(base.Db, func(tx *sql.Tx) error {
				stm, err := tx.Prepare("INSERT INTO message(content, containerid, index_m, size) VALUES ($1, (SELECT id FROM container WHERE ianix=$2), $3, $4)")
				if err != nil {
					return err
				}
				defer stm.Close()
				_, err = stm.Exec(strings.Trim(mes.Content, "\x00"), ball.Id_ball, f.Value.(Message).Id, f.Value.(Message).Size)
				return err
			})
			if err != nil {
				return er
			}
		}
	}
	return nil
}
Ejemplo n.º 4
0
func (b *Ball) UpdateLocation(base *db.Env) error {
	var idB int64
	base.Db.QueryRow("SELECT id FROM container WHERE ianix=$1;", b.Id_ball).Scan(&idB)
	id := int64(0)
	if b.Possessed != nil {
		id = b.Possessed.Value.(*users.User).Id
	}

	err := base.Transact(base.Db, func(tx *sql.Tx) error {
		stm, err := tx.Prepare(" SELECT setdatacontainer($1, $2, $3, $4, $5, $6, $7, $8)")
		if err != nil {
			return err
		}
		defer stm.Close()
		_, err = stm.Exec(b.Wind.Degress, b.Wind.Speed, b.Coord.Value.(Checkpoint).Coord.Lat, b.Coord.Value.(Checkpoint).Coord.Lon, idB, id, b.Coord.Value.(Checkpoint).Date, b.Coord.Value.(Checkpoint).MagnetFlag)
		if err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 5
0
func TestUsers(t *testing.T) {

	var err error
	Lst_users := new(users.All_users)
	Lst_ball := new(ballon.All_ball)
	myDb := new(db.Env)
	Lst_users.Ulist = list.New()
	Lst_ball.Blist = list.New()
	Db, err := myDb.OpenCo(err)
	user1 := new(users.User)
	user1.Mail = "*****@*****.**"
	user1.Log = time.Now()
	user1.Followed = list.New()
	user1.Stats = &users.StatsUser{}
	user1.Stats.CreationDate = time.Now()
	user1.Coord.Lat = 48.833086
	user1.Coord.Lon = 2.316055
	user1.Log = time.Now()
	Lst_users.Ulist.PushBack(user1)

	fmt.Println(result)
}
Ejemplo n.º 6
0
func (Lst_ball *All_ball) InsertMessages(messages *list.List, idBall int64, base *db.Env) (err error) {
	i := 0
	for e := messages.Front(); e != nil; e = e.Next() {
		err = base.Transact(base.Db, func(tx *sql.Tx) error {
			stm, err := tx.Prepare("INSERT INTO message(content, containerid, index_m, size) VALUES ($1, $2, $3, $4)")
			if err != nil {
				Lst_ball.Logger.Println("Erreur tx prepare: ", err)
				return err
			}
			defer stm.Close()
			row, err := stm.Query(sanit.Accents(strings.Trim(e.Value.(Message).Content, "\x00")), idBall, i, e.Value.(Message).Size)
			if err != nil {
				Lst_ball.Logger.Println("Erreur Query: ", err)
				return err
			}
			defer row.Close()
			i++
			return err
		})
	}
	return nil
}
Ejemplo n.º 7
0
func TestBallon(t *testing.T) {

	var err error

	Lst_users := new(users.All_users)
	Lst_ball := new(ballon.All_ball)
	myDb := new(db.Env)
	Lst_users.Ulist = list.New()
	Lst_ball.Blist = list.New()
	Db, err := myDb.OpenCo(err)
	Lst_ball.Get_balls(Lst_users, myDb)
	fmt.Println(Db)

	user1 := new(users.User)
	user1.Id = 125
	user1.Mail = "*****@*****.**"
	user1.Log = time.Now()
	user1.Followed = list.New()
	user1.Stats = &users.StatsUser{}
	user1.Stats.CreationDate = time.Now()
	user1.Coord.Lat = 48.833086
	user1.Coord.Lon = 2.316055
	user1.Log = time.Now()
	Lst_users.Ulist.PushBack(user1)

	createBall1000(Lst_ball, Lst_users.Ulist.Front(), myDb)
	/* CREER UN BALLON POUR FAIRE DES TESTS */

	// tmp_lst := list.New()
	// var check_test0 ballon.Checkpoint
	// check_test0.Coord.Lon = 48.833086
	// check_test0.Coord.Lat = 2.316055
	// check_test0.Date = time.Now()

	// var check_test1 ballon.Checkpoint
	// check_test1.Coord.Lon = 48.833586
	// check_test1.Coord.Lat = 2.316065
	// check_test1.Date = time.Now()

	// var check_test2 ballon.Checkpoint
	// check_test2.Coord.Lon = 48.833368
	// check_test2.Coord.Lat = 2.316059
	// check_test2.Date = time.Now()

	// var check_test3 ballon.Checkpoint
	// check_test3.Coord.Lon = 48.833286
	// check_test3.Coord.Lat = 2.316903
	// check_test3.Date = time.Now()

	// var check_test4 ballon.Checkpoint
	// check_test4.Coord.Lon = 48.833986
	// check_test4.Coord.Lat = 2.316045
	// check_test4.Date = time.Now()

	// lmessages := list.New()
	// listMessage1 := list.New()
	// var message0 ballon.Message
	// message0.Id = 0
	// message0.Size = 68
	// message0.Content = "Mensaje 1 test"
	// message0.Type = 1
	// var message1 ballon.Message
	// message1.Id = 0
	// message1.Size = 68
	// message1.Content = "Mensaje 2 test"
	// message1.Type = 1
	// var message2 ballon.Message
	// message2.Id = 2
	// message2.Size = 68
	// message2.Content = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean com"
	// message2.Type = 1

	// listMessage1.PushBack(message0)
	// lmessages.PushBack(message0)
	// lmessages.PushBack(message1)
	// lmessages.PushBack(message2)

	// ball0 := new(ballon.Ball)
	// ball0.Id_ball = 45
	// ball0.Title = "toto"
	// ball0.Coord = tmp_lst.PushBack(check_test0)
	// ball0.Wind = ballon.Wind{}
	// ball0.Messages = listMessage1
	// ball0.Date = time.Now()
	// ball0.Checkpoints = list.New()
	// ball0.Possessed = nil
	// ball0.Followers = list.New()
	// ball0.Creator = nil
	// Lst_ball.Blist.PushBack(ball0)

	// ball1 := new(ballon.Ball)
	// ball1.Id_ball = 1
	// ball1.Title = "tata"
	// ball1.Coord = tmp_lst.PushBack(check_test1)
	// ball1.Wind = ballon.Wind{}
	// ball1.Messages = listMessage1
	// ball1.Date = time.Now()
	// ball1.Checkpoints = list.New()
	// ball1.Possessed = nil
	// ball1.Followers = list.New()
	// ball1.Creator = nil
	// Lst_ball.Blist.PushBack(ball1)

	// ball2 := new(ballon.Ball)
	// ball2.Id_ball = 2
	// ball2.Title = "tutu"
	// ball2.Coord = tmp_lst.PushBack(check_test2)
	// ball2.Wind = ballon.Wind{}
	// ball2.Messages = listMessage1
	// ball2.Date = time.Now()
	// ball2.Checkpoints = list.New()
	// ball2.Possessed = nil
	// ball2.Followers = list.New()
	// ball2.Creator = nil
	// Lst_ball.Blist.PushBack(ball2)

	// ball3 := new(ballon.Ball)
	// ball3.Id_ball = 3
	// ball3.Title = "tete"
	// ball3.Coord = tmp_lst.PushBack(check_test3)
	// ball3.Wind = ballon.Wind{}
	// ball3.Messages = listMessage1
	// ball3.Date = time.Now()
	// ball3.Checkpoints = list.New()
	// ball3.Possessed = nil
	// ball3.Followers = list.New()
	// ball3.Creator = nil
	// Lst_ball.Blist.PushBack(ball3)

	// Ball4 := new(ballon.Ball)
	// Ball4.Id_ball = 63
	// Ball4.Title = "tyty"
	// Ball4.Edited = true
	// Ball4.Coord = tmp_lst.PushBack(check_test4)
	// Ball4.Wind = ballon.Wind{Speed: 32, Degress: 2}
	// Ball4.Messages = listMessage1
	// Ball4.Date = time.Now()
	// Ball4.Checkpoints = tmp_lst
	// Ball4.Possessed = Lst_users.Ulist.Front()
	// Ball4.Followers = list.New()
	// Ball4.Creator = Lst_users.Ulist.Front()
	// Lst_ball.Blist.PushBack(Ball4)
	// if _, err := Lst_ball.InsertBallon(Ball4, myDb); err != nil {
	// 	t.Fatalf("Fail insert ball:%s", err)
	// }

	// if _, err :=	ball4.GetItinerary(myDb.Db); err != nil {
	// 	t.Fatalf("Fail get GetItinerary error: %s", err)
	// }
	// Lst_ball.Update_balls(Lst_ball, myDb)
	// fmt.Println("\x1b[31;1m SECOND PRINT ALL BALLS\x1b[0m")
	//Lst_ball.Print_all_balls()

}
Ejemplo n.º 8
0
func TestBallon(t *testing.T) {

	var err error

	Lst_users := new(users.All_users)
	Lst_ball := new(ballon.All_ball)
	Lprotocol := new(protocol.Request)
	Ldevices := new(devices.All_Devices)
	myDb := new(db.Env)
	Lst_users.Ulist = list.New()
	Lst_ball.Blist = list.New()
	Db, err := myDb.OpenCo(err)
	Lst_ball.Get_balls(Lst_users, myDb)
	fmt.Println(Db)

	Lprotocol.Coord.Lon = 48.833986
	Lprotocol.Coord.Lat = 2.316045
	// user1 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user2 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user3 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user4 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user5 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user6 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()
	// user7 := new(users.User)
	// 	user1.Id = 68
	// 	user1.Mail = "*****@*****.**"
	// 	user1.Log = time.Now()
	// 	user1.Followed = list.New()

	// 	Lst_users.Ulist.PushBack(user1)
	// 	Lst_users.Ulist.PushBack(user1)
	// 	Lst_users.Ulist.PushBack(user1)
	// 	Lst_users.Ulist.PushBack(user1)
	// 	Lst_users.Ulist.PushBack(user1)
	Lst_users.Get_users(myDb.Db)
	Ldevices.Get_devices(Lst_users, myDb)

}
Ejemplo n.º 9
0
func (Lb *All_ball) GetListBallsByUser(userE *list.Element, base *db.Env, Ulist *list.List) (lBallon *list.List, err error) {
	lBallon = list.New()
	err = nil

	err = base.Transact(base.Db, func(tx *sql.Tx) error {
		var errT error
		stm, errT := tx.Prepare("SELECT getcontainersbyuserid($1);")
		if errT != nil {
			Lb.Logger.Println(errT)
			return errT
		}
		defer stm.Close()
		rows, errT := stm.Query(userE.Value.(*users.User).Id)
		if errT != nil {
			Lb.Logger.Println(err)
			return errT
		}
		defer rows.Close()
		for rows.Next() {
			var infoCont string
			errT = rows.Scan(&infoCont)
			if errT != nil {
				Lb.Logger.Println(err)
				return errT
			}
			result := strings.Split(infoCont, ",")

			idBall := GetIdBall(result[0])
			magnet, _ := strconv.Atoi(result[8])
			tempCord := Lb.getExtraInfo(result[6], Lb.GetDateFormat(result[9]), int16(magnet))
			lstIt := list.New()
			sStat, errT := Lb.GetStatsBallon(int64(idBall), base.Db)
			if errT != nil {
				Lb.Logger.Println("GetStatsBallon: ", errT)
				return errT
			}
			possessed, errT := GetWhomGotBall(idBall, Ulist, base.Db)
			if errT != nil {
				Lb.Logger.Println("GetWhomGotBall error: ", errT)
				return errT
			}
			tmpBall := Lb.Get_ballbyid(GetIdBall(result[7]))
			if tmpBall != nil {
				// Do Nothing
			} else {
				lstMess, errT := Lb.GetMessagesBall(idBall, base.Db)
				if errT != nil {
					Lb.Logger.Println("GetMessageBall error: ", errT)
					return errT
				}
				lstFols, errT := Lb.GetFollowers(idBall, base.Db, Ulist)
				if errT != nil {
					Lb.Logger.Println("GetFollowers error: ", errT)
					return errT
				}
				tr := '"'
				lBallon.PushBack(
					&Ball{
						Id_ball:     GetIdBall(result[7]),
						Title:       strings.Trim(result[1], string(tr)),
						Date:        Lb.GetDateFormat(result[5]),
						Checkpoints: list.New(),
						Itinerary:   lstIt,
						Scoord:      tempCord.Front(),
						Coord:       tempCord.Front(),
						Wind:        GetWin(result[3], result[4]),
						Messages:    lstMess,
						Followers:   lstFols,
						Possessed:   possessed,
						Stats:       sStat,
						Creator:     userE})
			}
		}
		return errT
	})
	if err != nil {
		return nil, err
	}
	return lBallon, nil

}