about summary refs log tree commit diff
path: root/processStars.go
blob: 1bea11c2afb177aea77cefe98ac8afcf2a95d435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main

import (
	"fmt"
	"git.darknebu.la/GalaxySimulator/db-actions"
	"git.darknebu.la/GalaxySimulator/structs"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"time"
)

func processStars() {
	log.Println("[processStars] Initializing the processing process")

	for {
		log.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

		// print the amount of stars allready processed
		printStarsProcessed()

		// get some values such as a starID,
		starID := getStarID()
		star := getStar(starID)
		timestep := getTimestep(starID)

		//force := calcAllForces(star, timestep, theta)
		force := structs.Vec2{10, 10}
		newStar := calcNewPos(star, force, 1e4)

		insertStar(newStar, timestep+1)

		time.Sleep(5 * time.Second)
	}
}

func printStarsProcessed() {
	log.Printf("[   ] Stars processed: %d", starsProcessed)
}

func getStarID() int64 {
	log.Printf("[getStarID] Getting a new starID from the distributor (%s)...", DISTRIBUTORURL)

	log.Println("[getStarID] Preparing the http request")
	requestURL := fmt.Sprintf("http://%s/distributor", DISTRIBUTORURL)
	resp, err := http.Get(requestURL)
	log.Printf("[getStarID] Sent http request to %s\n", requestURL)
	defer resp.Body.Close()
	if err != nil {
		log.Printf("resp: %v", resp)
		panic(err)
	}

	log.Println("[getStarID] Reading the response body")
	body, err := ioutil.ReadAll(resp.Body)
	log.Println("[getStarID] Done Reading the response body")
	if err != nil {
		panic(err)
	}

	log.Printf("[getStarID] Got the response: %v", string(body))
	log.Println("[getStarID] Parsing the integer")
	starID, parseIntErr := strconv.ParseInt(string(body), 10, 64)
	if parseIntErr != nil {
		panic(parseIntErr)
	}
	log.Println("[getStarID] Done parsing the integer")

	return starID
}

func getStar(starID int64) structs.Star2D {
	var star = db_actions.GetStar(db, starID)
	return star
}

func getTimestep(starID int64) int64 {
	var timestep = db_actions.GetStarIDTimestep(db, starID)
	return timestep
}

func calcNewPos(star structs.Star2D, force structs.Vec2, timestep float64) structs.Star2D {
	star.CalcNewPos(force, timestep)
	returnStar := structs.Star2D{
		C: structs.Vec2{
			X: star.C.X,
			Y: star.C.Y,
		},
		V: structs.Vec2{
			X: force.X,
			Y: force.Y,
		},
		M: star.M,
	}
	return returnStar
}

func insertStar(star structs.Star2D, timestep int64) {
	log.Printf("Inserting the star %v into the timestep %d", star, timestep)
}