@@ -16,7 +16,7 @@ import (
1616var Logger * log.Logger
1717
1818// ParseHeader parses the fist line of the file
19- func ParseHeader (input io.Reader ) {
19+ func ParseHeader (input io.Reader ) ([]types. Endpoint , [] * types. Video , [] * types. CacheServer , []types. RequestDescription ) {
2020 var videosCount int
2121 var endpointsCount int
2222 var requestsDescriptionsCount int
@@ -42,32 +42,52 @@ func ParseHeader(input io.Reader) {
4242 & capacity ,
4343 )
4444
45+ serverTemplate := types.CacheServer {
46+ Capacity : capacity ,
47+ }
48+
49+ var servers []* types.CacheServer
50+ for i := 0 ; i < cacheServersCount ; i ++ {
51+ serverTemplateCopy := serverTemplate
52+ servers = append (servers , & serverTemplateCopy )
53+ }
54+
55+ for _ , server := range servers {
56+ server = new (types.CacheServer )
57+ server .Capacity = capacity
58+ }
59+
4560 scanner .Scan ()
4661 videos := parseVideos (scanner .Text ())
4762 parseVideos (scanner .Text ())
4863
4964 for i := 0 ; i < endpointsCount ; i ++ {
50- endpoints = append (endpoints , parseEndpoint (scanner ))
65+ endpoints = append (endpoints , parseEndpoint (scanner , servers ))
5166 }
5267
5368 for i := 0 ; i < requestsDescriptionsCount ; i ++ {
5469 requests = append (requests , parseRequest (scanner , videos , endpoints ))
5570 }
71+
72+ return endpoints , videos , servers , requests
5673}
5774
58- func parseVideos (line string ) []types.Video {
59- var videos []types.Video
75+ func parseVideos (line string ) []* types.Video {
76+ var videos []* types.Video
6077 sizes := strings .Split (line , " " )
6178
62- for _ , size := range sizes {
79+ for i , size := range sizes {
6380 sizeInt , _ := strconv .ParseInt (size , 10 , 32 )
64- videos = append (videos , types.Video {Size : int (sizeInt )})
81+ videos = append (videos , & types.Video {
82+ ID : i ,
83+ Size : int (sizeInt ),
84+ })
6585 }
6686
6787 return videos
6888}
6989
70- func parseEndpoint (scanner * bufio.Scanner ) types.Endpoint {
90+ func parseEndpoint (scanner * bufio.Scanner , servers [] * types. CacheServer ) types.Endpoint {
7191 var connections int
7292 var latency int
7393
@@ -77,15 +97,17 @@ func parseEndpoint(scanner *bufio.Scanner) types.Endpoint {
7797
7898 endpoint := types.Endpoint {
7999 Latency : latency ,
80- Connections : make ([]types.Connection , connections ),
100+ Connections : make ([]types.Connection , 0 ),
81101 }
82102
83103 for i := 0 ; i < connections ; i ++ {
84104 connection := types.Connection {}
105+ var connectionID int
85106
86107 scanner .Scan ()
87108 attributes := scanner .Text ()
88- fmt .Sscanf (attributes , "%d %d" , & connection .ID , & connection .CacheLatency )
109+ fmt .Sscanf (attributes , "%d %d" , & connectionID , & connection .CacheLatency )
110+ connection .Server = servers [connectionID ]
89111 endpoint .Connections = append (endpoint .Connections , connection )
90112 }
91113
@@ -94,7 +116,7 @@ func parseEndpoint(scanner *bufio.Scanner) types.Endpoint {
94116
95117func parseRequest (
96118 scanner * bufio.Scanner ,
97- videos []types.Video ,
119+ videos []* types.Video ,
98120 endpoints []types.Endpoint ,
99121) types.RequestDescription {
100122 var videoID int
@@ -104,11 +126,11 @@ func parseRequest(
104126 scanner .Scan ()
105127 line := scanner .Text ()
106128 fmt .Sscanf (line , "%d %d %d" , & videoID , & endpointID , & amount )
107- Logger .Debugf ("[REQUEST] Amount: %d | Video: %d | Endpoint: %d" , amount , videoID , endpointID )
129+ // Logger.Debugf("[REQUEST] Amount: %d | Video: %d | Endpoint: %d", amount, videoID, endpointID)
108130
109131 return types.RequestDescription {
110132 Amount : amount ,
111- Video : & videos [videoID ],
133+ Video : videos [videoID ],
112134 Source : & endpoints [endpointID ],
113135 }
114136}
0 commit comments