DriveCatalogueEntry.cpp 26.3 KB
Newer Older
Steven Murray's avatar
Steven Murray committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/******************************************************************************
 *         castor/tape/tapeserver/daemon/DriveCatalogueEntry.cpp
 *
 * 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 Steven.Murray@cern.ch
 *****************************************************************************/

25
#include "castor/exception/Exception.hpp"
Steven Murray's avatar
Steven Murray committed
26
#include "castor/tape/tapeserver/daemon/DriveCatalogueEntry.hpp"
27
28
#include "castor/utils/utils.hpp"
#include "h/Ctape_constants.h"
Daniele Kruse's avatar
Daniele Kruse committed
29
30
#include "castor/tape/tapeserver/daemon/DriveCatalogueLabelSession.hpp"
#include "castor/tape/tapeserver/daemon/DriveCatalogueTransferSession.hpp"
Steven Murray's avatar
Steven Murray committed
31
32
33

#include <string.h>

Daniele Kruse's avatar
Daniele Kruse committed
34
35
36
37
38
39
40
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueEntry::~DriveCatalogueEntry() throw() {
  delete m_session;
}

Steven Murray's avatar
Steven Murray committed
41
42
43
44
45
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueEntry::DriveCatalogueEntry()
  throw():
46
47
  m_mode(castor::messages::TAPE_MODE_NONE),
  m_getToBeMountedForTapeStatDriveEntry(false),
48
49
  m_state(DRIVE_STATE_INIT),
  m_sessionType(SESSION_TYPE_NONE),
Daniele Kruse's avatar
Daniele Kruse committed
50
51
  m_labelCmdConnection(-1),
  m_session(NULL) {
52
53
54
55
56
57
58
59
}

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueEntry::DriveCatalogueEntry(
  const utils::DriveConfig &config, const DriveState state) throw():
  m_config(config),
60
61
  m_mode(castor::messages::TAPE_MODE_NONE),
  m_getToBeMountedForTapeStatDriveEntry(false),
62
63
  m_state(state),
  m_sessionType(SESSION_TYPE_NONE),
Daniele Kruse's avatar
Daniele Kruse committed
64
65
  m_labelCmdConnection(-1),
  m_session(NULL) {
Steven Murray's avatar
Steven Murray committed
66
67
68
69
70
71
72
73
74
}

//-----------------------------------------------------------------------------
// drvState2Str
//-----------------------------------------------------------------------------
const char
  *castor::tape::tapeserver::daemon::DriveCatalogueEntry::drvState2Str(
  const DriveState state) throw() {
  switch(state) {
75
76
77
  case DRIVE_STATE_INIT             : return "INIT";
  case DRIVE_STATE_DOWN             : return "DOWN";
  case DRIVE_STATE_UP               : return "UP";
Daniele Kruse's avatar
Daniele Kruse committed
78
  case DRIVE_STATE_SESSIONRUNNING   : return "SESSIONRUNNING";
79
80
  case DRIVE_STATE_WAITDOWN         : return "WAITDOWN";
  default                           : return "UNKNOWN";
Steven Murray's avatar
Steven Murray committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  }
}

//-----------------------------------------------------------------------------
// sessionType2Str
//-----------------------------------------------------------------------------
const char
   *castor::tape::tapeserver::daemon::DriveCatalogueEntry::sessionType2Str(
  const SessionType sessionType) throw() {
  switch(sessionType) {
  case SESSION_TYPE_NONE        : return "NONE";
  case SESSION_TYPE_DATATRANSFER: return "DATATRANSFER";
  case SESSION_TYPE_LABEL       : return "LABEL";
  default                       : return "UNKNOWN";
  }
}
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

//------------------------------------------------------------------------------
// setConfig
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::setConfig(
  const tape::utils::DriveConfig &config) throw() {
}

//------------------------------------------------------------------------------
// getConfig
//------------------------------------------------------------------------------
const castor::tape::utils::DriveConfig
  &castor::tape::tapeserver::daemon::DriveCatalogueEntry::getConfig() const {
  return m_config;
}

