3 // Base class interface for handling Mode connections to device
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__
35 class SocketRoutingQueue
;
41 class BXEXPORT Connector
44 std::string m_password
;
45 bool m_needs_reconnect
;
46 Barry::IConverter m_ic
;
47 Barry::ProbeResult m_probe_result
;
49 time_t m_last_disconnect
;
51 // bad password status
56 static Barry::ProbeResult
FindDevice(Barry::Pin pin
);
58 // required overrides by derived classes
59 virtual void StartConnect(const char *password
) = 0;
60 virtual void RetryPassword(const char *password
) = 0;
61 virtual void FinishConnect() = 0;
62 virtual void DoDisconnect() = 0;
63 /// slightly different than IsConnected()... this returns true
64 /// even if there is a partial connection in progress...
65 /// i.e. this returns true if DoDisconnect() can be safely skipped
66 virtual bool IsDisconnected() = 0;
69 Connector(const char *password
, const std::string
&locale
,
71 Connector(const char *password
, const std::string
&locale
,
72 const Barry::ProbeResult
&result
);
75 IConverter
& GetIConverter() { return m_ic
; }
76 const IConverter
& GetIConverter() const { return m_ic
; }
77 Barry::ProbeResult
& GetProbeResult() { return m_probe_result
; }
78 const Barry::ProbeResult
& GetProbeResult() const { return m_probe_result
; }
79 const Barry::BadPassword
& GetBadPassword() const { return m_bpcopy
; }
81 virtual void ClearPassword();
82 virtual void SetPassword(const char *password
);
84 /// Returns true if connected, false if user cancelled, throws
85 /// Barry exception on error. Note that in the case of a bad
86 /// password, this will return false on the first password try,
87 /// unless you override PasswordPrompt() below. In the default
88 /// case, a false here means the password was invalid, and you
89 /// should use GetBadPassword() to report the error.
90 virtual bool Connect();
92 /// Disconnects from the device
93 virtual void Disconnect();
95 /// Returns same as Connect(), but normally remembers the password
96 /// and so avoids prompting the user if possible. Password prompts
97 /// are still possible though, if you have called ClearPassword().
99 /// It is valid to call Reconnect() without ever calling Connect(),
100 /// since Reconnect() is simply a wrapper that handles retries.
101 virtual bool Reconnect(int total_tries
= 2);
103 /// Calls Reconnect() (and returns it's result) only if you have
104 /// called RequireDirtyReconnect(). Otherwise, does nothing, but
106 virtual bool ReconnectForDirtyFlags();
108 /// Returns true if connected, false if not
109 virtual bool IsConnected() = 0;
111 /// This function flags the Connector object so that a future
112 /// call to ReconnectForDirtyFlags() will actually Reconnect().
113 /// This is needed in cases where you are updating the device,
114 /// and require that the dirty flags on the device itself are
115 /// properly cleared and updated. In this case, you must call
116 /// ReconnectForDirtyFlags() before Desktop::GetRecordStateTable().
117 /// Disconnecting from the device, or reconnecting, clears the flag.
118 virtual void RequireDirtyReconnect();
121 // Callbacks, overridden by the application
124 /// App should prompt user for password, fill password_result with
125 /// what he enters and then return true. Return false if user
126 /// wishes to stop trying.
128 /// This function is *not* called from inside a catch() routine,
129 /// so it is safe to throw exceptions from it if you must.
130 virtual bool PasswordPrompt(const Barry::BadPassword
&bp
,
131 std::string
&password_result
) = 0;
134 class BXEXPORT DesktopConnector
: public Connector
136 Barry::SocketRoutingQueue
*m_router
;
137 std::auto_ptr
<Barry::Controller
> m_con
;
138 std::auto_ptr
<Mode::Desktop
> m_desktop
;
139 int m_connect_timeout
;
142 virtual void StartConnect(const char *password
);
143 virtual void RetryPassword(const char *password
);
144 virtual void FinishConnect();
145 virtual void DoDisconnect();
146 virtual bool IsDisconnected();
149 // Override the timeout due to a firmware issue... sometimes
150 // the firmware will hang during a Reconnect, and fail to
151 // respond to a Desktop::Open(). To work around this, we
152 // set the default timeout to 10 seconds so that we find this
153 // failure early enough to fix it within opensync's 30 second timeout.
154 // Then if we get such a timeout, we do the Reconnect again and
155 // hope for the best... this often fixes it.
157 DesktopConnector(const char *password
, const std::string
&locale
,
158 Barry::Pin pin
= 0, Barry::SocketRoutingQueue
*router
= 0,
159 int connect_timeout
= 10000);
161 DesktopConnector(const char *password
, const std::string
&locale
,
162 const Barry::ProbeResult
&result
,
163 Barry::SocketRoutingQueue
*router
= 0,
164 int connect_timeout
= 10000);
166 virtual bool IsConnected();
168 virtual bool PasswordPrompt(const Barry::BadPassword
&bp
,
169 std::string
&password_result
)
171 // default to only trying the existing password once
176 // Do not use these functions if IsConnected() returns false
179 Controller
& GetController() { return *m_con
; }
180 Mode::Desktop
& GetDesktop() { return *m_desktop
; }
182 const Controller
& GetController() const { return *m_con
; }
183 const Mode::Desktop
& GetDesktop() const{ return *m_desktop
; }