| 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 |
package proton |
| 21 |
|
| 22 |
// #include <proton/types.h> |
| 23 |
// #include <proton/message.h> |
| 24 |
// #include <proton/codec.h> |
| 25 |
import "C" |
| 26 |
|
| 27 |
import ( |
| 28 |
"fmt" |
| 29 |
"strconv" |
| 30 |
"sync/atomic" |
| 31 |
|
| 32 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 33 |
) |
| 34 |
|
| 35 |
// HasMessage is true if all message data is available. |
| 36 |
// Equivalent to !d.isNil && d.Readable() && !d.Partial() |
| 37 |
func (d Delivery) HasMessage() bool { return !d.IsNil() && d.Readable() && !d.Partial() } |
| 38 |
|
| 39 |
// Message decodes the message contained in a delivery. |
| 40 |
// |
| 41 |
// Must be called in the correct link context with this delivery as the current message, |
| 42 |
// handling an MMessage event is always a safe context to call this function. |
| 43 |
// |
| 44 |
// Will return an error if message is incomplete or not current. |
| 45 |
func (delivery Delivery) Message() (amqp.Message, error) { |
| 46 |
var err error |
| 47 |
bytes, err := delivery.MessageBytes() |
| 48 |
if err == nil { |
| 49 |
m := amqp.NewMessage() |
| 50 |
err = m.Decode(bytes) |
| 51 |
return m, err |
| 52 |
} |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
// MessageBytes extracts the raw message bytes contained in a delivery. |
| 57 |
// |
| 58 |
// Must be called in the correct link context with this delivery as the current message, |
| 59 |
// handling an MMessage event is always a safe context to call this function. |
| 60 |
// |
| 61 |
// Will return an error if message is incomplete or not current. |
| 62 |
func (delivery Delivery) MessageBytes() ([]byte, error) { |
| 63 |
if !delivery.Readable() { |
| 64 |
return nil, fmt.Errorf("delivery is not readable") |
| 65 |
} |
| 66 |
if delivery.Partial() { |
| 67 |
return nil, fmt.Errorf("delivery has partial message") |
| 68 |
} |
| 69 |
data := make([]byte, delivery.Pending()) |
| 70 |
result := delivery.Link().Recv(data) |
| 71 |
if result != len(data) { |
| 72 |
return nil, fmt.Errorf("cannot receive message: %s", PnErrorCode(result)) |
| 73 |
} |
| 74 |
return data, nil |
| 75 |
} |
| 76 |
|
| 77 |
// Process-wide atomic counter for generating tag names |
| 78 |
var tagCounter uint64 |
| 79 |
|
| 80 |
func nextTag() string { |
| 81 |
return strconv.FormatUint(atomic.AddUint64(&tagCounter, 1), 32) |
| 82 |
} |
| 83 |
|
| 84 |
// Send sends a amqp.Message over a Link. |
| 85 |
// Returns a Delivery that can be use to determine the outcome of the message. |
| 86 |
func (link Link) Send(m amqp.Message) (Delivery, error) { |
| 87 |
bytes, err := m.Encode(nil) |
| 88 |
if err != nil { |
| 89 |
return Delivery{}, err |
| 90 |
} |
| 91 |
d, err := link.SendMessageBytes(bytes) |
| 92 |
return d, err |
| 93 |
} |
| 94 |
|
| 95 |
// SendMessageBytes sends encoded bytes of an amqp.Message over a Link. |
| 96 |
// Returns a Delivery that can be use to determine the outcome of the message. |
| 97 |
func (link Link) SendMessageBytes(bytes []byte) (Delivery, error) { |
| 98 |
if !link.IsSender() { |
| 99 |
return Delivery{}, fmt.Errorf("attempt to send message on receiving link") |
| 100 |
} |
| 101 |
delivery := link.Delivery(nextTag()) |
| 102 |
result := link.SendBytes(bytes) |
| 103 |
link.Advance() |
| 104 |
if result != len(bytes) { |
| 105 |
if result < 0 { |
| 106 |
return delivery, fmt.Errorf("send failed %v", PnErrorCode(result)) |
| 107 |
} else { |
| 108 |
return delivery, fmt.Errorf("send incomplete %v of %v", result, len(bytes)) |
| 109 |
} |
| 110 |
} |
| 111 |
if link.RemoteSndSettleMode() == SndSettled { |
| 112 |
delivery.Settle() |
| 113 |
} |
| 114 |
return delivery, nil |
| 115 |
} |