-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
95 lines (75 loc) · 2.18 KB
/
wrapper.go
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
package pmq_responder
import (
"errors"
"fmt"
"github.com/joe-at-startupmedia/posix_mq"
)
type QueueConfig posix_mq.QueueConfig
const (
O_RDONLY = posix_mq.O_RDONLY
O_WRONLY = posix_mq.O_WRONLY
O_RDWR = posix_mq.O_RDWR
O_CLOEXEC = posix_mq.O_CLOEXEC
O_CREAT = posix_mq.O_CREAT
O_EXCL = posix_mq.O_EXCL
O_NONBLOCK = posix_mq.O_NONBLOCK
)
// GetFile gets the file on the OS where the queues are stored
func (config *QueueConfig) GetFile() string {
return (*posix_mq.QueueConfig)(config).GetFile()
}
// NewMessageQueue returns an instance of the message queue given a QueueConfig.
func NewMessageQueue(config *QueueConfig) (*posix_mq.MessageQueue, error) {
return posix_mq.NewMessageQueue((*posix_mq.QueueConfig)(config))
}
func NewMessageQueueWithOwnership(config QueueConfig, owner *Ownership, postfix string) (*posix_mq.MessageQueue, error) {
if len(postfix) > 0 {
config.Name = fmt.Sprintf("%s_%s", config.Name, postfix)
}
var (
messageQueue *posix_mq.MessageQueue
)
if config.Mode == 0 {
if owner != nil && owner.IsValid() {
config.Mode = 0660
} else {
config.Mode = 0666
}
}
messageQueue, err := NewMessageQueue(&config)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not create message queue %s: %-v", config.GetFile(), err))
}
err = ApplyPermissions(owner, &config)
if err != nil {
return messageQueue, errors.New(fmt.Sprintf("Could not apply permissions %s: %-v", config.GetFile(), err))
}
return messageQueue, nil
}
type BidirectionalQueue struct {
MqRqst *posix_mq.MessageQueue
ErrRqst error
MqResp *posix_mq.MessageQueue
ErrResp error
}
func (bdr *BidirectionalQueue) Close() error {
if err := bdr.MqRqst.Close(); err != nil {
return err
}
return bdr.MqResp.Close()
}
func (bdr *BidirectionalQueue) Unlink() error {
if err := bdr.MqRqst.Unlink(); err != nil {
return err
}
return bdr.MqResp.Unlink()
}
func (bdr *BidirectionalQueue) HasErrors() bool {
return bdr.ErrResp != nil || bdr.ErrRqst != nil
}
func (bdr *BidirectionalQueue) Error() error {
return fmt.Errorf("responder: %w\nrequester: %w", bdr.ErrResp, bdr.ErrRqst)
}
func ForceRemoveQueue(queueFile string) error {
return posix_mq.ForceRemoveQueue(queueFile)
}