//------------------------------------------------------------------------------
// setVid
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::setVid(
  const std::string &vid) {
  m_vid = vid;
}

//------------------------------------------------------------------------------
// getVid
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::DriveCatalogueEntry::getVid()
  const {
  return m_vid;
}

//------------------------------------------------------------------------------
// setAssignmentTime
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::setAssignmentTime(
 const time_t assignmentTime) {
 m_assignmentTime = assignmentTime;
}

//------------------------------------------------------------------------------
// getAssignmentTime
//------------------------------------------------------------------------------
time_t
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getAssignmentTime()
  const {
  return m_assignmentTime;
}

//------------------------------------------------------------------------------
// getState
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueEntry::DriveState
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getState()
  const throw() {
  return m_state;
} 

Daniele Kruse's avatar
Daniele Kruse committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//------------------------------------------------------------------------------
// getSessionState
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueSession::SessionState
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getSessionState()
  const throw() {
  return getSession()->getState();
}

//------------------------------------------------------------------------------
// getSessionState
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueSession * castor::tape::tapeserver::daemon::DriveCatalogueEntry::getSession()
  const {
  if(m_session) return m_session;
  else {
    castor::exception::Exception ex;
    ex.getMessage() << "The session pointer is null";
    throw ex;
  }
}

177
178
179
180
181
182
183
184
185
186
187
188
189
//------------------------------------------------------------------------------
// getSessionType
//------------------------------------------------------------------------------
castor::tape::tapeserver::daemon::DriveCatalogueEntry::SessionType
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getSessionType()
  const throw() {
  return m_sessionType;
}

