add readme, now the driver is ok!
This commit is contained in:
parent
7dd0faf24b
commit
95120c77b1
6 changed files with 145 additions and 29 deletions
11
sonic/actions.go
Normal file
11
sonic/actions.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package sonic
|
||||
|
||||
type Action string
|
||||
|
||||
const (
|
||||
Consolidate Action = "consolidate"
|
||||
)
|
||||
|
||||
func IsActionValid(action Action) bool {
|
||||
return action == Consolidate
|
||||
}
|
||||
|
|
@ -10,9 +10,13 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var ClosedError = errors.New("sonic connection is closed")
|
||||
var (
|
||||
ClosedError = errors.New("sonic connection is closed")
|
||||
InvalidChanName = errors.New("invalid channel name")
|
||||
InvalidActionName = errors.New("invalid action name")
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
type Driver struct {
|
||||
Host string
|
||||
Port int
|
||||
Password string
|
||||
|
|
@ -23,9 +27,20 @@ type Connection struct {
|
|||
closed bool
|
||||
}
|
||||
|
||||
func (c *Connection) Connect() error {
|
||||
func NewControl(host string, port int, password string) (*Driver, error) {
|
||||
driver := &Driver{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Password: password,
|
||||
Channel: Ingest,
|
||||
}
|
||||
|
||||
return driver, driver.connect()
|
||||
}
|
||||
|
||||
func (c *Driver) connect() error {
|
||||
if !IsChannelValid(c.Channel) {
|
||||
return errors.New("invalid channel name")
|
||||
return InvalidChanName
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port))
|
||||
|
|
@ -49,7 +64,7 @@ func (c *Connection) Connect() error {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Connection) read() (string, error) {
|
||||
func (c *Driver) read() (string, error) {
|
||||
if c.closed {
|
||||
return "", ClosedError
|
||||
}
|
||||
|
|
@ -75,7 +90,7 @@ func (c *Connection) read() (string, error) {
|
|||
return str, nil
|
||||
}
|
||||
|
||||
func (c Connection) write(str string) error {
|
||||
func (c Driver) write(str string) error {
|
||||
if c.closed {
|
||||
return ClosedError
|
||||
}
|
||||
|
|
@ -83,34 +98,50 @@ func (c Connection) write(str string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (c *Connection) Quit() error {
|
||||
func (c *Driver) Quit() error {
|
||||
err := c.write("QUIT")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// should get ENDED
|
||||
_, err = c.read()
|
||||
c.clean()
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Connection) Ping() error {
|
||||
func (c Driver) Ping() error {
|
||||
err := c.write("PING")
|
||||
if err != nil {
|
||||
fmt.Println("err write")
|
||||
return err
|
||||
}
|
||||
|
||||
// should get PONG
|
||||
_, err = c.read()
|
||||
if err != nil {
|
||||
fmt.Println("err read")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Connection) clean() {
|
||||
func (c Driver) Trigger(action Action) error {
|
||||
if IsActionValid(action) {
|
||||
return InvalidActionName
|
||||
}
|
||||
err := c.write(fmt.Sprintf("TRIGGER %s", action))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// should get OK
|
||||
_, err = c.read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Driver) clean() {
|
||||
c.closed = true
|
||||
_ = c.conn.Close()
|
||||
c.conn = nil
|
||||
|
|
@ -28,7 +28,23 @@ const (
|
|||
)
|
||||
|
||||
type IngesterChannel struct {
|
||||
*Connection
|
||||
*Driver
|
||||
}
|
||||
|
||||
func NewIngester(host string, port int, password string) (Ingestable, error) {
|
||||
driver := &Driver{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Password: password,
|
||||
Channel: Ingest,
|
||||
}
|
||||
err := driver.connect()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return IngesterChannel{
|
||||
Driver: driver,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (i IngesterChannel) Push(collection, bucket, object, text string) (err error) {
|
||||
|
|
|
|||
|
|
@ -18,7 +18,23 @@ const (
|
|||
)
|
||||
|
||||
type SearchChannel struct {
|
||||
*Connection
|
||||
*Driver
|
||||
}
|
||||
|
||||
func NewSearch(host string, port int, password string) (Searchable, error) {
|
||||
driver := &Driver{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Password: password,
|
||||
Channel: Search,
|
||||
}
|
||||
err := driver.connect()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return SearchChannel{
|
||||
Driver: driver,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s SearchChannel) Query(collection, bucket, term string, limit, offset int) (results []string, err error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue