// create file called "X.[messageid].msg" located in the store // the contents of the file is the bytes of the message // if a message with m's message id already exists, it will // be overwritten // X will be 'i' for inbound messages, and O for outbound messages func write(store, key string, m packets.ControlPacket) { filepath := fullpath(store, key) f, err := os.Create(filepath) chkerr(err) werr := m.Write(f) chkerr(werr) cerr := f.Close() chkerr(cerr) }
// govern which outgoing messages are persisted func persistOutbound(s Store, m packets.ControlPacket) { switch m.Details().Qos { case 0: switch m.(type) { case *packets.PubackPacket, *packets.PubcompPacket: // Sending puback. delete matching publish // from ibound s.Del(inboundKeyFromMID(m.Details().MessageID)) } case 1: switch m.(type) { case *packets.PublishPacket, *packets.PubrelPacket, *packets.SubscribePacket, *packets.UnsubscribePacket: // Sending publish. store in obound // until puback received s.Put(outboundKeyFromMID(m.Details().MessageID), m) default: chkcond(false) } case 2: switch m.(type) { case *packets.PublishPacket: // Sending publish. store in obound // until pubrel received s.Put(outboundKeyFromMID(m.Details().MessageID), m) default: chkcond(false) } } }
// govern which incoming messages are persisted func persistInbound(s Store, m packets.ControlPacket) { switch m.Details().Qos { case 0: switch m.(type) { case *packets.PubackPacket, *packets.SubackPacket, *packets.UnsubackPacket, *packets.PubcompPacket: // Received a puback. delete matching publish // from obound s.Del(outboundKeyFromMID(m.Details().MessageID)) case *packets.PublishPacket, *packets.PubrecPacket, *packets.PingrespPacket, *packets.ConnackPacket: default: chkcond(false) } case 1: switch m.(type) { case *packets.PublishPacket, *packets.PubrelPacket: // Received a publish. store it in ibound // until puback sent s.Put(inboundKeyFromMID(m.Details().MessageID), m) default: chkcond(false) } case 2: switch m.(type) { case *packets.PublishPacket: // Received a publish. store it in ibound // until pubrel received s.Put(inboundKeyFromMID(m.Details().MessageID), m) default: chkcond(false) } } }
func (ts *TestStore) Put(key string, m packets.ControlPacket) { ts.mput = append(ts.mput, m.Details().MessageID) }