Debian Codegraph @

go/pkg/electron/container.go

raw
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 electron
21
22 import (
23 "net"
24 "strconv"
25 "sync/atomic"
26
27 "github.com/apache/qpid-proton/go/pkg/proton"
28 )
29
30 // Container is an AMQP container, it represents a single AMQP "application"
31 // which can have multiple client or server connections.
32 //
33 // Each Container in a distributed AMQP application must have a unique
34 // container-id which is applied to its connections.
35 //
36 // Create with NewContainer()
37 //
38 type Container interface {
39 // Id is a unique identifier for the container in your distributed application.
40 Id() string
41
42 // Connection creates a connection associated with this container.
43 Connection(conn net.Conn, opts ...ConnectionOption) (Connection, error)
44
45 // Dial is shorthand for
46 // conn, err := net.Dial(); c, err := Connection(conn, opts...)
47 // See net.Dial() for the meaning of the network, address arguments.
48 Dial(network string, address string, opts ...ConnectionOption) (Connection, error)
49
50 // Accept is shorthand for:
51 // conn, err := l.Accept(); c, err := Connection(conn, append(opts, Server()...)
52 Accept(l net.Listener, opts ...ConnectionOption) (Connection, error)
53
54 // String returns Id()
55 String() string
56 }
57
58 type container struct {
59 id string
60 tagCounter uint64
61 }
62
63 func (cont *container) nextTag() string {
64 return strconv.FormatUint(atomic.AddUint64(&cont.tagCounter, 1), 32)
65 }
66
67 // NewContainer creates a new container. The id must be unique in your
68 // distributed application, all connections created by the container
69 // will have this container-id.
70 //
71 // If id == "" a random UUID will be generated for the id.
72 func NewContainer(id string) Container {
73 if id == "" {
74 id = proton.UUID4().String()
75 }
76 cont := &container{id: id}
77 return cont
78 }
79
80 func (cont *container) Id() string { return cont.id }
81
82 func (cont *container) String() string { return cont.Id() }
83
84 func (cont *container) nextLinkName() string {
85 return cont.id + "@" + cont.nextTag()
86 }
87
88 func (cont *container) Connection(conn net.Conn, opts ...ConnectionOption) (Connection, error) {
89 return NewConnection(conn, append(opts, Parent(cont))...)
90 }
91
92 func (cont *container) Dial(network, address string, opts ...ConnectionOption) (c Connection, err error) {
93 conn, err := net.Dial(network, address)
94 if err == nil {
95 c, err = cont.Connection(conn, opts...)
96 }
97 return
98 }
99
100 func (cont *container) Accept(l net.Listener, opts ...ConnectionOption) (c Connection, err error) {
101 conn, err := l.Accept()
102 if err == nil {
103 c, err = cont.Connection(conn, append([]ConnectionOption{Server()}, opts...)...)
104 }
105 return
106 }