AcsMessageHandler.cpp 13.8 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
/******************************************************************************
 *
 * 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 Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#include "castor/messages/messages.hpp"
#include "castor/messages/ReturnValue.pb.h"
24
25
#include "castor/messages/AcsMountTapeReadOnly.pb.h"
#include "castor/messages/AcsMountTapeReadWrite.pb.h"
26
27
28
29
#include "castor/messages/AcsDismountTape.pb.h"
#include "castor/acs/Constants.hpp"
#include "castor/acs/AcsMessageHandler.hpp"
#include "castor/acs/AcsDismountTape.hpp"
30
31
#include "castor/acs/AcsMountTapeReadOnly.hpp"
#include "castor/acs/AcsMountTapeReadWrite.hpp"
32
33
#include "castor/acs/AcsDaemon.hpp"
#include "castor/tape/utils/utils.hpp"
34
35
#include "castor/acs/Acs.hpp"
#include "castor/acs/AcsImpl.hpp"
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

#include <sstream>

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::acs::AcsMessageHandler::AcsMessageHandler(
  castor::tape::reactor::ZMQReactor &reactor,
  log::Logger &log,
  const std::string &hostName,
  void *const zmqContext,
  const AcsDaemon::CastorConf &castorConf):
  m_reactor(reactor),
  m_log(log),
  m_socket(zmqContext, ZMQ_ROUTER),
  m_hostName(hostName),
  m_castorConf(castorConf) { 

  std::ostringstream endpoint;
  endpoint << "tcp://127.0.0.1:" << m_castorConf.ascServerInternalListeningPort;
  
  try {
    m_socket.bind(endpoint.str().c_str());
    log::Param params[] = {log::Param("endpoint", endpoint.str())};
    m_log(LOG_INFO, "Bound the ZMQ socket of the AcsMessageHandler",
      params);
  } catch(castor::exception::Exception &ne){
    castor::exception::Exception ex;
    ex.getMessage() <<
      "Failed to bind the ZMQ socket of the AcsMessageHandler"
      ": endpoint=" << endpoint.str() << ": " << ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::acs::AcsMessageHandler::~AcsMessageHandler()
  throw() {
}

//------------------------------------------------------------------------------
// getName
//------------------------------------------------------------------------------
std::string castor::acs::AcsMessageHandler::getName()
  const throw() {
  return "AcsMessageHandler";
}

//------------------------------------------------------------------------------
// fillPollFd
//------------------------------------------------------------------------------
void castor::acs::AcsMessageHandler::fillPollFd(
  zmq_pollitem_t &fd) throw() {
  fd.events = ZMQ_POLLIN;
  fd.revents = 0;
  fd.socket = m_socket.getZmqSocket();
  fd.fd = -1;
}

//------------------------------------------------------------------------------
// handleEvent
//------------------------------------------------------------------------------
bool castor::acs::AcsMessageHandler::handleEvent(
  const zmq_pollitem_t &fd) throw() {
  // Try to receive a request, simply giving up if an exception is raised
  messages::Frame rqst;

  //for handling zeroMQ's router socket type specific elements 
  //ie first frame = identity of the sender
  //   second one  =  empty
  //   third and following = actual data frames
 
  //The ZmqMsg address data can be dump as string and used as key for storing 
  //the identity (for clients who need a late answer)
  castor::messages::ZmqMsg address;
  castor::messages::ZmqMsg empty;
  try {
    checkSocket(fd);
    m_socket.recv(address);
    m_socket.recv(empty);
    rqst = messages::recvFrame(m_socket);
  } catch(castor::exception::Exception &ex) {
    log::Param params[] = {log::Param("message", ex.getMessage().str())};
    m_log(LOG_ERR, "AcsMessageHandler failed to handle event", params);
    return false; // Give up and stay registered with the reactor
  }
  log::Param params[] = {
      log::Param("sender identity", 
              tape::utils::toHexString(address.getData(),address.size()))
     };
  m_log(LOG_DEBUG, "handling event in AcsMessageHandler", params);
  
  // From this point on any exception thrown should be converted into an
  // Exception message and sent back to the client
  messages::Frame reply;
  try {
    reply = dispatchMsgHandler(rqst);
  } catch(castor::exception::Exception &ex) {
    reply = createExceptionFrame(ex.code(), ex.getMessage().str());
  } catch(std::exception &se) {
    reply = createExceptionFrame(SEINTERNAL, se.what());
    m_log(LOG_ERR, se.what());
  } catch(...) {
    reply = createExceptionFrame(SEINTERNAL, "Caught an unknown exception");  
    m_log(LOG_ERR, "Caught an unknown exception");
  }

  // Send the reply to the client
  try {
    //we need to prepend our frames the same way we received them
    // ie identity + empty frames 
    m_socket.send(address,ZMQ_SNDMORE);
    m_socket.send(empty,ZMQ_SNDMORE);
    
    messages::sendFrame(m_socket, reply);
  } catch(castor::exception::Exception &ex) {
    log::Param params[] = {log::Param("message", ex.getMessage().str())};
    m_log(LOG_ERR, "AcsMessageHandler failed to send reply to client", params);
  }

  return false; // Stay registered with the reactor
}

//------------------------------------------------------------------------------
// checkSocket
//------------------------------------------------------------------------------
void castor::acs::AcsMessageHandler::checkSocket(
  const zmq_pollitem_t &fd) const{
  void* underlyingSocket = m_socket.getZmqSocket();
  if(fd.socket != underlyingSocket){
    castor::exception::Exception ex;
    ex.getMessage() << "AcsMessageHandler passed wrong poll item";
    throw ex;
  }
}

//------------------------------------------------------------------------------
// dispatchMsgHandler
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
  dispatchMsgHandler(const messages::Frame &rqst) {
  m_log(LOG_DEBUG, "AcsMessageHandler dispatching message handler");
  
  switch(rqst.header.msgtype()) {
182
183
    case messages::MSG_TYPE_ACSMOUNTTAPEREADONLY:
      return handleAcsMountTapeReadOnly(rqst);
184
      
185
186
    case messages::MSG_TYPE_ACSMOUNTTAPEREADWRITE:
      return handleAcsMountTapeReadWrite(rqst);  
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

    case messages::MSG_TYPE_ACSDISMOUNTTAPE:
      return handleAcsDismountTape(rqst);

    default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to dispatch message handler"
        ": Unknown request type: msgtype=" << rqst.header.msgtype();
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
202
// handleAcsMountTapeReadOnly
203
204
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
205
206
  handleAcsMountTapeReadOnly(const messages::Frame &rqst) {
  m_log(LOG_DEBUG, "Handling AcsMountTapeReadOnly message");
207
208

  try {
209
    messages::AcsMountTapeReadOnly rqstBody;
210
211
212
213
214
215
216
217
    rqst.parseBodyIntoProtocolBuffer(rqstBody);
    
    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
218
219
220
221
222
    log::Param params[] = {log::Param("vid", vid),
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
223
    m_log(LOG_INFO, "AcsMountTapeReadOnly message", params);
224

225
    castor::acs::AcsImpl acsWrapper;
226
    castor::acs::AcsMountTapeReadOnly acsMountTapeReadOnly(vid, acs, lsm, 
227
228
      panel, drive, acsWrapper, m_log, m_castorConf);
    try {
229
230
      acsMountTapeReadOnly.execute();
      m_log(LOG_INFO,"Tape successfully mounted for read only access", params);
231
    } catch (castor::exception::Exception &ne) {
232
233
      m_log(LOG_ERR,"Tape mount for read only access failed: "
        + ne.getMessage().str(), params);  
234
235
236
237
238
239
      throw;  
    }     
    const messages::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
240
    ex.getMessage() << "Failed to handle AcsMountTapeReadOnly message: " <<
241
242
243
244
245
246
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
247
// handleAcsMountTapeReadWrite
248
249
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
250
251
  handleAcsMountTapeReadWrite(const messages::Frame &rqst) {
  m_log(LOG_DEBUG, "Handling AcsMountTapeReadWrite message");
252
253

  try {
254
    messages::AcsMountTapeReadWrite rqstBody;
255
256
257
258
259
260
261
262
    rqst.parseBodyIntoProtocolBuffer(rqstBody);
     
    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
263
264
265
266
267
    log::Param params[] = {log::Param("vid", vid),
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
268
    m_log(LOG_INFO, "AcsMountTapeReadWrite message", params);
269

270
    castor::acs::AcsImpl acsWrapper;
271
    castor::acs::AcsMountTapeReadWrite acsMountTapeReadWrite(vid, acs,
272
273
      lsm, panel, drive, acsWrapper, m_log, m_castorConf);
    try {
274
275
      acsMountTapeReadWrite.execute();   
      m_log(LOG_INFO,"Tape successfully mounted for read/write access", params);
276
    } catch (castor::exception::Exception &ne) {
277
278
      m_log(LOG_ERR,"Tape mount for read/write access failed: "
        + ne.getMessage().str(), params);  
279
280
281
282
283
284
      throw;  
    }     
    const messages::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
285
    ex.getMessage() << "Failed to handle AcsMountTapeReadWrite message: " <<
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleAcsDismountTape
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
  handleAcsDismountTape(const messages::Frame& rqst) {
  m_log(LOG_DEBUG, "Handling AcsDismountTape message");

  try {
    messages::AcsDismountTape rqstBody;
    rqst.parseBodyIntoProtocolBuffer(rqstBody);

    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
308
309
310
311
312
    log::Param params[] = {log::Param("vid", vid),
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
313
314
    m_log(LOG_INFO, "AcsDismountTape message", params);

315
    castor::acs::AcsImpl acsWrapper;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    castor::acs::AcsDismountTape acsDismountTape(vid, acs, lsm, panel, drive,
      acsWrapper, m_log, m_castorConf);
    try {
      acsDismountTape.execute();
      m_log(LOG_INFO,"Tape successfully dismounted", params);
    } catch (castor::exception::Exception &ne) {
      m_log(LOG_ERR,"Tape dismount failed: "+ne.getMessage().str(), params);  
      throw;  
    }    
    const messages::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsDismountTape message: " <<
      ne.getMessage().str();
    throw ex;
  } catch(...) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsDismountTape message: " 
                    << "Caught an unknown exception";
    throw ex;
  }
}

//------------------------------------------------------------------------------
// createReturnValueFrame
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
  createReturnValueFrame(const int value) {
  messages::Frame frame;

  frame.header = castor::messages::protoTapePreFillHeader();
  frame.header.set_msgtype(messages::MSG_TYPE_RETURNVALUE);
  frame.header.set_bodyhashvalue(messages::computeSHA1Base64(frame.body));
  frame.header.set_bodysignature("PIPO");

  messages::ReturnValue body;
  body.set_value(value);
  frame.serializeProtocolBufferIntoBody(body);

  return frame;
}

//------------------------------------------------------------------------------
// createExceptionFrame
//------------------------------------------------------------------------------
castor::messages::Frame castor::acs::AcsMessageHandler::
  createExceptionFrame(const int code, const std::string& msg) {
  messages::Frame frame;

  frame.header = castor::messages::protoTapePreFillHeader();
  frame.header.set_msgtype(messages::MSG_TYPE_EXCEPTION);
  frame.header.set_bodyhashvalue(messages::computeSHA1Base64(frame.body));
  frame.header.set_bodysignature("PIPO");

  messages::Exception body;
  body.set_code(code);
  body.set_message(msg);
  frame.serializeProtocolBufferIntoBody(body);

  return frame;
}