github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/cloud/pkg/cloudhub/common/io/hubio.go (about)

     1  package io
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/kubeedge/beehive/pkg/core/model"
     7  	"github.com/kubeedge/viaduct/pkg/conn"
     8  )
     9  
    10  // CloudHubIO handle the IO operation from connection
    11  type CloudHubIO interface {
    12  	SetReadDeadline(time.Time) error
    13  	SetWriteDeadline(time.Time) error
    14  	ReadData(*model.Message) (int, error)
    15  	WriteData(*model.Message) error
    16  	Close() error
    17  }
    18  
    19  // JSONIO address the json data from connection
    20  type JSONIO struct {
    21  	Connection conn.Connection
    22  }
    23  
    24  // SetReadDeadline set read operation dead line
    25  func (io *JSONIO) SetReadDeadline(time time.Time) error {
    26  	return io.Connection.SetReadDeadline(time)
    27  }
    28  
    29  // SetWriteDeadline set write operation dead line
    30  func (io *JSONIO) SetWriteDeadline(time time.Time) error {
    31  	return io.Connection.SetWriteDeadline(time)
    32  }
    33  
    34  // ReadData read data from connection
    35  func (io *JSONIO) ReadData(msg *model.Message) (int, error) {
    36  	return 0, io.Connection.ReadMessage(msg)
    37  }
    38  
    39  // WriteData write data to connection
    40  func (io *JSONIO) WriteData(msg *model.Message) error {
    41  	err := io.Connection.WriteMessageAsync(msg)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	return nil
    46  }
    47  
    48  // Close close the IO operation
    49  func (io *JSONIO) Close() error {
    50  	return io.Connection.Close()
    51  }