//------------------------------------------------------------------------------
// getVdqmJob
//------------------------------------------------------------------------------
const castor::legacymsg::RtcpJobRqstMsgBody
Daniele Kruse's avatar
Daniele Kruse committed
190
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getVdqmJob() const {
191
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
192
  case DRIVE_STATE_SESSIONRUNNING:
193
  case DRIVE_STATE_WAITDOWN:
Daniele Kruse's avatar
Daniele Kruse committed
194
195
196
197
  {
    DriveCatalogueTransferSession *theTransferSession = dynamic_cast<DriveCatalogueTransferSession *>(getSession());
    return theTransferSession->getVdqmJob();
  }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to get VDQM job of tape-drive " <<
        m_config.unitName << ": Incompatible drive state: state=" <<
        drvState2Str(m_state);
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// getLabelJob
//------------------------------------------------------------------------------
const castor::legacymsg::TapeLabelRqstMsgBody
Daniele Kruse's avatar
Daniele Kruse committed
213
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getLabelJob() const {
214
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
215
  case DRIVE_STATE_SESSIONRUNNING:
216
  case DRIVE_STATE_WAITDOWN:
Daniele Kruse's avatar
Daniele Kruse committed
217
218
219
220
  {
    DriveCatalogueLabelSession *theLabelSession = dynamic_cast<DriveCatalogueLabelSession *>(getSession());
    return theLabelSession->getLabelJob();
  }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to get label job of tape-drive " <<
        m_config.unitName << ": Incompatible drive state: state=" <<
        drvState2Str(m_state);
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// getSessionPid
//------------------------------------------------------------------------------
pid_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::getSessionPid()
  const {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
238
  case DRIVE_STATE_SESSIONRUNNING:
239
  case DRIVE_STATE_WAITDOWN:
Daniele Kruse's avatar
Daniele Kruse committed
240
    return getSession()->getPid();
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to get the process ID of tape-drive " <<
        m_config.unitName << ": Incompatible drive state: state=" <<
        drvState2Str(m_state);
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// getLabelCmdConnection
//------------------------------------------------------------------------------
int
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getLabelCmdConnection()
  const {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
259
  case DRIVE_STATE_SESSIONRUNNING:
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  case DRIVE_STATE_WAITDOWN:
    return m_labelCmdConnection;;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to get the file descriptor of the TCP/IP"
        " connection with the castor-tape-label command-line tool for"
        " tape-drive" << m_config.unitName << ": Incompatible drive state"
        ": state=" << drvState2Str(m_state);
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// releaseLabelCmdConnection
//------------------------------------------------------------------------------
int castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  releaseLabelCmdConnection() {
    switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
280
  case DRIVE_STATE_SESSIONRUNNING:
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  case DRIVE_STATE_WAITDOWN:
    if(0 < m_labelCmdConnection) {
      const int tmpConnection = m_labelCmdConnection;
      m_labelCmdConnection = -1;
      return tmpConnection;
    } else {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to release the file descriptor of the TCP/IP"
        " connection with the castor-tape-label command-line tool for"
        " tape-drive" << m_config.unitName << ": File descriptor is not owned"
        " by the drive entry in the tape-drive catalogue";
      throw ex;
    }
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to release the file descriptor of the TCP/IP"
        " connection with the castor-tape-label command-line tool for"
        " tape-drive" << m_config.unitName << ": Incompatible drive state"
        ": state=" << drvState2Str(m_state);
      throw ex;
    }
  }

}

//-----------------------------------------------------------------------------
Daniele Kruse's avatar
Daniele Kruse committed
308
// updateVolumeInfo
309
310
311
312
313
314
315
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::updateVolumeInfo(
  const castor::messages::NotifyDriveBeforeMountStarted &body) {
  m_assignmentTime = time(0);
  m_vid = body.vid();
  m_getToBeMountedForTapeStatDriveEntry = true;
  m_mode = body.mode();
Daniele Kruse's avatar
Daniele Kruse committed
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
/*
  try {    
    m_event = (castor::legacymsg::TapeUpdateDriveRqstMsgBody::TapeEvent)body.event;
    switch(body.event) {
      case legacymsg::TapeUpdateDriveRqstMsgBody::TAPE_STATUS_BEFORE_MOUNT_STARTED:
        m_vid = body.vid;
        m_assignmentTime = time(0);
        m_mode = (castor::legacymsg::TapeUpdateDriveRqstMsgBody::TapeMode)body.mode;
        break;
      case legacymsg::TapeUpdateDriveRqstMsgBody::TAPE_STATUS_MOUNTED:
        break;
      case legacymsg::TapeUpdateDriveRqstMsgBody::TAPE_STATUS_UNMOUNT_STARTED:
        break;
      case legacymsg::TapeUpdateDriveRqstMsgBody::TAPE_STATUS_UNMOUNTED:
        m_vid = "";
        m_assignmentTime = 0;
        m_mode = castor::legacymsg::TapeUpdateDriveRqstMsgBody::TAPE_MODE_NONE;
        break;
      default:
        break;
    }
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to " << task << ": " << ne.getMessage().str();
    throw ex;
  }
*/
343
}
Daniele Kruse's avatar
Daniele Kruse committed
344

345
346
347
348
349
350
//------------------------------------------------------------------------------
// configureUp
//------------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::configureUp() {
  switch(m_state) {
  case DRIVE_STATE_UP:
Daniele Kruse's avatar
Daniele Kruse committed
351
  case DRIVE_STATE_SESSIONRUNNING:
352
353
354
355
356
    break;
  case DRIVE_STATE_DOWN:
    m_state = DRIVE_STATE_UP;
    break;
  case DRIVE_STATE_WAITDOWN:
Daniele Kruse's avatar
Daniele Kruse committed
357
    m_state = DRIVE_STATE_SESSIONRUNNING;
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to configure tape-drive " << m_config.unitName
        << " up : Incompatible drive state: state=" << drvState2Str(m_state);
      throw ex;
    }
  }
}

//-----------------------------------------------------------------------------
// configureDown
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::configureDown() {
  switch(m_state) {
  case DRIVE_STATE_DOWN:
    break;
  case DRIVE_STATE_UP:
    m_state = DRIVE_STATE_DOWN;
    break;
Daniele Kruse's avatar
Daniele Kruse committed
379
  case DriveCatalogueEntry::DRIVE_STATE_SESSIONRUNNING:
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    m_state = DRIVE_STATE_WAITDOWN;
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to configure tape drive " << m_config.unitName
        << " down: " << ": Incompatible drive state: state=" <<
        drvState2Str(m_state);
      throw ex;
    }
  }
}

