sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/utils/client.go

69 lines
1.2 KiB
Go
Raw Normal View History

2017-12-16 23:20:46 +01:00
package utils
import (
"encoding/xml"
"net"
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/yaja/model"
"dev.sum7.eu/genofire/yaja/xmpp/base"
2017-12-16 23:20:46 +01:00
)
type Client struct {
Log *log.Entry
Conn net.Conn
Out *xml.Encoder
In *xml.Decoder
JID *xmppbase.JID
2017-12-16 23:20:46 +01:00
account *model.Account
Messages chan interface{}
close chan interface{}
}
func NewClient(conn net.Conn, level log.Level) *Client {
logger := log.New()
logger.SetLevel(level)
client := &Client{
2017-12-17 15:39:36 +01:00
Conn: conn,
Log: log.NewEntry(logger),
In: xml.NewDecoder(conn),
Out: xml.NewEncoder(conn),
2017-12-17 17:50:51 +01:00
Messages: make(chan interface{}),
2017-12-17 15:39:36 +01:00
close: make(chan interface{}),
2017-12-16 23:20:46 +01:00
}
return client
}
func (client *Client) SetConnecting(conn net.Conn) {
client.Conn = conn
client.In = xml.NewDecoder(conn)
client.Out = xml.NewEncoder(conn)
}
func (client *Client) Read() (*xml.StartElement, error) {
for {
nextToken, err := client.In.Token()
if err != nil {
return nil, err
}
switch nextToken.(type) {
case xml.StartElement:
element := nextToken.(xml.StartElement)
return &element, nil
}
}
}
func (client *Client) OnClose() <-chan interface{} {
return client.close
}
func (client *Client) Close() {
client.close <- true
client.Conn.Close()
}