LabelCmdConnectionHandler.hpp 6.69 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
/******************************************************************************
 *         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     {

/**
Steven Murray's avatar
Steven Murray committed
39
40
 * Handles the events of the socket listening for connection from the
 * data-transfer session
41
42
43
44
45
46
47
48
 */
class LabelCmdConnectionHandler: public reactor::ZMQPollEventHandler {
public:

  /**
   * Constructor.
   *
   * @param fd The file descriptor of the socket listening for
Steven Murray's avatar
Steven Murray committed
49
   * connections from the data-transfer session
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
   * @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();

  /**
Steven Murray's avatar
Steven Murray committed
66
   * Returns the human-readable name this event handler.
67
   */
Steven Murray's avatar
Steven Murray committed
68
  std::string getName() const throw();
69
70
71
72
73

  /**
   * Fills the specified poll file-descriptor ready to be used in a call to
   * poll().
   */
Steven Murray's avatar
Steven Murray committed
74
  void fillPollFd(zmq_pollitem_t &fd) throw();
75
76
77
78
79
80
81
82

  /**
   * 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.
   */
Steven Murray's avatar
Steven Murray committed
83
  bool handleEvent(const zmq_pollitem_t &fd);
84
85
86
87
88
89
90
91
92

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

private:

  /**
Steven Murray's avatar
Steven Murray committed
93
   * Logs the specifed IO event of the data-transfer session connection.
94
   */
Steven Murray's avatar
Steven Murray committed
95
  void logLabelCmdConnectionEvent(const zmq_pollitem_t &fd);
96
97
98
  
  /**
   * Throws an exception if the specified file-descriptor is not that of the
Steven Murray's avatar
Steven Murray committed
99
   * socket listening for connections from the data-transfer session.
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
   */
  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);
  
  /**
Steven Murray's avatar
Steven Murray committed
118
119
   * Handles the case when the status of the tape is TAPE_STATUS_MOUNTED in the
   * update drive message
120
121
122
123
124
125
126
   * 
   * @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);
  
  /**
Steven Murray's avatar
Steven Murray committed
127
128
   * Checks that a tape mounted for migration by request of the tapegateway is
   * marked as BUSY in the VMGR
129
130
   * 
   * @param vid  Volume ID of the tape to be mounted
Steven Murray's avatar
Steven Murray committed
131
   * @param type The client type of this data-transfer session
132
   */
Steven Murray's avatar
Steven Murray committed
133
134
  void checkTapeConsistencyWithVMGR(const std::string& vid, const uint32_t type,
    const uint32_t mode);
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
228
229
230
231
  
   /**
   * 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