Skip to content

Commit

Permalink
Convert systemInfo to a class and make systemInfoString and `syst…
Browse files Browse the repository at this point in the history
…emVersionString` instance properties
  • Loading branch information
thecatalinstan committed Jun 18, 2021
1 parent 2b0bcd4 commit b156e26
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 144 deletions.
2 changes: 1 addition & 1 deletion CSSystemInfoHelper.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Pod::Spec.new do |s|
s.module_name = "CSSystemInfoHelper"

s.source_files = "CSSystemInfoHelper/Sources/*.{h,m}", "CSSystemInfoHelper/Headers/CSSystemInfoHelper/*.h"
s.public_header_files = "CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSSystemInfoHelper.h", "CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSNetworkInterface.h"
s.public_header_files = "CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSSystemInfoHelper.h", "CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSNetworkInterface.h", "CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSSystemInfo.h"

s.ios.deployment_target = "9.0"
s.ios.frameworks = "Foundation"
Expand Down
12 changes: 12 additions & 0 deletions CSSystemInfoHelper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
AD143BA7267C87450094D246 /* CSSystemInfoProviderMock.m in Sources */ = {isa = PBXBuildFile; fileRef = AD143BA6267C87450094D246 /* CSSystemInfoProviderMock.m */; };
AD352510267CB84700A3A929 /* CSSystemInfoHelperDeprecatedTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AD35250F267CB84700A3A929 /* CSSystemInfoHelperDeprecatedTests.m */; };
AD352512267CCE0900A3A929 /* Errors.m in Sources */ = {isa = PBXBuildFile; fileRef = AD352511267CCE0900A3A929 /* Errors.m */; };
AD35252B267D48FF00A3A929 /* CSSystemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = AD352529267D48FF00A3A929 /* CSSystemInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
AD35252C267D48FF00A3A929 /* CSSystemInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = AD35252A267D48FF00A3A929 /* CSSystemInfo.m */; };
AD35252E267D4B6300A3A929 /* CSSystemInfo+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = AD35252D267D4B6300A3A929 /* CSSystemInfo+Internal.h */; };
AD3BFDF0267AA56600FF8076 /* CSSystemInfoHelper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD3BFDE6267AA56600FF8076 /* CSSystemInfoHelper.framework */; };
AD3BFDF5267AA56600FF8076 /* CSSystemInfoHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AD3BFDF4267AA56600FF8076 /* CSSystemInfoHelperTests.m */; };
AD3BFE07267AA5BD00FF8076 /* CSSystemInfoHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = AD3BFE03267AA5BD00FF8076 /* CSSystemInfoHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -40,6 +43,9 @@
AD143BA6267C87450094D246 /* CSSystemInfoProviderMock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSSystemInfoProviderMock.m; sourceTree = "<group>"; };
AD35250F267CB84700A3A929 /* CSSystemInfoHelperDeprecatedTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CSSystemInfoHelperDeprecatedTests.m; sourceTree = "<group>"; };
AD352511267CCE0900A3A929 /* Errors.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Errors.m; sourceTree = "<group>"; };
AD352529267D48FF00A3A929 /* CSSystemInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CSSystemInfo.h; sourceTree = "<group>"; };
AD35252A267D48FF00A3A929 /* CSSystemInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CSSystemInfo.m; sourceTree = "<group>"; };
AD35252D267D4B6300A3A929 /* CSSystemInfo+Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CSSystemInfo+Internal.h"; sourceTree = "<group>"; };
AD3BFDE6267AA56600FF8076 /* CSSystemInfoHelper.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CSSystemInfoHelper.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AD3BFDEF267AA56600FF8076 /* CSSystemInfoHelperTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CSSystemInfoHelperTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
AD3BFDF4267AA56600FF8076 /* CSSystemInfoHelperTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CSSystemInfoHelperTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -133,6 +139,7 @@
children = (
AD3BFE03267AA5BD00FF8076 /* CSSystemInfoHelper.h */,
ADB06B22267B5A2400D4F0E3 /* CSNetworkInterface.h */,
AD352529267D48FF00A3A929 /* CSSystemInfo.h */,
);
path = CSSystemInfoHelper;
sourceTree = "<group>";
Expand All @@ -144,6 +151,8 @@
AD3BFE05267AA5BD00FF8076 /* CSSystemInfoHelper.m */,
ADB06B26267B5B6E00D4F0E3 /* CSNetworkInterface+Internal.h */,
ADB06B23267B5A2400D4F0E3 /* CSNetworkInterface.m */,
AD35252D267D4B6300A3A929 /* CSSystemInfo+Internal.h */,
AD35252A267D48FF00A3A929 /* CSSystemInfo.m */,
AD143B9F267C7D350094D246 /* CSSystemInfoProvider.h */,
AD143BA0267C7D350094D246 /* CSSystemInfoProvider.m */,
ADB06B28267B608800D4F0E3 /* Errors.h */,
Expand All @@ -164,6 +173,8 @@
AD3BFE07267AA5BD00FF8076 /* CSSystemInfoHelper.h in Headers */,
AD143BA1267C7D350094D246 /* CSSystemInfoProvider.h in Headers */,
ADB06B24267B5A2400D4F0E3 /* CSNetworkInterface.h in Headers */,
AD35252B267D48FF00A3A929 /* CSSystemInfo.h in Headers */,
AD35252E267D4B6300A3A929 /* CSSystemInfo+Internal.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -266,6 +277,7 @@
files = (
AD352512267CCE0900A3A929 /* Errors.m in Sources */,
ADB06B25267B5A2400D4F0E3 /* CSNetworkInterface.m in Sources */,
AD35252C267D48FF00A3A929 /* CSSystemInfo.m in Sources */,
AD143BA2267C7D350094D246 /* CSSystemInfoProvider.m in Sources */,
AD3BFE08267AA5BD00FF8076 /* CSSystemInfoHelper.m in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@

NS_ASSUME_NONNULL_BEGIN

/// A container class for the attributes of a network interface as understood by
/// @c getaddrinfo(3) and related APIs
@interface CSNetworkInterface : NSObject

/// The name of the interface
@property (nonatomic, readonly, strong) NSString *name;

/// A string representation of the address (IPv4 or IPv6) of the interface
@property (nonatomic, readonly, strong) NSString *address;

/// The address family of the address, either @c AF_INET or @c AF_INET6, for
/// IPv4 or IPv6 respectively
@property (nonatomic, readonly) sa_family_t family;

/// A string representation of the address family
@property (nonatomic, readonly, strong) NSString *familyName;

+ (instancetype)new NS_UNAVAILABLE;
Expand Down
44 changes: 44 additions & 0 deletions CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSSystemInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// CSSystemInfo.h
// CSSystemInfoHelper
//
// Created by Cătălin Stan on 18/06/2021.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/// A container class for the @c utsname struct.
/// See the @c uname(3) manual page for more info.
@interface CSSystemInfo : NSObject

/// Name of the operating system implementation.
@property (nonatomic, readonly) NSString *sysName;

/// Network name of this machine.
@property (nonatomic, readonly) NSString *nodeName;

/// Release level of the operating system.
@property (nonatomic, readonly) NSString *releaseLevel;

/// Version level of the operating system.
@property (nonatomic, readonly) NSString *versionLevel;

/// Machine hardware platform.
@property (nonatomic, readonly) NSString *machine;

/// A concatenated string representation of all the properties in the order
/// listed in the @c uname(1) manual page.
@property (nonatomic, readonly, strong) NSString *string;

/// A concatenated string representation of the @c sysName, @c releaseLevel,
/// properties @c machine, separated by space.
@property (nonatomic, readonly, strong) NSString *versionString;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
33 changes: 6 additions & 27 deletions CSSystemInfoHelper/Headers/CSSystemInfoHelper/CSSystemInfoHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,10 @@ FOUNDATION_EXPORT double CSSystemInfoHelperVersionNumber;
FOUNDATION_EXPORT const unsigned char CSSystemInfoHelperVersionString[];

#import <CSSystemInfoHelper/CSNetworkInterface.h>
#import <CSSystemInfoHelper/CSSystemInfo.h>

NS_ASSUME_NONNULL_BEGIN

typedef NSString * CSSystemInfoKey NS_TYPED_EXTENSIBLE_ENUM;

/// Name of the operating system implementation.
FOUNDATION_EXPORT CSSystemInfoKey const CSSystemInfoKeySysname;
/// Network name of this machine.
FOUNDATION_EXPORT CSSystemInfoKey const CSSystemInfoKeyNodename;
/// Release level of the operating system.
FOUNDATION_EXPORT CSSystemInfoKey const CSSystemInfoKeyRelease;
/// Version level of the operating system.
FOUNDATION_EXPORT CSSystemInfoKey const CSSystemInfoKeyVersion;
/// Machine hardware platform.
FOUNDATION_EXPORT CSSystemInfoKey const CSSystemInfoKeyMachine;

/// Constant returned when no IP address could be deternimed
FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_ATTRIBUTE;

/// The CSSystemInfoHelper class provides easy-access to some useful system
/// information that would otherwise require some more elaborate code.
@interface CSSystemInfoHelper : NSObject
Expand All @@ -49,17 +34,8 @@ FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_AT

/// @name Getting @c uname System Information

/// A dictionary containing the results of the @c uname(3) call.
@property (nonatomic, readonly, strong) NSDictionary<CSSystemInfoKey, NSString *> *systemInfo;

/// A concatenated string representation of all the values in `systemInfo`, in
/// the order listed in the @c uname(3) manual page.
@property (nonatomic, readonly, strong) NSString *systemInfoString;

/// A concatenated string representation of the OS version keys from
/// @c -systemInfo, in the following order: @c CSSystemInfoKeySysname,
/// @c CSSystemInfoKeyRelease, @c CSSystemInfoKeyMachine, separated by space.
@property (nonatomic, readonly, strong) NSString *systemVersionString;
/// A @c CSSystemInfo object populated with the results of the @c uname(3) call.
@property (nonatomic, readonly, strong, nullable) CSSystemInfo *systemInfo;

/// @name Getting Memory Usage

Expand Down Expand Up @@ -104,4 +80,7 @@ FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_AT

@end

/// Constant returned when no IP address could be deternimed
FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_ATTRIBUTE;

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion CSSystemInfoHelper/Sources/CSNetworkInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import "CSNetworkInterface+Internal.h"

#include <sys/socket.h>
#import <sys/socket.h>

#import "Errors.h"

Expand Down
27 changes: 27 additions & 0 deletions CSSystemInfoHelper/Sources/CSSystemInfo+Internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// CSSystemInfo+Internal.h
// CSSystemInfoHelper
//
// Created by Cătălin Stan on 18/06/2021.
//

#import <CSSystemInfoHelper/CSSystemInfo.h>

#import <sys/utsname.h>

NS_ASSUME_NONNULL_BEGIN

@interface CSSystemInfo ()

- (instancetype)initWithSysName:(NSString *)sysName
nodeName:(NSString *)nodeName
releaseLevel:(NSString *)releaseLevel
versionLevel:(NSString *)versionLevel
machine:(NSString *)machine
NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithUtsnameStruct:(struct utsname *)utsnameStruct;

@end

NS_ASSUME_NONNULL_END
40 changes: 40 additions & 0 deletions CSSystemInfoHelper/Sources/CSSystemInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CSSystemInfo.m
// CSSystemInfoHelper
//
// Created by Cătălin Stan on 18/06/2021.
//

#import <CSSystemInfoHelper/CSSystemInfo.h>
#import "CSSystemInfo+Internal.h"

@implementation CSSystemInfo

- (instancetype)initWithUtsnameStruct:(struct utsname *)utsnameStruct {
return [self initWithSysName:@(utsnameStruct->sysname)
nodeName:@(utsnameStruct->nodename)
releaseLevel:@(utsnameStruct->release)
versionLevel:@(utsnameStruct->version)
machine:@(utsnameStruct->machine)];
}

- (instancetype)initWithSysName:(NSString *)sysName
nodeName:(NSString *)nodeName
releaseLevel:(NSString *)releaseLevel
versionLevel:(NSString *)versionLevel
machine:(NSString *)machine {
self = [super init];
if (self) {
_sysName = sysName;
_nodeName = nodeName;
_releaseLevel = releaseLevel;
_versionLevel = versionLevel;
_machine = machine;

_string = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", _sysName, _nodeName, _releaseLevel, _versionLevel, _machine];
_versionString = [NSString stringWithFormat:@"%@ %@ %@", _sysName, _releaseLevel, _machine];
}
return self;
}

@end
73 changes: 15 additions & 58 deletions CSSystemInfoHelper/Sources/CSSystemInfoHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,9 @@
#import <CSSystemInfoHelper/CSSystemInfoHelper.h>
#import "CSSystemInfoHelper+Internal.h"

#import <sys/utsname.h>

#import "CSNetworkInterface+Internal.h"
#import "CSSystemInfoProvider.h"
#import "Errors.h"

CSSystemInfoKey const CSSystemInfoKeySysname = @"CSSystemInfoSysname";
CSSystemInfoKey const CSSystemInfoKeyNodename = @"CSSystemInfoNodename";
CSSystemInfoKey const CSSystemInfoKeyRelease = @"CSSystemInfoRelease";
CSSystemInfoKey const CSSystemInfoKeyVersion = @"CSSystemInfoVersion";
CSSystemInfoKey const CSSystemInfoKeyMachine = @"CSSystemInfoMachine";

NSString * const CSSystemInfoHelperIPAddressNone = @"(none)";
NSString * const CSSystemInfoHelperDefaultInterface = @"en0";


__attribute__((objc_direct_members))
@interface CSSystemInfoHelper ()

Expand Down Expand Up @@ -61,49 +48,16 @@ - (instancetype)initWithSystemInfoProvider:(id<CSSystemInfoProviderProtocol>)sys
return networkInterfaces;
}

- (NSDictionary<CSSystemInfoKey, NSString *> *)systemInfo {
static NSDictionary<CSSystemInfoKey, NSString *> * systemInfo;
if (!systemInfo) {
struct utsname unameStruct;
if (uname(&unameStruct) != 0) {
@throw [NSException exceptionWithName:NSGenericException reason:[NSString stringWithUTF8String:strerror(errno)] userInfo:nil];
}

systemInfo = @{
CSSystemInfoKeySysname: @(unameStruct.sysname),
CSSystemInfoKeyNodename: @(unameStruct.nodename),
CSSystemInfoKeyRelease: @(unameStruct.release),
CSSystemInfoKeyVersion: @(unameStruct.version),
CSSystemInfoKeyMachine: @(unameStruct.machine)
};
- (CSSystemInfo *)systemInfo {
NSError *error;
CSSystemInfo *systemInfo;
if (!(systemInfo = [self.systemInfoProvider quertSystemInfo:&error])) {
NSLog(@"Error loading system info: %@. %@.", error.localizedDescription, error.localizedFailureReason);
return nil;
}
return systemInfo;
}

- (NSString *)systemInfoString {
static NSString *systemInfoString;
if (!systemInfoString) {
systemInfoString = [NSString stringWithFormat:@"%@ %@ %@ %@ %@",
self.systemInfo[CSSystemInfoKeySysname],
self.systemInfo[CSSystemInfoKeyNodename],
self.systemInfo[CSSystemInfoKeyRelease],
self.systemInfo[CSSystemInfoKeyVersion],
self.systemInfo[CSSystemInfoKeyMachine]];
}
return systemInfoString;
}

- (NSString *)systemVersionString {
static NSString* systemVersionString;
if (!systemVersionString) {
systemVersionString = [NSString stringWithFormat:@"%@ %@ %@",
self.systemInfo[CSSystemInfoKeySysname],
self.systemInfo[CSSystemInfoKeyRelease],
self.systemInfo[CSSystemInfoKeyMachine]];
}
return systemVersionString;
}

- (vm_size_t)memoryUsage {
NSError *error;
vm_size_t memoryUsage = 0;
Expand Down Expand Up @@ -145,6 +99,12 @@ - (NSString *)platformUUID {
}
#endif

#pragma mark - Private Helpers

- (NSString *)formatByteCount:(long long)byteCount {
return [NSByteCountFormatter stringFromByteCount:byteCount countStyle:NSByteCountFormatterCountStyleMemory];
}

#pragma mark - Deprecated

#pragma clang diagnostic push
Expand Down Expand Up @@ -187,11 +147,8 @@ - (NSString *)IPAddress {

#pragma clang diagnostic pop

#pragma mark - Private Helpers

- (NSString *)formatByteCount:(long long)byteCount {
return [NSByteCountFormatter stringFromByteCount:byteCount countStyle:NSByteCountFormatterCountStyleMemory];
}
@end

NSString * const CSSystemInfoHelperIPAddressNone = @"(none)";
NSString * const CSSystemInfoHelperDefaultInterface = @"en0";

@end
4 changes: 3 additions & 1 deletion CSSystemInfoHelper/Sources/CSSystemInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
#import <Foundation/Foundation.h>
#import <ifaddrs.h>

@class CSNetworkInterface;
@class CSNetworkInterface, CSSystemInfo;

NS_ASSUME_NONNULL_BEGIN

@protocol CSSystemInfoProviderProtocol

- (nullable NSArray<CSNetworkInterface *> *)queryNetworkInterfaces:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;

- (nullable CSSystemInfo *)quertSystemInfo:(NSError *__autoreleasing *)error;

- (BOOL)getResidentSize:(vm_size_t *)residentSize error:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;
- (BOOL)getPhysFootprint:(vm_size_t *)physFootprint error:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;

Expand Down
Loading

0 comments on commit b156e26

Please sign in to comment.