summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_MailBox.cxx
blob: 8500f0b55777ef79aa11e2f76a7a26130a0de272 (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
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
 
#include <Standard_ProgramError.hxx>
#include <Standard_NullObject.hxx>
#include <Standard_ConstructionError.hxx>
#if !defined( WNT ) && !defined(__hpux) && !defined(HPUX)
#include <OSD_MailBox.ixx>
#include <OSD_WhoAmI.hxx>

#include <errno.h>

const OSD_WhoAmI Iam = OSD_WMailBox;

extern "C"{
int create_mailbox(char *,int, int (*)(...));
int open_mailbox(char *,int);
int remove_mailbox(int *, char *);
int write_mailbox(int *,char *,char *,int);
}


//====================== Create a mail box =========================

OSD_MailBox::OSD_MailBox(){
}



//============== Create an instanciated mail box ===================

OSD_MailBox::OSD_MailBox(const TCollection_AsciiString& box_name,
                         const Standard_Integer         box_size,
                         const OSD_Function&            async_function){


 if (!box_name.IsAscii())
  Standard_ConstructionError::Raise("OSD_MailBox::OSD_MailBox : box_name");

 myName = box_name;

 if (box_size <= 0)
  Standard_ProgramError::Raise("OSD_MailBox::OSD_MailBox : box_size");
 mySize = box_size;

 if (async_function == NULL)
  Standard_NullObject::Raise("OSD_MailBox::OSD_MailBox : async_function");
 myFunc = async_function;

}


//=======================================================================
//function : Build
//purpose  : 
//=======================================================================
void OSD_MailBox::Build()
{
  Standard_PCharacter pStr;
  pStr=(Standard_PCharacter)myName.ToCString();
  myId = create_mailbox(pStr, (int)mySize, myFunc);
  
  if (myId == 0)
    myError.SetValue (errno, Iam, "OSD_MailBox::Build");
}

//=======================================================================
//function : Open
//purpose  : 
//=======================================================================
void OSD_MailBox::Open ( const TCollection_AsciiString& box_name,
                         const Standard_Integer box_size)
{

 // Test function security 

 if (box_name == NULL)
  Standard_NullObject::Raise("OSD_MailBox::Open : box_name");

 if (!box_name.IsAscii())
  Standard_ConstructionError::Raise("OSD_MailBox::Open : box_name");

 myName = box_name;

 if (box_size <= 0)
  Standard_ProgramError::Raise("OSD_MailBox::Open : box_size");
 mySize = box_size;

 Standard_PCharacter pStr;
 //
 pStr=(Standard_PCharacter)box_name.ToCString();
 myId = open_mailbox(pStr, (int)box_size );
 if (myId == 0)
   myError.SetValue (errno, Iam, "OSD_MailBox::Open");
  
}





//====================== Close a mail box =======================

void OSD_MailBox::Delete ()
{
 
 if (myError.Failed()) myError.Perror();

 if (myId == 0)
  Standard_ProgramError::Raise("OSD_MailBox::Delete : mail box not opened/created");

 if (myName == NULL) 
  Standard_ProgramError::Raise("OSD_MailBox::Delete : mail box not opened/created");

 Standard_PCharacter pStr;
 pStr=(Standard_PCharacter)myName.ToCString();
 if (remove_mailbox((int *)&myId, pStr) == 0)
  myError.SetValue(errno, Iam, "OSD_MailBox::Delete");

 myId = 0;
}



//====================== Write into a mail box =======================

void OSD_MailBox::Write(const TCollection_AsciiString& message,
                        const Standard_Integer length)
{


 if (length <= 0 || length > mySize) 
  Standard_ProgramError::Raise("OSD_Mailbox::Write : bad length");

 Standard_PCharacter pStr, pStrM;
 //
 pStr=(Standard_PCharacter)myName.ToCString();
 pStrM=(Standard_PCharacter)message.ToCString();
 //
 if (write_mailbox((int *)&myId, pStr, pStrM, (int)length) == 0)
  myError.SetValue(errno, Iam, "OSD_Mailbox::Write");
}


void OSD_MailBox::Reset(){
 myError.Reset();
}

Standard_Boolean OSD_MailBox::Failed()const{
 return( myError.Failed());
}

void OSD_MailBox::Perror() {
 myError.Perror();
}


Standard_Integer OSD_MailBox::Error()const{
 return( myError.Error());
}
#endif