Commit 6cc8e77b authored by Gleb Pinigin's avatar Gleb Pinigin
Browse files

http uploader: factor code, use en1 as wifi interface for iPhoneSimulator,...

http uploader: factor code, use en1 as wifi interface for iPhoneSimulator, prevent adding multiple loggers
parent e17c415a
......@@ -17,7 +17,8 @@
@property (nonatomic, readonly) HTTPServer *httpServer;
-(BOOL)changeHTTPServerState:(BOOL)state;
- (BOOL)changeHTTPServerState:(BOOL)state;
- (NSString *)currentIPAddress;
@end
......
......@@ -25,6 +25,14 @@
#import "MultipartFormDataParser.h"
#import "MultipartMessageHeaderField.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#if TARGET_IPHONE_SIMULATOR
NSString *const WifiInterfaceName = @"en1";
#else
NSString *const WifiInterfaceName = @"en0";
#endif
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE;
......@@ -38,13 +46,13 @@ static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE
- (id)init
{
if ( self = [super init] ) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationDidBecomeActive:)
// Just log to the Xcode console.
[DDLog addLogger:[DDTTYLogger sharedInstance]];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationDidEnterBackground:)
[center addObserver:self selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
return self;
......@@ -67,12 +75,11 @@ static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE
-(BOOL)changeHTTPServerState:(BOOL)state
{
if(state) {
// Just log to the Xcode console.
[DDLog addLogger:[DDTTYLogger sharedInstance]];
// Initalize our http server
_httpServer = [[HTTPServer alloc] init];
[_httpServer setInterface:WifiInterfaceName];
// Tell the server to broadcast its presence via Bonjour.
// This allows browsers such as Safari to automatically discover our service.
[self.httpServer setType:@"_http._tcp."];
......@@ -108,6 +115,27 @@ static const int httpLogLevel = HTTP_LOG_LEVEL_VERBOSE; // | HTTP_LOG_FLAG_TRACE
}
}
- (NSString *)currentIPAddress
{
NSString *address = @"";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = getifaddrs(&interfaces);
if (success == 0) {
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
if([@(temp_addr->ifa_name) isEqualToString:WifiInterfaceName])
address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
@end
......
......@@ -25,9 +25,6 @@
#import "VLCHTTPDownloadViewController.h"
#import "VLCBugreporter.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
@interface VLCMenuViewController () {
VLCHTTPDownloadViewController *_downloadViewController;
Reachability *_reachability;
......@@ -158,32 +155,13 @@
[self _presentViewController:self.settingsController.viewController];
}
- (NSString *)_currentIPAddress
{
NSString *address = @"";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = getifaddrs(&interfaces);
if (success == 0) {
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
- (void)updateHTTPServerAddress
{
HTTPServer *server = self.uploadController.httpServer;
if (server.isRunning)
self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self.uploadController currentIPAddress], server.listeningPort];
else
self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment