FIRMessagingPacketQueue.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/FIRMessagingPacketQueue.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  18. @interface FIRMessagingPacket ()
  19. @property(nonatomic, readwrite, strong) NSData *data;
  20. @property(nonatomic, readwrite, assign) int8_t tag;
  21. @property(nonatomic, readwrite, assign) NSString *rmqId;
  22. @end
  23. @implementation FIRMessagingPacket
  24. + (FIRMessagingPacket *)packetWithTag:(int8_t)tag rmqId:(NSString *)rmqId data:(NSData *)data {
  25. return [[self alloc] initWithTag:tag rmqId:rmqId data:data];
  26. }
  27. - (instancetype)init {
  28. FIRMessagingInvalidateInitializer();
  29. }
  30. - (instancetype)initWithTag:(int8_t)tag rmqId:(NSString *)rmqId data:(NSData *)data {
  31. self = [super init];
  32. if (self != nil) {
  33. _data = data;
  34. _tag = tag;
  35. _rmqId = rmqId;
  36. }
  37. return self;
  38. }
  39. - (NSString *)description {
  40. if ([self.rmqId length]) {
  41. return [NSString stringWithFormat:@"<Packet: Tag - %d, Length - %lu>, RmqId - %@", self.tag,
  42. _FIRMessaging_UL(self.data.length), self.rmqId];
  43. } else {
  44. return [NSString stringWithFormat:@"<Packet: Tag - %d, Length - %lu>", self.tag,
  45. _FIRMessaging_UL(self.data.length)];
  46. }
  47. }
  48. @end
  49. @interface FIRMessagingPacketQueue ()
  50. @property(nonatomic, readwrite, strong) NSMutableArray *packetsContainer;
  51. @end
  52. @implementation FIRMessagingPacketQueue
  53. ;
  54. - (id)init {
  55. self = [super init];
  56. if (self) {
  57. _packetsContainer = [[NSMutableArray alloc] init];
  58. }
  59. return self;
  60. }
  61. - (BOOL)isEmpty {
  62. return self.packetsContainer.count == 0;
  63. }
  64. - (NSUInteger)count {
  65. return self.packetsContainer.count;
  66. }
  67. - (void)push:(FIRMessagingPacket *)packet {
  68. [self.packetsContainer addObject:packet];
  69. }
  70. - (void)pushHead:(FIRMessagingPacket *)packet {
  71. [self.packetsContainer insertObject:packet atIndex:0];
  72. }
  73. - (FIRMessagingPacket *)pop {
  74. if (!self.isEmpty) {
  75. FIRMessagingPacket *packet = self.packetsContainer[0];
  76. [self.packetsContainer removeObjectAtIndex:0];
  77. return packet;
  78. }
  79. return nil;
  80. }
  81. @end