commit 6d0281a5959d0461af2543f11fd90cf266f034c0
parent 2616aa9ff4771bef401dd3ce7d44ac20d6e3dccd
Author: Dominik Schmidt <das1993@hotmail.com>
Date: Fri, 26 Jan 2018 13:17:48 +0000
Probe multiple Addresses received form getAddress before giving up.
Diffstat:
M | src/bot.d | | | 55 | ++++++++++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 48 insertions(+), 7 deletions(-)
diff --git a/src/bot.d b/src/bot.d
@@ -14,11 +14,28 @@ import std.file;
import std.typecons;
import irc.client;
import ssl.socket;
+import std.range;
+void tryNTimes(alias func, alias errhandle)(uint N){
+ Exception last_e;
+ foreach(n;iota(0,N)){
+ try{
+ func();
+ }
+ catch(Exception e){
+ errhandle(e);
+ last_e=e;
+ continue;
+ }
+ return;
+ }
+ throw last_e;
+}
struct LookupTable{
private long[][string] _irc;
private string[][long] _telegram;
+
void connect(string irc, long telegram){
_irc[irc]~=telegram;
_telegram[telegram]~=irc;
@@ -67,7 +84,6 @@ struct Watcher{
}
struct Bot{
- Address ircAddress;
Socket ircSocket;
Socket controlSocket;
string controlPath;
@@ -78,8 +94,32 @@ struct Bot{
Watcher w_irc,w_tele,w_ctrl;
ev_loop_t *eventloop;
string proxy_url;
+ uint max_retries = 5;
string[long] telegram_channels;
+
+ private Address delegate() _ircAddressgen;
+
+ @property void ircAddresses(Range)(Range r) if(isInputRange!(Range)&&is(ElementType!Range: Address)){
+ static if(isInfinite!Range){
+ _ircAddressgen=(){
+ auto res=r.front;
+ r.popFront();
+ return res;
+ };
+ }
+ else{
+ ircAddresses(r.cycle);
+ }
+ }
+ @property auto ircAddresses(){
+ import std.range:generate;
+ return generate!(()=>_ircAddressgen());
+ }
+
+ Address ircAddress(){
+ return _ircAddressgen();
+ }
struct LastUpdate{
SysTime Telegram,IRC;
@@ -356,12 +396,13 @@ struct Bot{
void connect(){
info("Connecting to Telegram");
- telegram.connect();
- telegram_listener.connect();
- //ircClient.connect(new InternetAddress("127.0.0.1",6667));
- info("Connecting to IRC");
- ircClient.connect(ircAddress);
+ tryNTimes!(()=>telegram.connect(), (e)=>warning(e))(max_retries);
+ tryNTimes!(()=>telegram_listener.connect(), (e)=>warning(e))(max_retries);
+ //ircClient.connect(new InternetAddress("127.0.0.1",6667));
+ info("Connecting to IRC on ", ircAddress.toString);
+ tryNTimes!(()=>ircClient.connect(ircAddress), (e)=>warning(e))(max_retries);
+
controlSocket.bind(controlAddress);
controlSocket.listen(10);
}
@@ -492,7 +533,7 @@ int main(string[] args){
Bot b=Bot(args[1]);
b.proxy_url=args[2];
b.controlPath=args[3];
- b.ircAddress=getAddress(args[4],args[5].to!ushort)[0];
+ b.ircAddresses=getAddress(args[4],args[5].to!ushort);
b.initialize();
if(exists("savefile")){
auto f=File("savefile", "r");