You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.5 KiB

6 years ago
  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 "FIRMessagingPacketQueue.h"
  17. #import "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 - %@",
  42. self.tag, _FIRMessaging_UL(self.data.length), self.rmqId];
  43. } else {
  44. return [NSString stringWithFormat:@"<Packet: Tag - %d, Length - %lu>",
  45. self.tag, _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. - (id)init {
  54. self = [super init];
  55. if (self) {
  56. _packetsContainer = [[NSMutableArray alloc] init];
  57. }
  58. return self;
  59. }
  60. - (BOOL)isEmpty {
  61. return self.packetsContainer.count == 0;
  62. }
  63. - (NSUInteger)count {
  64. return self.packetsContainer.count;
  65. }
  66. - (void)push:(FIRMessagingPacket *)packet {
  67. [self.packetsContainer addObject:packet];
  68. }
  69. - (void)pushHead:(FIRMessagingPacket *)packet {
  70. [self.packetsContainer insertObject:packet atIndex:0];
  71. }
  72. - (FIRMessagingPacket *)pop {
  73. if (!self.isEmpty) {
  74. FIRMessagingPacket *packet = self.packetsContainer[0];
  75. [self.packetsContainer removeObjectAtIndex:0];
  76. return packet;
  77. }
  78. return nil;
  79. }
  80. @end