-
Notifications
You must be signed in to change notification settings - Fork 23
/
endpoint.go
167 lines (141 loc) · 3.52 KB
/
endpoint.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
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
163
164
165
166
167
/*
* === This file is part of ALICE O² ===
*
* Copyright 2020 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package channel
import (
"fmt"
"reflect"
"strings"
"github.com/rs/xid"
)
const ipcPathFormat = "/tmp/o2ipc-%s"
type BindMap map[string]Endpoint
type Endpoint interface {
GetAddressFormat() AddressFormat
GetAddress() string
GetTransport() TransportType
ToTargetEndpoint(taskHostname string) Endpoint
ToBoundEndpoint() Endpoint
}
func EndpointEquals(e Endpoint, f Endpoint) bool {
if reflect.TypeOf(e) != reflect.TypeOf(f) {
return false
}
switch v := e.(type) {
case TcpEndpoint:
w, ok := f.(TcpEndpoint)
if !ok {
return false
}
return v == w
case IpcEndpoint:
w, ok := f.(IpcEndpoint)
if !ok {
return false
}
return v == w
}
return false
}
func NewTcpEndpoint(host string, port uint64, transport TransportType) Endpoint {
return TcpEndpoint{
Host: host,
Port: port,
Transport: transport,
}
}
func NewBoundTcpEndpoint(port uint64, transport TransportType) Endpoint {
return TcpEndpoint{
Host: "*",
Port: port,
Transport: transport,
}
}
func NewIpcEndpoint(path string, transport TransportType) Endpoint {
return IpcEndpoint{
Path: strings.TrimPrefix(path, "ipc://"),
Transport: transport,
}
}
func NewBoundIpcEndpoint(transport TransportType) Endpoint {
return IpcEndpoint{
Path: fmt.Sprintf(ipcPathFormat, xid.New().String()),
Transport: transport,
}
}
type TcpEndpoint struct {
Host string
Port uint64
Transport TransportType
}
func (TcpEndpoint) GetAddressFormat() AddressFormat {
return TCP
}
func (t TcpEndpoint) GetAddress() string {
if len(t.Host) == 0 || t.Host == "*" {
return fmt.Sprintf("tcp://*:%d", t.Port)
}
return fmt.Sprintf("tcp://%s:%d", t.Host, t.Port)
}
func (t TcpEndpoint) GetTransport() TransportType {
return t.Transport
}
func (t TcpEndpoint) ToTargetEndpoint(taskHostname string) Endpoint {
return TcpEndpoint{
Host: taskHostname,
Port: t.Port,
Transport: t.Transport,
}
}
func (t TcpEndpoint) ToBoundEndpoint() Endpoint {
return TcpEndpoint{
Host: "*",
Port: t.Port,
Transport: t.Transport,
}
}
type IpcEndpoint struct {
Path string
Transport TransportType
}
func (IpcEndpoint) GetAddressFormat() AddressFormat {
return IPC
}
func (t IpcEndpoint) GetAddress() string {
return fmt.Sprintf("ipc://%s", t.Path)
}
func (t IpcEndpoint) GetTransport() TransportType {
return t.Transport
}
func (t IpcEndpoint) ToTargetEndpoint(_ string) Endpoint {
return IpcEndpoint{
Path: t.Path,
Transport: t.Transport,
}
}
func (t IpcEndpoint) ToBoundEndpoint() Endpoint {
return IpcEndpoint{
Path: t.Path,
Transport: t.Transport,
}
}