1 module libsweatyballs.router.core;
2 
3 import libsweatyballs.link.core : Link;
4 import libsweatyballs.security.identity : Identity;
5 import libsweatyballs.router.table : Table, Route;
6 import core.thread : Thread, dur;
7 import core.sync.mutex : Mutex;
8 import libsweatyballs.router.advertiser : Advertiser;
9 import libsweatyballs.engine.core : Engine;
10 import gogga;
11 import std.conv : to;
12 
13 /**
14 * Router
15 *
16 * Description: TODO
17 */
18 public final class Router : Thread
19 {
20     private Identity identity;
21     private Table routingTable;
22 
23     private Advertiser advertiser;
24 
25     private Engine engine;
26 
27     this(Engine engine, Identity identity)
28     {
29         /* Set the thread's worker function */
30         super(&worker);
31 
32         
33 
34         this.engine = engine;
35         this.identity = identity;
36 
37         /* Create a new routing table */
38         routingTable = new Table();
39 
40         /* Initialize the advertiser */
41         initAdvertiser();
42     }
43 
44     
45 
46     private void initAdvertiser()
47     {
48         advertiser = new Advertiser(this);
49     }
50 
51     private void worker()
52     {
53         /* TODO: Implement me */
54 
55         
56         while(true)
57         {
58             // /* Cycle through the in queue of each link */
59             // Link[] links = getLinks();
60             // foreach(Link link; links)
61             // {
62             //     /* Check if the in-queue has anything in it */
63             //     if(link.hasInQueue())
64             //     {
65             //         Message message = link.popInQueue();
66             //         process(message);
67             //     }
68             // }
69 
70             // process(null);
71 
72             /* Check if any routes need to be expired */
73             checkRouteExpiration();
74 
75             
76             string routeInfo;
77             Route[] routes = routingTable.getRoutes();
78             foreach(Route route; routes)
79             {
80                 routeInfo ~= routeInfo ~ to!(string)(route) ~ "\n";
81             }
82 
83             gprintln("<<<<<<< Routing table state "~getIdentity().getKeys().publicKey~">>>>>>>\n"~routeInfo);
84 
85 
86             sleep(dur!("seconds")(5));
87         }
88     }
89 
90     private void checkRouteExpiration()
91     {
92         Route[] routes = routingTable.getRoutes();
93         foreach(Route route; routes)
94         {
95             if(route.isExpired())
96             {
97                 routingTable.removeRoute(route);
98                 gprintln("Expired route "~to!(string)(route)~", removing...", DebugType.WARNING);
99             }
100         }
101     }
102 
103     
104 
105     public Engine getEngine()
106     {
107         return engine;
108     }
109 
110     public Table getTable()
111     {
112         return routingTable;
113     }
114 
115     public Identity getIdentity()
116     {
117         return identity;
118     }
119 
120     public void launch()
121     {
122         start();
123 
124         /* Launch the routes advertiser */
125         advertiser.launch();
126     }
127 }