LabelCmdConnectionHandler.hpp 6.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/******************************************************************************
 *         castor/tape/tapeserver/daemon/LabelCmdConnectionHandler.hpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * @author dkruse@cern.ch
 *****************************************************************************/

#pragma once

#include "castor/log/Logger.hpp"
#include "castor/tape/tapeserver/daemon/DriveCatalogue.hpp"
#include "castor/legacymsg/MessageHeader.hpp"
#include "castor/legacymsg/TapeUpdateDriveRqstMsgBody.hpp"
#include "castor/legacymsg/VdqmProxy.hpp"
#include "castor/legacymsg/VmgrProxy.hpp"
#include "castor/tape/reactor/ZMQReactor.hpp"

namespace castor     {
namespace tape       {
namespace tapeserver {
namespace daemon     {

/**
 * Handles the events of the socket listening for connection from the mount session
 */
class LabelCmdConnectionHandler: public reactor::ZMQPollEventHandler {
public:

  /**
   * Constructor.
   *
   * @param fd The file descriptor of the socket listening for
   * connections from the mount session
   * @param reactor The reactor to which new connection handlers are to be
   * registered.
   * @param log The object representing the API of the CASTOR logging system.
   * @param driveCatalogue The catalogue of tape drives controlled by the tape
   * server daemon.
   */
  LabelCmdConnectionHandler(
    const int fd,
    reactor::ZMQReactor &reactor,
    log::Logger &log,
    DriveCatalogue &driveCatalogue,
    const std::string &hostName,
    castor::legacymsg::VdqmProxy & vdqm,
    castor::legacymsg::VmgrProxy & vmgr) throw();

  /**
   * Returns the integer file descriptor of this event handler.
   */
  int getFd() throw();

  /**
   * Fills the specified poll file-descriptor ready to be used in a call to
   * poll().
   */
  void fillPollFd(zmq::pollitem_t &fd) throw();

  /**
   * Handles the specified event.
   *
   * @param fd The poll file-descriptor describing the event.
   * @return true if the event handler should be removed from and deleted by
   * the reactor.
   */
  bool handleEvent(const zmq::pollitem_t &fd);

  /**
   * Destructor.
   */
  ~LabelCmdConnectionHandler() throw();

private:

  /**
   * Logs the specifed IO event of the mount session connection.
   */
  void logLabelCmdConnectionEvent(const zmq::pollitem_t &fd);
  
  /**
   * Throws an exception if the specified file-descriptor is not that of the
   * socket listening for connections from the mount session.
   */
  void checkHandleEventFd(const int fd);
  
  /**
   * Reads a message header from the label-command connection.
   *
   * @return The message header.
   */
  legacymsg::MessageHeader readMsgHeader();

  /**
   * Handles the specified  message.
   *
   * @param header The header of the message.
   */
  void handleRequest(const legacymsg::MessageHeader &header);
  
  /**
   * Handles the case when the status of the tape is TAPE_STATUS_MOUNTED in the update drive message
   * 
   * @param vid   Volume ID of the tape mounted
   * @param mode  The mode in which the tape is mounted
   */
  void tellVMGRTapeWasMounted(const std::string& vid, const uint32_t mode);
  
  /**
   * Checks that a tape mounted for migration by request of the tapegateway is marked as BUSY in the VMGR
   * 
   * @param vid  Volume ID of the tape to be mounted
   * @param type The client type of this mount session
   */
  void checkTapeConsistencyWithVMGR(const std::string& vid, const uint32_t type, const uint32_t mode);
  
   /**
   * Marshals the specified source tape label reply message structure into the
   * specified destination buffer.
   *
   * @param dst    The destination buffer.
   * @param dstLen The length of the destination buffer.
   * @param rc     The return code to reply.
   * @return       The total length of the header.
   */
  size_t marshalLabelReplyMsg(char *const dst, const size_t dstLen,
    const int rc);
  
  /**
   * Writes a job reply message to the label-command connection.
   *
   * @param fd The file descriptor of the connection with the admin command.
   * @param rc The return code to reply.
   * 
   */
  void writeLabelReplyMsg(const int rc);
  
  /**
   * Handles a request to label a tape.
   *
   * @param header The header of the request message.
   */
  void handleLabelRequest(const legacymsg::MessageHeader &header);
  
  /**
   * Logs the reception of the specified label request.
   */
  void logLabelRequest(const legacymsg::TapeLabelRqstMsgBody &job)
    const throw();
  
  /**
   * Reads the body of a label message from the label-command connection.
   *
   * @param len The length of the message body in bytes.
   * @return The message body.
   */
  legacymsg::TapeLabelRqstMsgBody readLabelRqstMsgBody(const uint32_t len);

  /**
   * The file descriptor of the label-command connection.
   */
  const int m_fd;

  /**
   * True if this event handler owns the label-command connection.
   */
  bool m_thisEventHandlerOwnsFd;

  /**
   * The reactor to which new Vdqm connection handlers are to be registered.
   */
  reactor::ZMQReactor &m_reactor;

  /**
   * The object representing the API of the CASTOR logging system.
   */
  log::Logger &m_log;

  /**
   * The catalogue of tape drives controlled by the tape server daemon.
   */
  DriveCatalogue &m_driveCatalogue;
  
  /**
   * The name of the host on which tape daemon is running.
   */
  const std::string m_hostName;
  
  /** 
   * Reference to the VdqmProxy, allowing reporting of the drive status. It
   * will be used by the StatusReporter 
   */
  castor::legacymsg::VdqmProxy & m_vdqm;

  /** 
   * Reference to the VmgrProxy, allowing reporting and checking tape status.
   * It is also used by the StatusReporter 
   */
  castor::legacymsg::VmgrProxy & m_vmgr;
  
  /**
   * The timeout in seconds to be applied when performing network read and
   * write operations.
   */
  const int m_netTimeout;

}; // class VdqmConnectionHandler

} // namespace daemon
} // namespace tapeserver
} // namespace tape
} // namespace castor