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
;
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;
66 Connector(const char *password
, const std::string
&locale
,
68 Connector(const char *password
, const std::string
&locale
,
69 const Barry::ProbeResult
&result
);
72 IConverter
& GetIConverter() { return m_ic
; }
73 const IConverter
& GetIConverter() const { return m_ic
; }
74 Barry::ProbeResult
& GetProbeResult() { return m_probe_result
; }
75 const Barry::ProbeResult
& GetProbeResult() const { return m_probe_result
; }
77 virtual void ClearPassword();
78 virtual void SetPassword(const char *password
);
80 /// Returns true if connected, false if user cancelled, throws
81 /// Barry exception on error.
82 virtual bool Connect();
84 /// Disconnects from the device
85 virtual void Disconnect();
87 /// Returns same as Connect(), but normally remembers the password
88 /// and so avoids prompting the user if possible. Password prompts
89 /// are still possible though, if you have called ClearPassword().
91 /// It is valid to call Reconnect() without ever calling Connect(),
92 /// since Reconnect() is simply a wrapper that handles retries.
93 virtual bool Reconnect(int total_tries
= 2);
95 /// Calls Reconnect() (and returns it's result) only if you have
96 /// called RequireDirtyReconnect(). Otherwise, does nothing, but
98 virtual bool ReconnectForDirtyFlags();
100 /// Returns true if connected, false if not
101 virtual bool IsConnected() = 0;
103 /// This function flags the Connector object so that a future
104 /// call to ReconnectForDirtyFlags() will actually Reconnect().
105 /// This is needed in cases where you are updating the device,
106 /// and require that the dirty flags on the device itself are
107 /// properly cleared and updated. In this case, you must call
108 /// ReconnectForDirtyFlags() before Desktop::GetRecordStateTable().
109 /// Disconnecting from the device, or reconnecting, clears the flag.
110 virtual void RequireDirtyReconnect();
113 // Callbacks, overridden by the application
116 /// App should prompt user for password, fill password_result with
117 /// what he enters and then return true. Return false if user
118 /// wishes to stop trying.
120 /// This function is *not* called from inside a catch() routine,
121 /// so it is safe to throw exceptions from it if you must.
122 virtual bool PasswordPrompt(const Barry::BadPassword
&bp
,
123 std::string
&password_result
) = 0;
126 class BXEXPORT DesktopConnector
: public Connector
128 Barry::SocketRoutingQueue
*m_router
;
129 std::auto_ptr
<Barry::Controller
> m_con
;
130 std::auto_ptr
<Mode::Desktop
> m_desktop
;
131 int m_connect_timeout
;
134 virtual void StartConnect(const char *password
);
135 virtual void RetryPassword(const char *password
);
136 virtual void FinishConnect();
137 virtual void DoDisconnect();
138 virtual bool IsDisconnected();
141 // Override the timeout due to a firmware issue... sometimes
142 // the firmware will hang during a Reconnect, and fail to
143 // respond to a Desktop::Open(). To work around this, we
144 // set the default timeout to 10 seconds so that we find this
145 // failure early enough to fix it within opensync's 30 second timeout.
146 // Then if we get such a timeout, we do the Reconnect again and
147 // hope for the best... this often fixes it.
149 DesktopConnector(const char *password
, const std::string
&locale
,
150 Barry::Pin pin
= 0, Barry::SocketRoutingQueue
*router
= 0,
151 int connect_timeout
= 10000);
153 DesktopConnector(const char *password
, const std::string
&locale
,
154 const Barry::ProbeResult
&result
,
155 Barry::SocketRoutingQueue
*router
= 0,
156 int connect_timeout
= 10000);
158 virtual bool IsConnected();
160 virtual bool PasswordPrompt(const Barry::BadPassword
&bp
,
161 std::string
&password_result
)
163 // default to only trying the existing password once
168 // Do not use these functions if IsConnected() returns false
171 Controller
& GetController() { return *m_con
; }
172 Mode::Desktop
& GetDesktop() { return *m_desktop
; }
174 const Controller
& GetController() const { return *m_con
; }
175 const Mode::Desktop
& GetDesktop() const{ return *m_desktop
; }