| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. 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, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
// This file contains special-case wrapper functions or wrappers that don't follow |
| 21 |
// the pattern of genwrap.go. |
| 22 |
|
| 23 |
package proton |
| 24 |
|
| 25 |
//#include <proton/codec.h> |
| 26 |
//#include <proton/connection.h> |
| 27 |
//#include <proton/delivery.h> |
| 28 |
//#include <proton/event.h> |
| 29 |
//#include <proton/link.h> |
| 30 |
//#include <proton/link.h> |
| 31 |
//#include <proton/object.h> |
| 32 |
//#include <proton/sasl.h> |
| 33 |
//#include <proton/session.h> |
| 34 |
//#include <proton/transport.h> |
| 35 |
//#include <stdlib.h> |
| 36 |
import "C" |
| 37 |
|
| 38 |
import ( |
| 39 |
"fmt" |
| 40 |
"time" |
| 41 |
"unsafe" |
| 42 |
|
| 43 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 44 |
) |
| 45 |
|
| 46 |
// TODO aconway 2015-05-05: Documentation for generated types. |
| 47 |
|
| 48 |
// CHandle holds an unsafe.Pointer to a proton C struct, the C type depends on the |
| 49 |
// Go type implementing this interface. For low level, at-your-own-risk use only. |
| 50 |
type CHandle interface { |
| 51 |
// CPtr returns the unsafe C pointer, equivalent to a C void*. |
| 52 |
CPtr() unsafe.Pointer |
| 53 |
} |
| 54 |
|
| 55 |
// Incref increases the refcount of a proton value, which prevents the |
| 56 |
// underlying C struct being freed until you call Decref(). |
| 57 |
// |
| 58 |
// It can be useful to "pin" a proton value in memory while it is in use by |
| 59 |
// goroutines other than the event loop goroutine. For example if you Incref() a |
| 60 |
// Link, the underlying object is not freed when the link is closed, so means |
| 61 |
// other goroutines can continue to safely use it as an index in a map or inject |
| 62 |
// it into the event loop goroutine. There will of course be an error if you try |
| 63 |
// to use a link after it is closed, but not a segmentation fault. |
| 64 |
func Incref(c CHandle) { |
| 65 |
if p := c.CPtr(); p != nil { |
| 66 |
C.pn_incref(p) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Decref decreases the refcount of a proton value, freeing the underlying C |
| 71 |
// struct if this is the last reference. Only call this if you previously |
| 72 |
// called Incref() for this value. |
| 73 |
func Decref(c CHandle) { |
| 74 |
if p := c.CPtr(); p != nil { |
| 75 |
C.pn_decref(p) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Event is an AMQP protocol event. |
| 80 |
type Event struct { |
| 81 |
pn *C.pn_event_t |
| 82 |
eventType EventType |
| 83 |
connection Connection |
| 84 |
transport Transport |
| 85 |
session Session |
| 86 |
link Link |
| 87 |
delivery Delivery |
| 88 |
injecter Injecter |
| 89 |
} |
| 90 |
|
| 91 |
func makeEvent(pn *C.pn_event_t, injecter Injecter) Event { |
| 92 |
return Event{ |
| 93 |
pn: pn, |
| 94 |
eventType: EventType(C.pn_event_type(pn)), |
| 95 |
connection: Connection{C.pn_event_connection(pn)}, |
| 96 |
transport: Transport{C.pn_event_transport(pn)}, |
| 97 |
session: Session{C.pn_event_session(pn)}, |
| 98 |
link: Link{C.pn_event_link(pn)}, |
| 99 |
delivery: Delivery{C.pn_event_delivery(pn)}, |
| 100 |
injecter: injecter, |
| 101 |
} |
| 102 |
} |
| 103 |
func (e Event) IsNil() bool { return e.eventType == EventType(0) } |
| 104 |
func (e Event) Type() EventType { return e.eventType } |
| 105 |
func (e Event) Connection() Connection { return e.connection } |
| 106 |
func (e Event) Transport() Transport { return e.transport } |
| 107 |
func (e Event) Session() Session { return e.session } |
| 108 |
func (e Event) Link() Link { return e.link } |
| 109 |
func (e Event) Delivery() Delivery { return e.delivery } |
| 110 |
func (e Event) String() string { return e.Type().String() } |
| 111 |
|
| 112 |
// Injecter should not be used in a handler function, but it can be passed to |
| 113 |
// other goroutines (via a channel or to a goroutine started by handler |
| 114 |
// functions) to let them inject functions back into the handlers goroutine. |
| 115 |
func (e Event) Injecter() Injecter { return e.injecter } |
| 116 |
|
| 117 |
// Data is an intermediate form of decoded AMQP data. |
| 118 |
type Data struct{ pn *C.pn_data_t } |
| 119 |
|
| 120 |
func (d Data) Free() { C.pn_data_free(d.pn) } |
| 121 |
func (d Data) CPtr() unsafe.Pointer { return unsafe.Pointer(d.pn) } |
| 122 |
func (d Data) Clear() { C.pn_data_clear(d.pn) } |
| 123 |
func (d Data) Rewind() { C.pn_data_rewind(d.pn) } |
| 124 |
func (d Data) Next() { C.pn_data_next(d.pn) } |
| 125 |
func (d Data) Error() error { return PnError(C.pn_data_error(d.pn)) } |
| 126 |
func (d Data) Empty() bool { return C.pn_data_size(d.pn) == 0 } |
| 127 |
|
| 128 |
func (d Data) String() string { |
| 129 |
str := C.pn_string(C.CString("")) |
| 130 |
defer C.pn_free(unsafe.Pointer(str)) |
| 131 |
C.pn_inspect(unsafe.Pointer(d.pn), str) |
| 132 |
return C.GoString(C.pn_string_get(str)) |
| 133 |
} |
| 134 |
|
| 135 |
// Unmarshal the value of d into value pointed at by ptr, see amqp.Unmarshal() for details |
| 136 |
func (d Data) Unmarshal(ptr interface{}) error { |
| 137 |
d.Rewind() |
| 138 |
d.Next() |
| 139 |
err := amqp.UnmarshalUnsafe(d.CPtr(), ptr) |
| 140 |
return err |
| 141 |
} |
| 142 |
|
| 143 |
// Marshal the value v into d, see amqp.Marshal() for details |
| 144 |
func (d Data) Marshal(v interface{}) error { |
| 145 |
d.Clear() |
| 146 |
return amqp.MarshalUnsafe(v, d.CPtr()) |
| 147 |
} |
| 148 |
|
| 149 |
// State holds the state flags for an AMQP endpoint. |
| 150 |
type State byte |
| 151 |
|
| 152 |
const ( |
| 153 |
SLocalUninit State = C.PN_LOCAL_UNINIT |
| 154 |
SLocalActive = C.PN_LOCAL_ACTIVE |
| 155 |
SLocalClosed = C.PN_LOCAL_CLOSED |
| 156 |
SRemoteUninit = C.PN_REMOTE_UNINIT |
| 157 |
SRemoteActive = C.PN_REMOTE_ACTIVE |
| 158 |
SRemoteClosed = C.PN_REMOTE_CLOSED |
| 159 |
) |
| 160 |
|
| 161 |
// Has is True if bits & state is non 0. |
| 162 |
func (s State) Has(bits State) bool { return s&bits != 0 } |
| 163 |
|
| 164 |
func (s State) LocalUninit() bool { return s.Has(SLocalUninit) } |
| 165 |
func (s State) LocalActive() bool { return s.Has(SLocalActive) } |
| 166 |
func (s State) LocalClosed() bool { return s.Has(SLocalClosed) } |
| 167 |
func (s State) RemoteUninit() bool { return s.Has(SRemoteUninit) } |
| 168 |
func (s State) RemoteActive() bool { return s.Has(SRemoteActive) } |
| 169 |
func (s State) RemoteClosed() bool { return s.Has(SRemoteClosed) } |
| 170 |
|
| 171 |
// Return a State containing just the local flags |
| 172 |
func (s State) Local() State { return State(s & C.PN_LOCAL_MASK) } |
| 173 |
|
| 174 |
// Return a State containing just the remote flags |
| 175 |
func (s State) Remote() State { return State(s & C.PN_REMOTE_MASK) } |
| 176 |
|
| 177 |
// Endpoint is the common interface for Connection, Link and Session. |
| 178 |
type Endpoint interface { |
| 179 |
// State is the open/closed state. |
| 180 |
State() State |
| 181 |
// Open an endpoint. |
| 182 |
Open() |
| 183 |
// Close an endpoint. |
| 184 |
Close() |
| 185 |
// Condition holds a local error condition. |
| 186 |
Condition() Condition |
| 187 |
// RemoteCondition holds a remote error condition. |
| 188 |
RemoteCondition() Condition |
| 189 |
// Human readable name |
| 190 |
String() string |
| 191 |
// Human readable endpoint type "sender-link", "session" etc. |
| 192 |
Type() string |
| 193 |
} |
| 194 |
|
| 195 |
// CloseError sets an error condition (if err != nil) on an endpoint and closes |
| 196 |
// the endpoint if not already closed |
| 197 |
func CloseError(e Endpoint, err error) { |
| 198 |
if err != nil && !e.Condition().IsSet() { |
| 199 |
e.Condition().SetError(err) |
| 200 |
} |
| 201 |
e.Close() |
| 202 |
} |
| 203 |
|
| 204 |
// EndpointError returns the remote error if there is one, the local error if not |
| 205 |
// nil if there is no error. |
| 206 |
func EndpointError(e Endpoint) error { |
| 207 |
err := e.RemoteCondition().Error() |
| 208 |
if err == nil { |
| 209 |
err = e.Condition().Error() |
| 210 |
} |
| 211 |
return err |
| 212 |
} |
| 213 |
|
| 214 |
const ( |
| 215 |
Received uint64 = C.PN_RECEIVED |
| 216 |
Accepted = C.PN_ACCEPTED |
| 217 |
Rejected = C.PN_REJECTED |
| 218 |
Released = C.PN_RELEASED |
| 219 |
Modified = C.PN_MODIFIED |
| 220 |
) |
| 221 |
|
| 222 |
// SettleAs is equivalent to d.Update(disposition); d.Settle() |
| 223 |
func (d Delivery) SettleAs(disposition uint64) { |
| 224 |
d.Update(disposition) |
| 225 |
d.Settle() |
| 226 |
} |
| 227 |
|
| 228 |
// Accept accepts and settles a delivery. |
| 229 |
func (d Delivery) Accept() { d.SettleAs(Accepted) } |
| 230 |
|
| 231 |
// Reject rejects and settles a delivery |
| 232 |
func (d Delivery) Reject() { d.SettleAs(Rejected) } |
| 233 |
|
| 234 |
// Release releases and settles a delivery |
| 235 |
// If delivered is true the delivery count for the message will be increased. |
| 236 |
func (d Delivery) Release(delivered bool) { |
| 237 |
if delivered { |
| 238 |
d.SettleAs(Modified) |
| 239 |
} else { |
| 240 |
d.SettleAs(Released) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
type DeliveryTag struct{ pn C.pn_delivery_tag_t } |
| 245 |
|
| 246 |
func (t DeliveryTag) String() string { return C.GoStringN(t.pn.start, C.int(t.pn.size)) } |
| 247 |
|
| 248 |
func (l Link) Recv(buf []byte) int { |
| 249 |
if len(buf) == 0 { |
| 250 |
return 0 |
| 251 |
} |
| 252 |
return int(C.pn_link_recv(l.pn, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))) |
| 253 |
} |
| 254 |
|
| 255 |
func (l Link) SendBytes(bytes []byte) int { |
| 256 |
return int(C.pn_link_send(l.pn, cPtr(bytes), cLen(bytes))) |
| 257 |
} |
| 258 |
|
| 259 |
func pnTag(tag string) C.pn_delivery_tag_t { |
| 260 |
bytes := []byte(tag) |
| 261 |
return C.pn_dtag(cPtr(bytes), cLen(bytes)) |
| 262 |
} |
| 263 |
|
| 264 |
func (l Link) Delivery(tag string) Delivery { |
| 265 |
return Delivery{C.pn_delivery(l.pn, pnTag(tag))} |
| 266 |
} |
| 267 |
|
| 268 |
func (l Link) Connection() Connection { return l.Session().Connection() } |
| 269 |
|
| 270 |
// Human-readable link description including name, source, target and direction. |
| 271 |
func (l Link) String() string { |
| 272 |
switch { |
| 273 |
case l.IsNil(): |
| 274 |
return fmt.Sprintf("<nil-link>") |
| 275 |
case l.IsSender(): |
| 276 |
return fmt.Sprintf("%s(%s->%s)", l.Name(), l.Source().Address(), l.Target().Address()) |
| 277 |
default: |
| 278 |
return fmt.Sprintf("%s(%s<-%s)", l.Name(), l.Target().Address(), l.Source().Address()) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func (l Link) Type() string { |
| 283 |
if l.IsSender() { |
| 284 |
return "sender-link" |
| 285 |
} else { |
| 286 |
return "receiver-link" |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// IsDrain calls pn_link_get_drain(), it conflicts with pn_link_drain() under the normal mapping. |
| 291 |
func (l Link) IsDrain() bool { |
| 292 |
return bool(C.pn_link_get_drain(l.pn)) |
| 293 |
} |
| 294 |
|
| 295 |
func cPtr(b []byte) *C.char { |
| 296 |
if len(b) == 0 { |
| 297 |
return nil |
| 298 |
} |
| 299 |
return (*C.char)(unsafe.Pointer(&b[0])) |
| 300 |
} |
| 301 |
|
| 302 |
func cLen(b []byte) C.size_t { |
| 303 |
return C.size_t(len(b)) |
| 304 |
} |
| 305 |
|
| 306 |
func (s Session) Sender(name string) Link { |
| 307 |
cname := C.CString(name) |
| 308 |
defer C.free(unsafe.Pointer(cname)) |
| 309 |
return Link{C.pn_sender(s.pn, cname)} |
| 310 |
} |
| 311 |
|
| 312 |
func (s Session) Receiver(name string) Link { |
| 313 |
cname := C.CString(name) |
| 314 |
defer C.free(unsafe.Pointer(cname)) |
| 315 |
return Link{C.pn_receiver(s.pn, cname)} |
| 316 |
} |
| 317 |
|
| 318 |
func (t Transport) String() string { |
| 319 |
return fmt.Sprintf("(Transport)(%p)", t.CPtr()) |
| 320 |
} |
| 321 |
|
| 322 |
// Unique (per process) string identifier for a connection, useful for debugging. |
| 323 |
func (c Connection) String() string { |
| 324 |
// Use the transport address to match the default transport logs from PN_TRACE. |
| 325 |
return fmt.Sprintf("(Connection)(%p)", c.Transport().CPtr()) |
| 326 |
} |
| 327 |
|
| 328 |
func (c Connection) Type() string { |
| 329 |
return "connection" |
| 330 |
} |
| 331 |
|
| 332 |
// Head functions don't follow the normal naming conventions so missed by the generator. |
| 333 |
|
| 334 |
func (c Connection) LinkHead(s State) Link { |
| 335 |
return Link{C.pn_link_head(c.pn, C.pn_state_t(s))} |
| 336 |
} |
| 337 |
|
| 338 |
func (c Connection) SessionHead(s State) Session { |
| 339 |
return Session{C.pn_session_head(c.pn, C.pn_state_t(s))} |
| 340 |
} |
| 341 |
|
| 342 |
func (c Connection) Links(state State) (links []Link) { |
| 343 |
for l := c.LinkHead(state); !l.IsNil(); l = l.Next(state) { |
| 344 |
links = append(links, l) |
| 345 |
} |
| 346 |
return |
| 347 |
} |
| 348 |
|
| 349 |
func (c Connection) Sessions(state State) (sessions []Session) { |
| 350 |
for s := c.SessionHead(state); !s.IsNil(); s = s.Next(state) { |
| 351 |
sessions = append(sessions, s) |
| 352 |
} |
| 353 |
return |
| 354 |
} |
| 355 |
|
| 356 |
// SetPassword takes []byte not string because it is impossible to erase a string |
| 357 |
// from memory reliably. Proton will not keep the password in memory longer than |
| 358 |
// needed, the caller should overwrite their copy on return. |
| 359 |
// |
| 360 |
// The password must not contain embedded nul characters, a trailing nul is ignored. |
| 361 |
func (c Connection) SetPassword(password []byte) { |
| 362 |
if len(password) == 0 || password[len(password)-1] != 0 { |
| 363 |
password = append(password, 0) // Proton requires a terminating null. |
| 364 |
} |
| 365 |
C.pn_connection_set_password(c.pn, (*C.char)(unsafe.Pointer(&password[0]))) |
| 366 |
} |
| 367 |
|
| 368 |
func (s Session) String() string { |
| 369 |
return fmt.Sprintf("(Session)(%p)", s.pn) // TODO aconway 2016-09-12: should print channel number. |
| 370 |
} |
| 371 |
|
| 372 |
func (s Session) Type() string { return "session" } |
| 373 |
|
| 374 |
// Error returns an instance of amqp.Error or nil. |
| 375 |
func (c Condition) Error() error { |
| 376 |
if c.IsNil() || !c.IsSet() { |
| 377 |
return nil |
| 378 |
} |
| 379 |
return amqp.Error{Name: c.Name(), Description: c.Description()} |
| 380 |
} |
| 381 |
|
| 382 |
// Set a Go error into a condition, converting to an amqp.Error using amqp.MakeError |
| 383 |
func (c Condition) SetError(err error) { |
| 384 |
if err != nil { |
| 385 |
cond := amqp.MakeError(err) |
| 386 |
c.SetName(cond.Name) |
| 387 |
c.SetDescription(cond.Description) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
func (c Connection) Session() (Session, error) { |
| 392 |
s := Session{C.pn_session(c.pn)} |
| 393 |
if s.IsNil() { |
| 394 |
return s, Connection(c).Error() |
| 395 |
} |
| 396 |
return s, nil |
| 397 |
} |
| 398 |
|
| 399 |
// pnTime converts Go time.Time to Proton millisecond Unix time. |
| 400 |
// |
| 401 |
// Note: t.isZero() is converted to C.pn_timestamp_t(0) and vice-versa. These |
| 402 |
// are used as "not set" sentinel values by the Go and Proton APIs, so it is |
| 403 |
// better to conserve the "zeroness" even though they don't represent the same |
| 404 |
// time instant. |
| 405 |
// |
| 406 |
func pnTime(t time.Time) (pnt C.pn_timestamp_t) { |
| 407 |
if !t.IsZero() { |
| 408 |
pnt = C.pn_timestamp_t(t.Unix()*1000 + int64(t.Nanosecond())/int64(time.Millisecond)) |
| 409 |
} |
| 410 |
return |
| 411 |
} |
| 412 |
|
| 413 |
// goTime converts a pn_timestamp_t to a Go time.Time. |
| 414 |
// |
| 415 |
// Note: C.pn_timestamp_t(0) is converted to a zero time.Time and |
| 416 |
// vice-versa. These are used as "not set" sentinel values by the Go and Proton |
| 417 |
// APIs, so it is better to conserve the "zeroness" even though they don't |
| 418 |
// represent the same time instant. |
| 419 |
// |
| 420 |
func goTime(pnt C.pn_timestamp_t) (t time.Time) { |
| 421 |
if pnt != 0 { |
| 422 |
t = time.Unix(int64(pnt/1000), int64(pnt%1000)*int64(time.Millisecond)) |
| 423 |
} |
| 424 |
return |
| 425 |
} |
| 426 |
|
| 427 |
// Special treatment for Transport.Head, return value is unsafe.Pointer not string |
| 428 |
func (t Transport) Head() unsafe.Pointer { |
| 429 |
return unsafe.Pointer(C.pn_transport_head(t.pn)) |
| 430 |
} |
| 431 |
|
| 432 |
// Special treatment for Transport.Tail, return value is unsafe.Pointer not string |
| 433 |
func (t Transport) Tail() unsafe.Pointer { |
| 434 |
return unsafe.Pointer(C.pn_transport_tail(t.pn)) |
| 435 |
} |
| 436 |
|
| 437 |
// Special treatment for Transport.Push, takes []byte instead of char*, size |
| 438 |
func (t Transport) Push(bytes []byte) int { |
| 439 |
return int(C.pn_transport_push(t.pn, (*C.char)(unsafe.Pointer(&bytes[0])), C.size_t(len(bytes)))) |
| 440 |
} |
| 441 |
|
| 442 |
// Get the SASL object for the transport. |
| 443 |
func (t Transport) SASL() SASL { |
| 444 |
return SASL{C.pn_sasl(t.pn)} |
| 445 |
} |
| 446 |
|
| 447 |
// Do we support extended SASL negotiation? |
| 448 |
// All implementations of Proton support ANONYMOUS and EXTERNAL on both |
| 449 |
// client and server sides and PLAIN on the client side. |
| 450 |
// |
| 451 |
// Extended SASL implememtations use an external library (Cyrus SASL) |
| 452 |
// to support other mechanisms beyond these basic ones. |
| 453 |
func SASLExtended() bool { |
| 454 |
return bool(C.pn_sasl_extended()) |
| 455 |
} |