This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/tg/broker.go

120 lines
2.7 KiB
Go
Raw Normal View History

2016-02-09 10:01:05 +00:00
package tg
import (
"encoding/json"
"fmt"
"log"
"net"
)
// Broker is a broker connection handler with callback management functions
2016-02-09 10:01:05 +00:00
type Broker struct {
Socket net.Conn
Callbacks []BrokerCallback
cbFree int
2016-02-09 10:01:05 +00:00
}
// ConnectToBroker creates a Broker connection
2016-02-09 10:01:05 +00:00
func ConnectToBroker(brokerAddr string) (*Broker, error) {
sock, err := net.Dial("tcp", brokerAddr)
if err != nil {
return nil, err
}
broker := new(Broker)
broker.Socket = sock
broker.Callbacks = make([]BrokerCallback, 0)
broker.cbFree = 0
2016-02-09 10:01:05 +00:00
return broker, nil
}
// Close closes a broker connection
2016-02-09 10:01:05 +00:00
func (b *Broker) Close() {
b.Socket.Close()
}
// SendTextMessage sends a HTML-styles text message to a specific chat.
// A reply_to message ID can be specified as optional parameter.
2016-02-09 10:33:38 +00:00
func (b *Broker) SendTextMessage(chat *APIChat, text string, original *int) {
2016-02-09 10:01:05 +00:00
cmd := ClientCommand{
Type: CmdSendTextMessage,
TextMessageData: &ClientTextMessageData{
2016-02-09 10:33:38 +00:00
Text: text,
ChatID: chat.ChatID,
ReplyID: original,
2016-02-09 10:01:05 +00:00
},
}
// Encode command and send to broker
2016-02-09 10:04:27 +00:00
data, err := json.Marshal(cmd)
2016-02-09 10:01:05 +00:00
if err != nil {
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
}
2016-02-09 10:39:00 +00:00
fmt.Fprintln(b.Socket, string(data))
2016-02-09 10:01:05 +00:00
}
// GetFile sends a file retrieval request to the Broker.
// This function is asynchronous as data will be delivered to the given callback.
func (b *Broker) GetFile(fileID string, fn BrokerCallback) int {
cid := b.RegisterCallback(fn)
// Make file request
return cid
}
// RegisterCallback assigns a callback ID to the given callback and puts it on the callback list.
// This function should never be called by clients.
func (b *Broker) RegisterCallback(fn BrokerCallback) int {
cblen := len(b.Callbacks)
// List is full, append to end
if b.cbFree == cblen {
b.Callbacks = append(b.Callbacks, fn)
b.cbFree++
return cblen
}
// List is not full, use empty slot and find next one
id := b.cbFree
b.Callbacks[id] = fn
next := b.cbFree + 1
for ; next < cblen; next++ {
if b.Callbacks[next] == nil {
break
}
}
b.cbFree = next
return id
}
// RemoveCallback removes a callback from the callback list by ID.
// This function should never be called by clients.
func (b *Broker) RemoveCallback(id int) {
b.Callbacks[id] = nil
if id < b.cbFree {
b.cbFree = id
}
b.resizeCbArray()
}
// SpliceCallback retrieves a callback by ID and removes it from the list.
// This function should never be called by clients.
func (b *Broker) SpliceCallback(id int) BrokerCallback {
defer b.RemoveCallback(id)
return b.Callbacks[id]
}
func (b *Broker) resizeCbArray() {
var i int
cut := false
for i = len(b.Callbacks); i > 0; i-- {
if b.Callbacks[i-1] != nil {
break
}
cut = true
}
if cut {
b.Callbacks = b.Callbacks[0:i]
if b.cbFree > i {
b.cbFree = i
}
}
}