//-----------------------------------------------------------------------------
// receivedVdqmJob
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::receivedVdqmJob(
  const legacymsg::RtcpJobRqstMsgBody &job)  {

  std::ostringstream task;
  task << "handle vdqm job for tape drive " << m_config.unitName;

  // Sanity check
  if(job.driveUnit != m_config.unitName) {
    // Should never happen
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to " << task.str() <<
      ": unit name mismatch: job.driveUnit=" << job.driveUnit <<
      " m_config.unitName=" << m_config.unitName;
    throw ex;
  }
  
  switch(m_state) {
  case DRIVE_STATE_UP:
    if(std::string(job.dgn) != m_config.dgn) {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
        ": DGN mismatch: catalogueDgn=" << m_config.dgn << " vdqmJobDgn="
          << job.dgn;
      throw ex;
    }
Daniele Kruse's avatar
Daniele Kruse committed
421
422
423
    m_state = DRIVE_STATE_SESSIONRUNNING;
    m_sessionType = SESSION_TYPE_DATATRANSFER;
    m_session = new DriveCatalogueTransferSession(castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_WAITFORK, job);
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
        ": Incompatible drive state: state=" << drvState2Str(m_state);
      throw ex;
    }
  } 
} 

//-----------------------------------------------------------------------------
// receivedLabelJob
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::receivedLabelJob(
  const legacymsg::TapeLabelRqstMsgBody &job, const int labelCmdConnection) {
  const std::string unitName(job.drive);

  std::ostringstream task;
  task << "handle label job for tape drive " << m_config.unitName;

  // Sanity check
  if(job.drive != m_config.unitName) {
    // Should never happen
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to " << task.str() <<
      ": unit name mismatch: job.drive=" << job.drive << 
      " m_config.unitName=" << m_config.unitName;
    throw ex;
  }

  switch(m_state) {
  case DRIVE_STATE_UP:
    if(std::string(job.dgn) != m_config.dgn) {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
        ": DGN mismatch: catalogueDgn=" << m_config.dgn << " labelJobDgn="
        << job.dgn;
      throw ex;
    }
    if(-1 != m_labelCmdConnection) {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
        ": Drive catalogue already owns a label-command connection";
      throw ex;
    }
    m_labelCmdConnection = labelCmdConnection;
Daniele Kruse's avatar
Daniele Kruse committed
471
472
473
    m_state = DRIVE_STATE_SESSIONRUNNING;
    m_sessionType = SESSION_TYPE_LABEL;
    m_session = new DriveCatalogueLabelSession(castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_WAITFORK, job, labelCmdConnection);
474
475
476
477
478
479
480
481
482
483
484
485
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
        ": Incompatible drive state: state=" << drvState2Str(m_state);
      throw ex;
    }
  }
}

//-----------------------------------------------------------------------------
486
// forkedDataTransferSession
487
//-----------------------------------------------------------------------------
488
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::forkedDataTransferSession(
489
490
491
492
  const pid_t sessionPid)  {
  std::ostringstream task;
  task << "handle fork of mount session for tape drive " << m_config.unitName;

Daniele Kruse's avatar
Daniele Kruse committed
493
494
495
496
  switch(getSession()->getState()) {
  case castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_WAITFORK:
    getSession()->setState(castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_RUNNING);
    getSession()->setPid(sessionPid);
497
498
499
500
501
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
Daniele Kruse's avatar
Daniele Kruse committed
502
        ": Incompatible session state: state=" << getSession()->getState();
503
504
505
506
507
508
509
510
511
512
513
514
515
      throw ex;
    }
  }
}

//-----------------------------------------------------------------------------
// forkedLabelSession
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::forkedLabelSession(
  const pid_t sessionPid) {
  std::ostringstream task;
  task << "handle fork of label session for tape drive " << m_config.unitName;
    
Daniele Kruse's avatar
Daniele Kruse committed
516
517
518
519
  switch(getSession()->getState()) {
  case castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_WAITFORK:
    getSession()->setState(castor::tape::tapeserver::daemon::DriveCatalogueSession::SESSION_STATE_RUNNING);
    getSession()->setPid(sessionPid);
520
521
522
523
524
    break;
  default:
    {   
      castor::exception::Exception ex;
      ex.getMessage() << "Failed to " << task.str() <<
Daniele Kruse's avatar
Daniele Kruse committed
525
        ": Incompatible drive state: state=" << getSession()->getState();
526
527
528
529
530
531
532
533
      throw ex;
    }
  }
}

