~markwright/scalestack/zeromq

« back to all changes in this revision

Viewing changes to scalestack/common/notification.cc

  • Committer: Eric Day
  • Date: 2011-01-25 20:51:51 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: eday@oddments.org-20110125205151-rlam04duepoczo9v
Moved common into kernel, other related cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Scale Stack
3
 
 *
4
 
 * Copyright 2010 Eric Day
5
 
 *
6
 
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 
 * you may not use this file except in compliance with the License.
8
 
 * You may obtain a copy of the License at
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
12
 
 * Unless required by applicable law or agreed to in writing, software
13
 
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
 * See the License for the specific language governing permissions and
16
 
 * limitations under the License.
17
 
 */
18
 
 
19
 
/**
20
 
 * @file
21
 
 * @brief Notification Definitions
22
 
 */
23
 
 
24
 
#include "config.h"
25
 
 
26
 
#include <cerrno>
27
 
#include <cstdio>
28
 
#include <cstring>
29
 
#include <unistd.h>
30
 
 
31
 
#include <scalestack/common/exception.h>
32
 
#include <scalestack/common/notification.h>
33
 
 
34
 
namespace scalestack
35
 
{
36
 
namespace common
37
 
{
38
 
 
39
 
/*
40
 
 * Public methods.
41
 
 */
42
 
 
43
 
notification::notification(void)
44
 
{
45
 
  if (pipe(_pipe) == -1)
46
 
    _throw_exception(_("Could not create notification pipe"));
47
 
}
48
 
 
49
 
notification::~notification()
50
 
{
51
 
  close(_pipe[0]);
52
 
  close(_pipe[1]);
53
 
}
54
 
 
55
 
void notification::send(void)
56
 
{
57
 
  if (write(_pipe[1], "\0", 1) == -1 && errno != EINTR)
58
 
    _throw_exception(_("Could not write to notification pipe"));
59
 
}
60
 
 
61
 
void notification::wait(void)
62
 
{
63
 
  char buffer[1024];
64
 
 
65
 
  if (read(_pipe[0], buffer, sizeof(buffer)) == -1 && errno != EINTR)
66
 
    _throw_exception(_("Could not read from notification pipe"));
67
 
}
68
 
 
69
 
/*
70
 
 * Private methods.
71
 
 */
72
 
 
73
 
void notification::_throw_exception(const char* prefix)
74
 
{
75
 
  char message[SCALESTACK_MAX_MESSAGE_SIZE];
76
 
  snprintf(message, SCALESTACK_MAX_MESSAGE_SIZE,
77
 
           "%s: %s:%d", prefix, strerror(errno), errno);
78
 
  throw exception("notification", message);
79
 
}
80
 
 
81
 
} /* namespace common */
82
 
} /* namespace scalestack */