1 #include "wvexcconnmanager.h"
2 #include "wvcamelconn.h"
5 #include "wvsslstream.h"
6 #include "wvtimestream.h"
8 const time_t reconn_timeout
= 3000;
10 class WvExcReconnAlarm
: public WvTimeStream
13 WvExcReconnAlarm(WvExcConnManager
*_manager
,
14 CommandHash
*_new_commands
) :
17 new_commands(_new_commands
),
18 log("WvExcReconnAlarm", WvLog::Debug1
)
20 set_timer(reconn_timeout
);
22 virtual ~WvExcReconnAlarm() { log("Reconnect Alarm Dying\n"); }
23 virtual void execute()
25 WvTimeStream::execute();
27 log("Alarmed. Time to recreate connection.\n");
28 manager
->connect(new_commands
);
34 WvExcConnManager
*manager
;
35 CommandHash
*new_commands
;
39 WvExcConnManager::WvExcConnManager(ExchangeItStorage
*_storage
,
41 WvIStreamList
*_global_list
) :
45 global_list(_global_list
),
46 log("WvExcConnManager", WvLog::Debug1
),
47 pipename("%s" EXCHANGEIT_PATH
"/pipehack", g_get_home_dir()),
48 listener(pipename
, 0600),
51 global_list
->append(&listener
, false);
54 WvExcConnManager::~WvExcConnManager()
60 void WvExcConnManager::conn_closed(WvStream
&s
,
65 CommandHash
*new_commands
= static_cast<CommandHash
*>(userdata
);
68 int protocol_version
= conn
->get_protocol_version();
71 // Figure out why we were closed, and retry
72 if (protocol_version
== 3 && /*!conn->isok() &&*/
73 conn
->errstr() == "Response bad: * ERROR Client version is not compatible")
75 log("We seem to have died unexpectedly using protocol v3. "
78 connect(new_commands
, false, protocol_version
);
80 else if (!conn
->isok() ||
81 conn
->errstr() == "Couldn't perform initial folder sync" ||
82 conn
->errstr() == "Failed to apply pending responses")
84 log("We have either timed out or have "
85 "been disconnected from the server "
86 "against our will.\nTrying to reconnect (in %s msecs)\n",
91 reconn_alarm
= new WvExcReconnAlarm(this, new_commands
);
92 global_list
->append(reconn_alarm
, true);
96 log("We seem to have closed voluntarily."
97 "Not attempting to reconnect.\n");
102 conn
= NULL
; // since it's closed, it should be freed by the global list shortly.
103 // set it to NULL so we don't try to do anything more with it.
108 void WvExcConnManager::account_changed_callback()
110 log("Account changed callback (account changed: %s)\n",
111 !!config
["old-username"].getme());
113 connect(NULL
, false);
116 void WvExcConnManager::connect(CommandHash
*commands
, const bool do_ssl
,
117 const int protocol_version
)
126 WvLog
log("exchangeit_connection_init", WvLog::Info
);
127 WvString hostname
= config
["server"].getme();
130 !config
["username"].getme() ||
131 !config
["password"].getme())
133 log("Hostname, username, or password is blank: "
134 "not going to bother connecting\n");
139 log("Creating ExchangeIT connection to: %s\n", hostname
);
142 commands
= new CommandHash(0);
146 hostname
.append(":7070");
147 WvTCPConn
*tcp
= new WvTCPConn(hostname
);
148 WvSSLStream
*ssl
= new WvSSLStream(tcp
, NULL
, false);
149 conn
= new WvExcConn(ssl
, config
, commands
, this,
150 storage
, protocol_version
);
154 hostname
.append(":6969");
155 WvTCPConn
*tcp
= new WvTCPConn(hostname
);
156 conn
= new WvExcConn(tcp
, config
, commands
, this,
157 storage
, protocol_version
);
160 if (conn
&& conn
->isok())
162 log("Started connection to: %s\n", WvString(*conn
->src()));
163 global_list
->append(conn
, true);
165 // FIXME: is this actually ok? won't accept_cb be deallocated off the stack?
166 // need to ask pcolijn about this.
167 WvStreamCallback
accept_cb(this, &WvExcConnManager::new_camel_connection
);
168 listener
.setcallback(accept_cb
, &listener
);
172 log("Failed to properly create ExchangeItConnection\n");
175 log("The failure was: %s\n", conn
->errstr());
184 void WvExcConnManager::new_camel_connection(WvStream
&stream
, void *userdata
)
186 log("new_camel_connection!\n");
187 WvUnixListener
&listener
= *(WvUnixListener
*)userdata
;
189 WvCamelConn
*camel_conn
= new WvCamelConn(listener
.accept(), conn
);
190 global_list
->append(camel_conn
, true);