//-----------------------------------------------------------------------------
// sessionSucceeded
//-----------------------------------------------------------------------------
Daniele Kruse's avatar
Daniele Kruse committed
534
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::sessionSucceeded() {
535
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
536
  case DRIVE_STATE_SESSIONRUNNING:
537
538
539
540
541
542
543
544
545
546
    m_state = DRIVE_STATE_UP;
    break;
  case DRIVE_STATE_WAITDOWN:
    m_state = DriveCatalogueEntry::DRIVE_STATE_DOWN;
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() <<
        "Failed to record tape session succeeded for session with pid " <<
Daniele Kruse's avatar
Daniele Kruse committed
547
        getSession()->getPid() << ": Incompatible drive state: state=" <<
548
        drvState2Str(m_state);
Daniele Kruse's avatar
Daniele Kruse committed
549
550
551
      delete m_session;
      m_session = NULL;
      m_sessionType = SESSION_TYPE_NONE;
552
553
554
      throw ex;
    }
  }
Daniele Kruse's avatar
Daniele Kruse committed
555
556
557
  delete m_session;
  m_session = NULL;
  m_sessionType = SESSION_TYPE_NONE;
558
559
560
561
562
563
564
}

//-----------------------------------------------------------------------------
// sessionFailed
//-----------------------------------------------------------------------------
void castor::tape::tapeserver::daemon::DriveCatalogueEntry::sessionFailed() {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
565
  case DRIVE_STATE_SESSIONRUNNING:
566
567
568
569
570
571
572
573
  case DRIVE_STATE_WAITDOWN:
    m_state = DRIVE_STATE_DOWN;
    break;
  default:
    {
      castor::exception::Exception ex;
      ex.getMessage() <<
        "Failed to record tape session failed for session with pid " <<
Daniele Kruse's avatar
Daniele Kruse committed
574
        getSession()->getPid() << ": Incompatible drive state: state=" <<
575
        drvState2Str(m_state);
Daniele Kruse's avatar
Daniele Kruse committed
576
577
578
      delete m_session;
      m_session = NULL;
      m_sessionType = SESSION_TYPE_NONE;
579
580
581
      throw ex;
    }
  }
Daniele Kruse's avatar
Daniele Kruse committed
582
583
584
  delete m_session;
  m_session = NULL;
  m_sessionType = SESSION_TYPE_NONE;
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
}

//------------------------------------------------------------------------------
// getTapeStatDriveEntry
//------------------------------------------------------------------------------
castor::legacymsg::TapeStatDriveEntry
  castor::tape::tapeserver::daemon::DriveCatalogueEntry::getTapeStatDriveEntry()
  const {
  legacymsg::TapeStatDriveEntry entry;

  try {
    entry.jid = getUidForTapeStatDriveEntry();
    entry.uid = getJidForTapeStatDriveEntry();
    castor::utils::copyString(entry.dgn, m_config.dgn.c_str());
    entry.up = getUpForTapeStatDriveEntry();
    entry.asn = getAsnForTapeStatDriveEntry();
    entry.asn_time = getAsnTimeForTapeStatDriveEntry();
    castor::utils::copyString(entry.drive, m_config.unitName.c_str());
    entry.mode = getModeForTapeStatDriveEntry();
    castor::utils::copyString(entry.lblcode,
      getLblCodeForTapeStatDriveEntry().c_str());
    entry.tobemounted = getToBeMountedForTapeStatDriveEntry();
    castor::utils::copyString(entry.vid, getVidForTapeStatDriveEntry().c_str());
    castor::utils::copyString(entry.vsn, getVsnForTapeStatDriveEntry().c_str());
    entry.cfseq = 0; // the fseq is ignored by tpstat, so we leave it set to 0
  } catch(castor::exception::Exception &ne) {
    castor::exception::Exception ex;
    ex.getMessage() << "Failed to get TapeStatDriveEntry: " <<
      ne.getMessage().str();
    throw ex;
  }

  return entry;
}

