Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

168

169

170

171

172

173

174

175

176

177

import os 

 

import txamqp.spec 

 

from txamqp.protocol import AMQClient, TwistedDelegate 

 

from twisted.internet import error, protocol, reactor 

from twisted.internet.defer import inlineCallbacks, Deferred, returnValue 

 

from ion.core import ioninit 

from ion.util import ionlog 

from ion.util.path import adjust_dir 

 

CONF = ioninit.config(__name__) 

log = ionlog.getLogger(__name__) 

 

 

class BrokerController: 

 

 

 

    def __init__(self, *args, **kwargs): 

        self._privileged_broker = CONF.getValue('privileged_broker_connection') 

        spec_path = adjust_dir(CONF.getValue('amqp_spec')) 

        if not os.path.isfile(spec_path): 

            log.critical('Could not locate AMQP spec file at: ' + spec_path) 

 

        self._amqp_spec = txamqp.spec.load(spec_path) 

        self.queues = [] 

        self.exchanges = [] 

        self.connectors = [] 

 

 

    @inlineCallbacks 

    def start(self): 

        """ 

        """ 

        try: 

            self.client = yield self._connect() 

        except txamqp.client.Closed: 

            log.critical( 

                    "failed to connect to amqp broker:\n " +    \ 

                    "\tusername: %s\n " +                       \ 

                    "\tpassword: %s\n " +                       \ 

                    "\thost:     %s\n " +                       \ 

                    "\tport:     %s\n " +                       \ 

                    "\tvhost:    %s" % ( 

                                str(self._privileged_broker['username']), 

                                str(self._privileged_broker['password']), 

                                str(self._privileged_broker['host']), 

                                str(self._privileged_broker['port']), 

                                str(self._privileged_broker['vhost']))) 

 

        self.channel = yield self.client.channel(1) 

        yield self.channel.channel_open() 

 

 

    @inlineCallbacks 

    def stop(self): 

        """ 

        """ 

        log.info("Stopping BrokerController") 

        # for ch, q in self.queues: 

        #    yield ch.queue_delete(queue=q) 

        # for ch, ex in self.exchanges: 

        #    yield ch.exchange_delete(exchange=ex) 

        #    log.info('broker_controller: delete_exchange()  name=' + ex) 

        for connector in self.connectors: 

            yield connector.disconnect() 

 

 

 

    @inlineCallbacks 

    def _connect(self): 

        host = self._privileged_broker['host'] 

        port = self._privileged_broker['port'] 

        username = self._privileged_broker['username'] 

        password = self._privileged_broker['password'] 

        vhost = self._privileged_broker['vhost'] 

        heartbeat = self._privileged_broker['heartbeat'] 

 

        delegate = TwistedDelegate() 

        onConn = Deferred() 

        p = AMQClient(delegate, vhost, self._amqp_spec, heartbeat=heartbeat) 

        f = protocol._InstanceFactory(reactor, p, onConn) 

        c = reactor.connectTCP(host, port, f) 

        def errb(thefailure): 

            thefailure.trap(error.ConnectionRefusedError) 

            log.critical( 

                    "failed to connect to amqp broker:\n " +    \ 

                    "\tusername: %s\n " +                       \ 

                    "\tpassword: %s\n " +                       \ 

                    "\thost:     %s\n " +                       \ 

                    "\tport:     %s\n " +                       \ 

                    "\tvhost:    %s" % ( 

                                str(self._privileged_broker['username']), 

                                str(self._privileged_broker['password']), 

                                str(self._privileged_broker['host']), 

                                str(self._privileged_broker['port']), 

                                str(self._privileged_broker['vhost']))) 

            thefailure.raiseException() 

        onConn.addErrback(errb) 

 

        self.connectors.append(c) 

        client = yield onConn 

 

        yield client.authenticate(username, password) 

        returnValue(client) 

 

 

    """ 

    Creates an exchange. 

     

    """ 

    @inlineCallbacks 

    def create_exchange( 

                 self, 

                 channel=None, 

                 ticket=0, 

                 exchange='', 

                 type='', 

                 passive=False, 

                 durable=False, 

                 auto_delete=True, 

                 internal=False, 

                 nowait=False, 

                 arguments={} 

                    ): 

 

        channel = channel or self.channel 

        reply = yield channel.exchange_declare( 

                ticket, 

                exchange, 

                type, 

                passive, 

                durable, 

                auto_delete, 

                internal, nowait, 

                arguments 

        ) 

        self.exchanges.append((channel,exchange)) 

        log.info('broker_controller: create_exchange()  name=' + exchange) 

        returnValue(reply) 

 

 

    @inlineCallbacks 

    def create_queue( 

                     self, 

                     name="", 

                    ): 

        q = yield self.channel.queue_declare( 

                queue=name, 

                durable=False, 

                exclusive=True, 

                auto_delete=True 

        ) 

        returnValue(q) 

 

 

    @inlineCallbacks 

    def create_binding( 

                    self, 

                    name="", 

                    exchangename="", 

                    routingkey="" 

                       ): 

        b = yield self.channel.queue_bind( 

                queue=exchangename + '.' + name, 

                exchange=exchangename, 

                routing_key=routingkey 

        ) 

 

        # self.queues.append((channel, reply.queue)) 

        returnValue(b)