lib: added Connector base class and DesktopConnector class
[barry.git] / src / connector.h
blob065d35f66d3d0116c307c911a11d4dae22397538
1 //
2 // \file connector.h
3 // Base class interface for handling Mode connections to device
4 //
6 /*
7 Copyright (C) 2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_CONNECT_H__
23 #define __BARRY_CONNECT_H__
25 #include "dll.h"
26 #include "iconv.h"
27 #include "pin.h"
28 #include "probe.h"
29 #include <string>
30 #include <memory>
31 #include <time.h>
33 namespace Barry {
35 class SocketRoutingQueue;
36 class Controller;
37 namespace Mode {
38 class Desktop;
41 class BXEXPORT Connector
43 protected:
44 std::string m_password;
45 bool m_needs_reconnect;
46 Barry::IConverter m_ic;
47 Barry::ProbeResult m_probe_result;
48 int m_connect_count;
49 time_t m_last_disconnect;
51 protected:
52 // helper functions
53 static Barry::ProbeResult FindDevice(Barry::Pin pin);
55 // required overrides by derived classes
56 virtual void StartConnect(const char *password) = 0;
57 virtual void RetryPassword(const char *password) = 0;
58 virtual void FinishConnect() = 0;
59 virtual void DoDisconnect() = 0;
60 /// slightly different than IsConnected()... this returns true
61 /// even if there is a partial connection in progress...
62 /// i.e. this returns true if DoDisconnect() can be safely skipped
63 virtual bool IsDisconnected() = 0;
65 public:
66 Connector(const char *password, const std::string &locale,
67 Barry::Pin pin = 0);
68 Connector(const char *password, const std::string &locale,
69 const Barry::ProbeResult &result);
71 IConverter& GetIConverter() { return m_ic; }
72 const IConverter& GetIConverter() const { return m_ic; }
73 Barry::ProbeResult& GetProbeResult() { return m_probe_result; }
74 const Barry::ProbeResult& GetProbeResult() const { return m_probe_result; }
76 virtual void ClearPassword();
77 virtual void SetPassword(const char *password);
79 /// Returns true if connected, false if user cancelled, throws
80 /// Barry exception on error.
81 virtual bool Connect();
83 /// Disconnects from the device
84 virtual void Disconnect();
86 /// Returns same as Connect(), but normally remembers the password
87 /// and so avoids prompting the user if possible. Password prompts
88 /// are still possible though, if you have called ClearPassword().
89 ///
90 /// It is valid to call Reconnect() without ever calling Connect(),
91 /// since Reconnect() is simply a wrapper that handles retries.
92 virtual bool Reconnect(int total_tries = 2);
94 /// Calls Reconnect() (and returns it's result) only if you have
95 /// called RequireDirtyReconnect(). Otherwise, does nothing, but
96 /// returns true.
97 virtual bool ReconnectForDirtyFlags();
99 /// Returns true if connected, false if not
100 virtual bool IsConnected() = 0;
102 /// This function flags the Connector object so that a future
103 /// call to ReconnectForDirtyFlags() will actually Reconnect().
104 /// This is needed in cases where you are updating the device,
105 /// and require that the dirty flags on the device itself are
106 /// properly cleared and updated. In this case, you must call
107 /// ReconnectForDirtyFlags() before Desktop::GetRecordStateTable().
108 /// Disconnecting from the device, or reconnecting, clears the flag.
109 virtual void RequireDirtyReconnect();
112 // Callbacks, overridden by the application
115 /// App should prompt user for password, fill password_result with
116 /// what he enters and then return true. Return false if user
117 /// wishes to stop trying.
119 /// This function is *not* called from inside a catch() routine,
120 /// so it is safe to throw exceptions from it if you must.
121 virtual bool PasswordPrompt(const Barry::BadPassword &bp,
122 std::string &password_result) = 0;
125 class BXEXPORT DesktopConnector : public Connector
127 Barry::SocketRoutingQueue *m_router;
128 std::auto_ptr<Barry::Controller> m_con;
129 std::auto_ptr<Mode::Desktop> m_desktop;
130 int m_connect_timeout;
132 protected:
133 virtual void StartConnect(const char *password);
134 virtual void RetryPassword(const char *password);
135 virtual void FinishConnect();
136 virtual void DoDisconnect();
137 virtual bool IsDisconnected();
139 public:
140 // Override the timeout due to a firmware issue... sometimes
141 // the firmware will hang during a Reconnect, and fail to
142 // respond to a Desktop::Open(). To work around this, we
143 // set the default timeout to 10 seconds so that we find this
144 // failure early enough to fix it within opensync's 30 second timeout.
145 // Then if we get such a timeout, we do the Reconnect again and
146 // hope for the best... this often fixes it.
148 DesktopConnector(const char *password, const std::string &locale,
149 Barry::Pin pin = 0, Barry::SocketRoutingQueue *router = 0,
150 int connect_timeout = 10000);
152 DesktopConnector(const char *password, const std::string &locale,
153 const Barry::ProbeResult &result,
154 Barry::SocketRoutingQueue *router = 0,
155 int connect_timeout = 10000);
157 virtual bool IsConnected();
159 virtual bool PasswordPrompt(const Barry::BadPassword &bp,
160 std::string &password_result)
162 // default to only trying the existing password once
163 return false;
167 // Do not use these functions if IsConnected() returns false
170 Controller& GetController() { return *m_con; }
171 Mode::Desktop& GetDesktop() { return *m_desktop; }
173 const Controller& GetController() const { return *m_con; }
174 const Mode::Desktop& GetDesktop() const{ return *m_desktop; }
179 #endif