//------------------------------------------------------------------------------
// getUidForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint32_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getUidForTapeStatDriveEntry() const throw() {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
626
  case DRIVE_STATE_SESSIONRUNNING:
627
628
629
630
631
632
633
634
635
636
637
638
639
  case DRIVE_STATE_WAITDOWN:
    return geteuid();
  default:
    return 0;
  }
}

//------------------------------------------------------------------------------
// getJidForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint32_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getJidForTapeStatDriveEntry() const throw() {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
640
  case DRIVE_STATE_SESSIONRUNNING:
641
  case DRIVE_STATE_WAITDOWN:
Daniele Kruse's avatar
Daniele Kruse committed
642
    return getSession()->getPid();
643
644
645
646
647
648
649
650
651
652
653
654
  default:
    return 0;
  }
}

//------------------------------------------------------------------------------
// getUpForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint16_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getUpForTapeStatDriveEntry() const throw() {
  switch(m_state) {
  case DRIVE_STATE_UP:
Daniele Kruse's avatar
Daniele Kruse committed
655
  case DRIVE_STATE_SESSIONRUNNING:
656
657
658
659
660
661
662
663
664
665
666
667
    return 1;
  default:
    return 0;
  }
}

//------------------------------------------------------------------------------
// getAsnForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint16_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getAsnForTapeStatDriveEntry() const throw() {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
668
  case DRIVE_STATE_SESSIONRUNNING:
669
670
671
672
673
674
675
676
677
678
679
680
  case DRIVE_STATE_WAITDOWN:
    return 1;
  default:
    return 0;
  }
}

//------------------------------------------------------------------------------
// getAsnTimeForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint32_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getAsnTimeForTapeStatDriveEntry() const throw() {
Daniele Kruse's avatar
Daniele Kruse committed
681
  return m_assignmentTime;
682
683
684
685
686
687
688
689
}

//------------------------------------------------------------------------------
// getModeForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint16_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getModeForTapeStatDriveEntry() const throw() {
  switch(m_state) {
Daniele Kruse's avatar
Daniele Kruse committed
690
  case DRIVE_STATE_SESSIONRUNNING:
691
692
  case DRIVE_STATE_WAITDOWN:
    switch(m_mode) {
693
    case castor::messages::TAPE_MODE_READ:
694
      return WRITE_DISABLE;
695
    case castor::messages::TAPE_MODE_READWRITE:
696
      return WRITE_ENABLE;
697
    case castor::messages::TAPE_MODE_DUMP:
698
      return WRITE_DISABLE;
699
      case castor::messages::TAPE_MODE_NONE:
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
      return WRITE_DISABLE;
    }
  default:
    return WRITE_DISABLE;
  }
}

//------------------------------------------------------------------------------
// getLblCodeForTapeStatDriveEntry
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getLblCodeForTapeStatDriveEntry() const throw() {
  return "aul";
}

//------------------------------------------------------------------------------
// getToBeMountedForTapeStatDriveEntry
//------------------------------------------------------------------------------
uint16_t castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getToBeMountedForTapeStatDriveEntry() const throw() {
720
  return m_getToBeMountedForTapeStatDriveEntry;
721
722
723
724
725
726
727
}

//------------------------------------------------------------------------------
// getVidForTapeStatDriveEntry
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getVidForTapeStatDriveEntry() const throw() {
Daniele Kruse's avatar
Daniele Kruse committed
728
  return m_vid;
729
730
731
732
733
734
735
736
737
}

//------------------------------------------------------------------------------
// getVsnForTapeStatDriveEntry
//------------------------------------------------------------------------------
std::string castor::tape::tapeserver::daemon::DriveCatalogueEntry::
  getVsnForTapeStatDriveEntry() const throw() {
  return getVidForTapeStatDriveEntry();
}