summaryrefslogtreecommitdiff
path: root/led/complex1/MessageQueueT_BASE.h
blob: a9ac3f71a69af868cc3a60cc904a9db97588a762 (plain)
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
//
//  MessageQueue Template Base
//
//  $Id$
//

#ifndef __MessageQueueT_OE3_h
#define __MessageQueueT_OE3_h

#include <iostream>
#include <string>

namespace OSWrapper {

  template<
    typename QueueType
  >
  class MessageQueueTemplate {
    public:

    typedef void (*Converter_t)( const QueueType *src, QueueType *dest );

    private:

    std::string                 mName;
    Converter_t                 mHTON;
    Converter_t                 mNTOH;
    void                       *mQueue;

    public:

    MessageQueueTemplate::MessageQueueTemplate(
      const char      *name,
      unsigned int     depth,
      Converter_t      hton_f,
      Converter_t      ntoh_f
    )
    {
      mName = name;
      this->mHTON = hton_f;
      this->mNTOH = ntoh_f;

      // create the message queue
      mQueue = NULL;
    }

    MessageQueueTemplate::~MessageQueueTemplate()
    {
      // destroy the message queue object
      // delete mQueue;
      // destroy anything else
    }

    bool MessageQueueTemplate::Read(
      QueueType *buffer
    )
    {
      int       status = 0;
      QueueType tbuffer; 

      // Get the message
      try {
        if ( mNTOH ) {
          status = 1;  // read into tbuffer
        } else {
          status = 1;  // read into buffer
        }
 
        // read the message queue
        if ( status ) {
          std::cerr << mName << " Queue not Read: " << status << std::endl;
          return false;
        }
      } catch (...) {
        std::cerr << mName << " Queue not Read: Exception" << std::endl;
        return false;
      }

      // convert from network neutral
      if ( mNTOH )
        (*mNTOH)( &tbuffer, buffer );

      return true;
    }

    bool MessageQueueTemplate::Write(
      QueueType *buffer
    )
    {
      int status = 0;
      QueueType tbuffer; 
      QueueType *b = buffer; 

      // convert to network neutral
      if ( mHTON ) {
        (*mHTON)( buffer, &tbuffer );
        b = &tbuffer;
      }
        

      // write the message
      status = 1; // write( b );
      if ( status ) {
        std::cerr << mName << " Queue not Written: " << status << std::endl;
        return false;
      }
      return true;
    }
  };

};


#endif