HEAD: rearrange things a bit so we can have a libwvbase.so, which contains
[wvapps.git] / evolution / wvexcconnmanager.cc
blobeeeeb1193c33bcea5ab8b13228ce7efb4bcf0615
1 #include "wvexcconnmanager.h"
2 #include "wvcamelconn.h"
4 #include "wvtcp.h"
5 #include "wvsslstream.h"
6 #include "wvtimestream.h"
8 const time_t reconn_timeout = 3000;
10 class WvExcReconnAlarm : public WvTimeStream
12 public:
13 WvExcReconnAlarm(WvExcConnManager *_manager,
14 CommandHash *_new_commands) :
15 WvTimeStream(),
16 manager(_manager),
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);
29 set_timer(-1);
30 close();
33 private:
34 WvExcConnManager *manager;
35 CommandHash *new_commands;
36 WvLog log;
39 WvExcConnManager::WvExcConnManager(ExchangeItStorage *_storage,
40 UniConf _config,
41 WvIStreamList *_global_list) :
42 conn(NULL),
43 storage(_storage),
44 config(_config),
45 global_list(_global_list),
46 log("WvExcConnManager", WvLog::Debug1),
47 pipename("%s" EXCHANGEIT_PATH "/pipehack", g_get_home_dir()),
48 listener(pipename, 0600),
49 reconn_alarm(NULL)
51 global_list->append(&listener, false);
54 WvExcConnManager::~WvExcConnManager()
56 if (conn)
57 conn->close();
60 void WvExcConnManager::conn_closed(WvStream &s,
61 void *userdata)
63 log("conn_closed\n");
65 CommandHash *new_commands = static_cast<CommandHash *>(userdata);
66 assert(conn);
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. "
76 "Trying v2.\n");
77 protocol_version = 2;
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",
87 reconn_timeout);
89 if (reconn_alarm)
90 delete reconn_alarm;
91 reconn_alarm = new WvExcReconnAlarm(this, new_commands);
92 global_list->append(reconn_alarm, true);
94 else
96 log("We seem to have closed voluntarily."
97 "Not attempting to reconnect.\n");
98 delete new_commands;
99 return;
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.
105 return;
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)
119 log("connect\n");
120 if (conn)
122 conn->close();
123 conn = NULL;
126 WvLog log("exchangeit_connection_init", WvLog::Info);
127 WvString hostname = config["server"].getme();
129 if (!hostname ||
130 !config["username"].getme() ||
131 !config["password"].getme())
133 log("Hostname, username, or password is blank: "
134 "not going to bother connecting\n");
135 delete commands;
136 return;
139 log("Creating ExchangeIT connection to: %s\n", hostname);
141 if (!commands)
142 commands = new CommandHash(0);
144 if (do_ssl)
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);
152 else
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);
170 else
172 log("Failed to properly create ExchangeItConnection\n");
173 if (conn)
175 log("The failure was: %s\n", conn->errstr());
176 delete conn;
177 conn = NULL;
180 delete commands;
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);