github.com/lei006/gmqtt-broker@v0.0.1/broker/lib/sessions/session.go (about)

     1  package sessions
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/eclipse/paho.mqtt.golang/packets"
     8  )
     9  
    10  const (
    11  	// Queue size for the ack queue
    12  	defaultQueueSize = 16
    13  )
    14  
    15  type Session struct {
    16  
    17  	// cmsg is the CONNECT message
    18  	cmsg *packets.ConnectPacket
    19  
    20  	// Will message to publish if connect is closed unexpectedly
    21  	Will *packets.PublishPacket
    22  
    23  	// Retained publish message
    24  	Retained *packets.PublishPacket
    25  
    26  	// topics stores all the topis for this session/client
    27  	topics map[string]byte
    28  
    29  	// Initialized?
    30  	initted bool
    31  
    32  	// Serialize access to this session
    33  	mu sync.Mutex
    34  
    35  	id string
    36  }
    37  
    38  func (this *Session) Init(msg *packets.ConnectPacket) error {
    39  	this.mu.Lock()
    40  	defer this.mu.Unlock()
    41  
    42  	if this.initted {
    43  		return fmt.Errorf("Session already initialized")
    44  	}
    45  
    46  	this.cmsg = msg
    47  
    48  	if this.cmsg.WillFlag {
    49  		this.Will = packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
    50  		this.Will.Qos = this.cmsg.Qos
    51  		this.Will.TopicName = this.cmsg.WillTopic
    52  		this.Will.Payload = this.cmsg.WillMessage
    53  		this.Will.Retain = this.cmsg.WillRetain
    54  	}
    55  
    56  	this.topics = make(map[string]byte, 1)
    57  
    58  	this.id = msg.ClientIdentifier
    59  
    60  	this.initted = true
    61  
    62  	return nil
    63  }
    64  
    65  func (this *Session) Update(msg *packets.ConnectPacket) error {
    66  	this.mu.Lock()
    67  	defer this.mu.Unlock()
    68  
    69  	this.cmsg = msg
    70  	return nil
    71  }
    72  
    73  func (this *Session) RetainMessage(msg *packets.PublishPacket) error {
    74  	this.mu.Lock()
    75  	defer this.mu.Unlock()
    76  
    77  	this.Retained = msg
    78  
    79  	return nil
    80  }
    81  
    82  func (this *Session) AddTopic(topic string, qos byte) error {
    83  	this.mu.Lock()
    84  	defer this.mu.Unlock()
    85  
    86  	if !this.initted {
    87  		return fmt.Errorf("Session not yet initialized")
    88  	}
    89  
    90  	this.topics[topic] = qos
    91  
    92  	return nil
    93  }
    94  
    95  func (this *Session) RemoveTopic(topic string) error {
    96  	this.mu.Lock()
    97  	defer this.mu.Unlock()
    98  
    99  	if !this.initted {
   100  		return fmt.Errorf("Session not yet initialized")
   101  	}
   102  
   103  	delete(this.topics, topic)
   104  
   105  	return nil
   106  }
   107  
   108  func (this *Session) Topics() ([]string, []byte, error) {
   109  	this.mu.Lock()
   110  	defer this.mu.Unlock()
   111  
   112  	if !this.initted {
   113  		return nil, nil, fmt.Errorf("Session not yet initialized")
   114  	}
   115  
   116  	var (
   117  		topics []string
   118  		qoss   []byte
   119  	)
   120  
   121  	for k, v := range this.topics {
   122  		topics = append(topics, k)
   123  		qoss = append(qoss, v)
   124  	}
   125  
   126  	return topics, qoss, nil
   127  }
   128  
   129  func (this *Session) ID() string {
   130  	return this.cmsg.ClientIdentifier
   131  }
   132  
   133  func (this *Session) WillFlag() bool {
   134  	this.mu.Lock()
   135  	defer this.mu.Unlock()
   136  	return this.cmsg.WillFlag
   137  }
   138  
   139  func (this *Session) SetWillFlag(v bool) {
   140  	this.mu.Lock()
   141  	defer this.mu.Unlock()
   142  	this.cmsg.WillFlag = v
   143  }
   144  
   145  func (this *Session) CleanSession() bool {
   146  	this.mu.Lock()
   147  	defer this.mu.Unlock()
   148  	return this.cmsg.CleanSession
   149  }