QNInetAddress.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // QNInetAddress.m
  3. // QiniuSDK
  4. //
  5. // Created by 杨森 on 2020/7/27.
  6. // Copyright © 2020 Qiniu. All rights reserved.
  7. //
  8. #import "QNInetAddress.h"
  9. @interface QNInetAddress()
  10. @end
  11. @implementation QNInetAddress
  12. + (instancetype)inetAddress:(id)addressInfo{
  13. NSDictionary *addressDic = nil;
  14. if ([addressInfo isKindOfClass:[NSDictionary class]]) {
  15. addressDic = (NSDictionary *)addressInfo;
  16. } else if ([addressInfo isKindOfClass:[NSString class]]){
  17. NSData *data = [(NSString *)addressInfo dataUsingEncoding:NSUTF8StringEncoding];
  18. addressDic = [NSJSONSerialization JSONObjectWithData:data
  19. options:NSJSONReadingMutableLeaves
  20. error:nil];
  21. } else if ([addressInfo isKindOfClass:[NSData class]]) {
  22. addressDic = [NSJSONSerialization JSONObjectWithData:(NSData *)addressInfo
  23. options:NSJSONReadingMutableLeaves
  24. error:nil];
  25. } else if ([addressInfo conformsToProtocol:@protocol(QNInetAddressDelegate)]){
  26. id <QNInetAddressDelegate> address = (id <QNInetAddressDelegate> )addressInfo;
  27. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  28. if ([address respondsToSelector:@selector(hostValue)] && [address hostValue]) {
  29. dic[@"hostValue"] = [address hostValue];
  30. }
  31. if ([address respondsToSelector:@selector(ipValue)] && [address ipValue]) {
  32. dic[@"ipValue"] = [address ipValue];
  33. }
  34. if ([address respondsToSelector:@selector(ttlValue)] && [address ttlValue]) {
  35. dic[@"ttlValue"] = [address ttlValue];
  36. }
  37. if ([address respondsToSelector:@selector(timestampValue)] && [address timestampValue]) {
  38. dic[@"timestampValue"] = [address timestampValue];
  39. }
  40. addressDic = [dic copy];
  41. }
  42. if (addressDic) {
  43. QNInetAddress *address = [[QNInetAddress alloc] init];
  44. [address setValuesForKeysWithDictionary:addressDic];
  45. return address;
  46. } else {
  47. return nil;
  48. }
  49. }
  50. - (BOOL)isValid{
  51. if (!self.timestampValue || !self.ipValue || self.ipValue.length == 0) {
  52. return NO;
  53. }
  54. NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
  55. if (currentTimestamp > self.timestampValue.doubleValue + self.ttlValue.doubleValue) {
  56. return NO;
  57. } else {
  58. return YES;
  59. }
  60. }
  61. - (NSString *)toJsonInfo{
  62. NSString *defaultString = @"{}";
  63. NSDictionary *infoDic = [self toDictionary];
  64. if (!infoDic) {
  65. return defaultString;
  66. }
  67. NSData *infoData = [NSJSONSerialization dataWithJSONObject:infoDic
  68. options:NSJSONWritingPrettyPrinted
  69. error:nil];
  70. if (!infoData) {
  71. return defaultString;
  72. }
  73. NSString *infoStr = [[NSString alloc] initWithData:infoData encoding:NSUTF8StringEncoding];
  74. if (!infoStr) {
  75. return defaultString;
  76. } else {
  77. return infoStr;
  78. }
  79. }
  80. - (NSDictionary *)toDictionary{
  81. return [self dictionaryWithValuesForKeys:@[@"ipValue", @"hostValue", @"ttlValue", @"timestampValue"]];
  82. }
  83. - (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
  84. @end