public virtual void Run()
{
int sleep = 60, sleep_min = 60, sleep_max = 3600;
DateTime start_time = DateTime.UtcNow;
// Keep creating new nodes no matter what!
while(_running) {
ApplicationNode node = CreateNode(_node_config);
_app_node = node;
new Information(node.Node, "BasicNode", node.SecurityOverlord);
new LogManager(node.Node);
Console.WriteLine("Starting at {0}, {1} is connecting to {2}.",
DateTime.UtcNow, node.Node.Address, node.Node.Realm);
node.Node.DisconnectOnOverload = true;
start_time = DateTime.UtcNow;
Thread pthread = null;
// Must do this to remove it after successfully creating the new node
Node.StateChangeHandler add_node = null;
if(node.PrivateNode != null) {
// Delayed add, removes ~15 seconds off bootstrapping time
add_node = delegate(Node n, Node.ConnectionState cs) {
if(cs != Node.ConnectionState.Connected) {
return;
}
node.Node.StateChangeEvent -= add_node;
new Information(node.PrivateNode.Node, "PrivateBasicNode", node.SecurityOverlord);
pthread = new Thread(node.PrivateNode.Node.Connect);
pthread.Start();
};
node.Node.StateChangeEvent += add_node;
}
node.Node.Connect();
if(node.PrivateNode != null) {
ApplicationNode pnode = node.PrivateNode;
pnode.Node.Disconnect();
if(pthread != null) {
pthread.Join();
}
}
// Assist in garbage collection
if(_xrm != null) {
_xrm.Remove(node.Node);
}
node = null;
_app_node = null;
// DisconnectOnOverload seems to be having issues with pathing...
foreach(var kvm in _type_to_pem) {
kvm.Value.Stop();
}
_type_to_pem.Clear();
if(!_running) {
break;
}
DateTime now = DateTime.UtcNow;
Thread.Sleep(sleep * 1000);
if(now - start_time < TimeSpan.FromSeconds(sleep_max)) {
sleep *= 2;
sleep = (sleep > sleep_max) ? sleep_max : sleep;
} else {
sleep /= 2;
sleep = (sleep < sleep_min) ? sleep_min : sleep;
}
}
}