deb: added new libbarrysync library to debian binary package
[barry.git] / src / m_mode_base.cc
blob65edfc11aeaa22b947054416e7d21b0ddd22b326
1 ///
2 /// \file m_mode_base.cc
3 /// Base for mode classes
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 #include "m_mode_base.h"
24 namespace Barry { namespace Mode {
26 //////////////////////////////////////////////////////////////////////////////
27 // Mode base class
29 Mode::Mode(Controller &con, Controller::ModeType type)
30 : m_con(con)
31 , m_modetype(type)
32 , m_ModeSocket(0)
36 Mode::~Mode()
41 // Open
43 /// Select device mode. This is required before using any other mode-based
44 /// operations, such as GetDBDB() and LoadDatabase().
45 ///
46 /// This function opens a socket to the device for communicating in Desktop
47 /// mode. If the device requires it, specify the password with a const char*
48 /// string in password. The password will not be stored in memory
49 /// inside this class, only a hash will be generated from it. After
50 /// using the hash, the hash memory will be set to 0. The application
51 /// is responsible for safely handling the raw password data.
52 ///
53 /// You can retry the password by catching Barry::BadPassword and
54 /// calling RetryPassword() with the new password.
55 ///
56 /// \exception Barry::Error
57 /// Thrown on protocol error.
58 ///
59 /// \exception std::logic_error()
60 /// Thrown if unsupported mode is requested, or if socket
61 /// already open.
62 ///
63 /// \exception Barry::BadPassword
64 /// Thrown when password is invalid or if not enough retries
65 /// left in the device.
66 ///
67 void Mode::Open(const char *password)
69 if( m_ModeSocket ) {
70 m_socket->Close();
71 m_socket.reset();
72 m_ModeSocket = 0;
75 m_ModeSocket = m_con.SelectMode(m_modetype);
76 RetryPassword(password);
80 // RetryPassword
82 /// Retry a failed password attempt from the first call to Open().
83 /// Only call this function in response to Barry::BadPassword exceptions
84 /// that are thrown from Open().
85 ///
86 /// \exception Barry::Error
87 /// Thrown on protocol error.
88 ///
89 /// \exception std::logic_error()
90 /// Thrown if in unsupported mode, or if socket already open.
91 ///
92 /// \exception Barry::BadPassword
93 /// Thrown when password is invalid or if not enough retries
94 /// left in the device.
95 ///
96 void Mode::RetryPassword(const char *password)
98 if( m_socket.get() != 0 )
99 throw std::logic_error("Socket alreay open in RetryPassword");
101 m_socket = m_con.m_zero.Open(m_ModeSocket, password);
103 // success... perform open-oriented setup
104 OnOpen();
108 void Mode::OnOpen()
113 // FIXME - is this necessary? and if it is, wouldn't it be better
114 // in the m_jvmdebug mode class? I'm not convinced that applications
115 // should have to bother with socket-level details.
116 //void Mode::Close()
118 // if( m_ModeSocket ) {
119 // m_socket->Close();
120 // m_socket.reset();
121 // m_ModeSocket = 0;
122 // }
125 }} // namespace Barry::Mode