//Kick kick stun func Kick(sn string) { _, err := rabbit.channel.QueueDeclare( "stun_queue", // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, //amqp.Table{"x-message-ttl": int32(300000)}, // arguments in ms ) if err != nil { log.Info("[amqp] QueueDeclare error: %s", err) return } //stun kick props := MessageProperties{ CorrelationID: "", ReplyTo: "", ContentEncoding: "UTF-8", ContentType: "text/plain", } kickmsg := Message{MessageProperties: props, Body: []byte(sn)} err = rabbit.publish("", "stun_queue", kickmsg) if err != nil { log.Info("[amqp] send error: %s", err) return } return }
//SendMsg send msg func SendMsg(msg Message, sendTo string) (err error) { rabbit := new(RabbitMQ) if err = rabbit.Connect(); err != nil { log.Info("[amqp] connect error: %s", err) return } defer rabbit.Close() _, err = rabbit.channel.QueueDeclare( sendTo, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait amqp.Table{"x-message-ttl": int32(300000)}, // arguments in ms ) if err != nil { log.Info("[amqp] QueueDeclare error: %s", err) return } err = rabbit.publish("", sendTo, msg) if err != nil { log.Info("[amqp] send error: %s", err) return } return }
//Connect create a new amqp conn func (r *RabbitMQ) Connect() (err error) { r.conn, err = amqp.Dial(Conf.AMQPUrl) if err != nil { log.Info("[amqp] connect error: %s\n", err) return err } r.channel, err = r.conn.Channel() if err != nil { log.Info("[amqp] get channel error: %s\n", err) return err } r.done = make(chan error) return nil }
//SendText send text func SendText(text, sendTo string) (err error) { rabbit := new(RabbitMQ) if err := rabbit.Connect(); err != nil { log.Info("[amqp] connect error: %s", err) return err } defer rabbit.Close() err = rabbit.publishText("", sendTo, text) if err != nil { log.Info("[amqp] send error: %s", err) return err } return nil }
//Reply rpc reply func Reply(msg Message, sendTo string) (err error) { rabbit := new(RabbitMQ) if err = rabbit.Connect(); err != nil { log.Info("[amqp] connect error: %s", err) return } defer rabbit.Close() err = rabbit.publish("", sendTo, msg) if err != nil { log.Info("[amqp] send error: %s", err) return } return }
//ReciveMsg query msg func ReciveMsg(queue string) (message Message) { rabbit := new(RabbitMQ) if err := rabbit.Connect(); err != nil { log.Info("[amqp] connect error: %s", err) return message } defer rabbit.Close() msg, err := rabbit.consumeQueue(queue) if err != nil { log.Info("[amqp] get error: %s", err) return message } return msg }
func initHTTP(addrs []string) (err error) { var ( bind string listener *net.TCPListener addr *net.TCPAddr server *http.Server httpServeMux = http.NewServeMux() ) globalSessions = session.NewManager() httpServeMux.HandleFunc("/ACS/tr069", tr069) for _, bind = range addrs { if addr, err = net.ResolveTCPAddr("tcp4", bind); err != nil { log.Error("net.ResolveTCPAddr(\"tcp4\", \"", bind, "\") error:", err) return } if listener, err = net.ListenTCP("tcp4", addr); err != nil { log.Error("net.ListenTCP(\"tcp4\", \"", bind, "\") error:", err) return } server = &http.Server{Handler: httpServeMux} log.Info("start http listen: \"%s\"", bind) go func() { if err = server.Serve(listener); err != nil { log.Error("server.Serve(\"%s\") error(%v)", bind, err) panic(err) } }() } return }
//Close close conn func (r *RabbitMQ) Close() (err error) { err = r.conn.Close() if err != nil { log.Info("[amqp] close error: %s", err) return err } return nil }
//PublishText publish text msg func (r *RabbitMQ) publishText(exchange, key, text string) (err error) { err = r.channel.Publish(exchange, key, false, false, amqp.Publishing{ ContentType: "text/plain", CorrelationId: "", ReplyTo: "", Body: []byte(text), }, ) if err != nil { log.Info("[amqp] publish message error: %s", err) return err } return nil }
//Publish publish amqp msg func (r *RabbitMQ) publish(exchange, key string, msg Message) (err error) { err = r.channel.Publish(exchange, key, false, false, amqp.Publishing{ Headers: msg.Headers, ContentType: msg.ContentType, ContentEncoding: msg.ContentEncoding, CorrelationId: msg.CorrelationID, //Expiration: msg.Expiration, Body: msg.Body, }, ) if err != nil { log.Info("[amqp] publish message error: %s", err) return err } return nil }
//ConsumeQueue bindding queue func (r *RabbitMQ) consumeQueue(queue string) (message Message, err error) { deliver, ok, err := r.channel.Get(queue, true) if err != nil { log.Info("[amqp] consume queue error: %s", err) return message, err } if ok { return Message{ MessageProperties: MessageProperties{ Headers: deliver.Headers, CorrelationID: deliver.CorrelationId, ReplyTo: deliver.ReplyTo, ContentEncoding: deliver.ContentEncoding, ContentType: deliver.ContentType, }, Body: deliver.Body, }, nil } return message, nil }
func (q *MsgQueue) fetchMsg() { log.Info("fetch") conn, err := amqp.Dial(Conf.AMQPUrl) if err != nil { log.Error("Failed to connect to RabbitMQ", err) return } defer conn.Close() ch, err := conn.Channel() if err != nil { log.Error("Failed to open a channel", err) return } defer ch.Close() qu, err := ch.QueueDeclare( Conf.ACSQueue, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) if err != nil { log.Error("Failed to declare a queue ", err) return } err = ch.Qos( 1, // prefetch count 0, // prefetch size false, // global ) if err != nil { log.Error("Failed to set QoS", err) return } msgs, err := ch.Consume( qu.Name, // queue "", // consumer false, // auto-ack false, // exclusive false, // no-local false, // no-wait nil, // args ) if err != nil { log.Error("Failed to register a consumer", err) return } forever := make(chan bool) go func(flag chan bool) { log.Error("closing", <-conn.NotifyClose(make(chan *amqp.Error))) flag <- true }(forever) go func() { for deliver := range msgs { if Debug { log.Debug("Received a message: %s", deliver.Body) log.Debug("Received a message replyTo: %s", deliver.ReplyTo) } deliver.Ack(false) if deliver.Headers["DeviceID"] != nil { sn := deliver.Headers["DeviceID"].(string) msg := Message{ MessageProperties: MessageProperties{ Headers: deliver.Headers, CorrelationID: deliver.CorrelationId, ReplyTo: deliver.ReplyTo, ContentEncoding: deliver.ContentEncoding, ContentType: deliver.ContentType, }, Body: deliver.Body, } //q.Push(sn, msg) var channel *Channel if channel = DefaultBucket.Channel(sn); channel != nil { //根据sub key找到user对应的channel err := channel.Push(msg.MessageProperties, msg.Body) if err != nil { log.Warn("push msg into %s ch err:%s", sn, err) } } else { //new channel channel = NewChannel(Conf.RingSize) err := channel.Push(msg.MessageProperties, msg.Body) if err != nil { log.Warn("push msg into %s new ch err:%s", sn, err) } DefaultBucket.Put(sn, channel) } //send to stun //dot_count := bytes.Count(d.Body, []byte(".")) //t := time.Duration(dot_count) //time.Sleep(t * time.Second) //models.Kick(sn) SendText(sn, "stun_queue") } } }() <-forever }