doc: added link to Nicolas Vivien's development blog
[barry.git] / src / m_mode_base.cc
blob1b6f3dd147a5e229b2c6bacf22dd3027d8a64364
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 // Open
82 /// Select device mode. This is required before using any other mode-based
83 /// operations, such as GetDBDB() and LoadDatabase().
84 ///
85 /// This function opens a socket to the device for communicating in Desktop
86 /// mode. If the device requires it, specify the password with a const char*
87 /// string in password. The password will not be stored in memory
88 /// inside this class, only a hash will be generated from it. After
89 /// using the hash, the hash memory will be set to 0. The application
90 /// is responsible for safely handling the raw password data.
91 ///
92 /// It uses the provided name as the name for the socket used in this mode.
93 /// Usually this shouldn't be needed unless using the raw channel mode.
94 ///
95 /// You can retry the password by catching Barry::BadPassword and
96 /// calling RetryPassword() with the new password.
97 ///
98 /// \exception Barry::Error
99 /// Thrown on protocol error.
101 /// \exception std::logic_error()
102 /// Thrown if unsupported mode is requested, or if socket
103 /// already open.
105 /// \exception Barry::BadPassword
106 /// Thrown when password is invalid or if not enough retries
107 /// left in the device.
109 void Mode::Open(const char *password, const char *name)
111 if( m_ModeSocket ) {
112 m_socket->Close();
113 m_socket.reset();
114 m_ModeSocket = 0;
117 m_ModeSocket = m_con.SelectMode(m_modetype, name);
118 RetryPassword(password);
122 // RetryPassword
124 /// Retry a failed password attempt from the first call to Open().
125 /// Only call this function in response to Barry::BadPassword exceptions
126 /// that are thrown from Open().
128 /// \exception Barry::Error
129 /// Thrown on protocol error.
131 /// \exception std::logic_error()
132 /// Thrown if in unsupported mode, or if socket already open.
134 /// \exception Barry::BadPassword
135 /// Thrown when password is invalid or if not enough retries
136 /// left in the device.
138 void Mode::RetryPassword(const char *password)
140 if( m_socket.get() != 0 )
141 throw std::logic_error("Socket alreay open in RetryPassword");
143 m_socket = m_con.m_zero.Open(m_ModeSocket, password);
145 // success... perform open-oriented setup
146 OnOpen();
150 void Mode::OnOpen()
155 // FIXME - is this necessary? and if it is, wouldn't it be better
156 // in the m_jvmdebug mode class? I'm not convinced that applications
157 // should have to bother with socket-level details.
158 //void Mode::Close()
160 // if( m_ModeSocket ) {
161 // m_socket->Close();
162 // m_socket.reset();
163 // m_ModeSocket = 0;
164 // }
167 }} // namespace Barry::Mode