@@ -215,20 +215,32 @@ func (self *Pidgey) Run() {
215215 // で、Pubackが帰ってきたらrefcountを下げて0になったらMessageを消す
216216 log .Debug ("TopicName: %s" , m .TopicName )
217217 targets := self .Qlobber .Match (m .TopicName )
218- if m .QosLevel > 0 {
219- // TODO: ClientごとにInflightTableを持つ
220- // engineのOutGoingTableなのはとりあえず、ということ
221- id := self .OutGoingTable .NewId ()
222- m .PacketIdentifier = id
223-
224- if sender , ok := m .Opaque .(Connection ); ok {
225- self .OutGoingTable .Register2 (m .PacketIdentifier , m , len (targets ), sender )
218+ for i := range targets {
219+ cn := targets [i ].(Connection )
220+ x , err := codec .CopyPublishMessage (m )
221+ if err != nil {
222+ continue
226223 }
227- }
228224
229- for i := range targets {
230- log .Debug ("sending publish message to %s [%s %s %d %d]" , targets [i ].(Connection ).GetId (), m .TopicName , m .Payload , m .PacketIdentifier , m .QosLevel )
231- targets [i ].(Connection ).WriteMessageQueue (m )
225+ subscriberQos := cn .GetSubscribedTopicQos (m .TopicName )
226+ // Downgrade QoS
227+ if subscriberQos < x .QosLevel {
228+ log .Debug ("===Downgrade QoS %d > %d" , x .QosLevel , subscriberQos )
229+ x .QosLevel = subscriberQos
230+ // client側はtopic filterで持ってるから単純にはqosわかんねーんだよな
231+ }
232+
233+ if x .QosLevel > 0 {
234+ // TODO: ClientごとにInflightTableを持つ
235+ // engineのOutGoingTableなのはとりあえず、ということ
236+ id := self .OutGoingTable .NewId ()
237+ x .PacketIdentifier = id
238+ if sender , ok := x .Opaque .(Connection ); ok {
239+ self .OutGoingTable .Register2 (x .PacketIdentifier , x , len (targets ), sender )
240+ }
241+ }
242+ log .Debug ("sending publish message to %s [%s %s %d %d]" , targets [i ].(Connection ).GetId (), x .TopicName , x .Payload , x .PacketIdentifier , x .QosLevel )
243+ cn .WriteMessageQueue (x )
232244 }
233245 break
234246 default :
@@ -243,7 +255,7 @@ func (self *Pidgey) Run() {
243255}
244256
245257func (self * Pidgey ) CleanHoge (conn Connection ) {
246- for _ , t := range conn .GetSubscribedTopics () {
258+ for t , _ := range conn .GetSubscribedTopics () {
247259 if conn .ShouldClearSession () {
248260 self .Qlobber .Remove (t , conn )
249261 }
@@ -388,7 +400,7 @@ func (self *Pidgey) handle(conn Connection) error {
388400 self .SubscribeMap [conn .GetId ()] = payload .TopicPath
389401
390402 self .Qlobber .Add (payload .TopicPath , conn )
391- conn .AppendSubscribedTopic (payload .TopicPath )
403+ conn .AppendSubscribedTopic (payload .TopicPath , int ( payload . RequestedQos ) )
392404
393405 // TODO: これはAtomicにさせたいなー、とおもったり。
394406 // というかTopicは実装上もうつかってないので消していいや
0 commit comments