2 /// \file m_mode_base.cc
3 /// Base for mode classes
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 //////////////////////////////////////////////////////////////////////////////
29 Mode::Mode(Controller
&con
, Controller::ModeType type
)
43 /// Select device mode. This is required before using any other mode-based
44 /// operations, such as GetDBDB() and LoadDatabase().
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.
53 /// You can retry the password by catching Barry::BadPassword and
54 /// calling RetryPassword() with the new password.
56 /// \exception Barry::Error
57 /// Thrown on protocol error.
59 /// \exception std::logic_error()
60 /// Thrown if unsupported mode is requested, or if socket
63 /// \exception Barry::BadPassword
64 /// Thrown when password is invalid or if not enough retries
65 /// left in the device.
67 void Mode::Open(const char *password
)
75 m_ModeSocket
= m_con
.SelectMode(m_modetype
);
76 RetryPassword(password
);
82 /// Select device mode. This is required before using any other mode-based
83 /// operations, such as GetDBDB() and LoadDatabase().
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.
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.
95 /// You can retry the password by catching Barry::BadPassword and
96 /// calling RetryPassword() with the new password.
98 /// \exception Barry::Error
99 /// Thrown on protocol error.
101 /// \exception std::logic_error()
102 /// Thrown if unsupported mode is requested, or if socket
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
)
117 m_ModeSocket
= m_con
.SelectMode(m_modetype
, name
);
118 RetryPassword(password
);
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
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.
160 // if( m_ModeSocket ) {
161 // m_socket->Close();
167 }} // namespace Barry::Mode