Commits

Pierre Pagnoux committed 619713d

Added Netsoul client : connection.

Comments (0)

Files changed (4)

MySoul.xcodeproj/project.pbxproj

 	objects = {
 
 /* Begin PBXBuildFile section */
+		8F2F042F1826AB7B00BE0D9E /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F2F042E1826AB7B00BE0D9E /* CFNetwork.framework */; };
 		8F4645A2182566CD009682AA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F4645A1182566CD009682AA /* Foundation.framework */; };
 		8F4645A4182566CD009682AA /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F4645A3182566CD009682AA /* CoreGraphics.framework */; };
 		8F4645A6182566CD009682AA /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F4645A5182566CD009682AA /* UIKit.framework */; };
 		8F4645C3182566CE009682AA /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F4645A5182566CD009682AA /* UIKit.framework */; };
 		8F4645CB182566CE009682AA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8F4645C9182566CE009682AA /* InfoPlist.strings */; };
 		8F4645CD182566CE009682AA /* MySoulTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F4645CC182566CE009682AA /* MySoulTests.m */; };
+		8F4645D8182570BF009682AA /* Netsoul.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F4645D7182570BF009682AA /* Netsoul.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		8F2F042E1826AB7B00BE0D9E /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
 		8F46459E182566CD009682AA /* MySoul.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MySoul.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		8F4645A1182566CD009682AA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
 		8F4645A3182566CD009682AA /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
 		8F4645C8182566CE009682AA /* MySoulTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "MySoulTests-Info.plist"; sourceTree = "<group>"; };
 		8F4645CA182566CE009682AA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
 		8F4645CC182566CE009682AA /* MySoulTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MySoulTests.m; sourceTree = "<group>"; };
+		8F4645D6182570BF009682AA /* Netsoul.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Netsoul.h; sourceTree = "<group>"; };
+		8F4645D7182570BF009682AA /* Netsoul.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Netsoul.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				8F2F042F1826AB7B00BE0D9E /* CFNetwork.framework in Frameworks */,
 				8F4645A4182566CD009682AA /* CoreGraphics.framework in Frameworks */,
 				8F4645A6182566CD009682AA /* UIKit.framework in Frameworks */,
 				8F4645A2182566CD009682AA /* Foundation.framework in Frameworks */,
 		8F4645A0182566CD009682AA /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				8F2F042E1826AB7B00BE0D9E /* CFNetwork.framework */,
 				8F4645A1182566CD009682AA /* Foundation.framework */,
 				8F4645A3182566CD009682AA /* CoreGraphics.framework */,
 				8F4645A5182566CD009682AA /* UIKit.framework */,
 		8F4645A7182566CD009682AA /* MySoul */ = {
 			isa = PBXGroup;
 			children = (
+				8F4645D6182570BF009682AA /* Netsoul.h */,
+				8F4645D7182570BF009682AA /* Netsoul.m */,
 				8F4645B0182566CD009682AA /* AppDelegate.h */,
 				8F4645B1182566CD009682AA /* AppDelegate.m */,
 				8F4645B3182566CD009682AA /* Main.storyboard */,
 			files = (
 				8F4645B8182566CD009682AA /* ViewController.m in Sources */,
 				8F4645B2182566CD009682AA /* AppDelegate.m in Sources */,
+				8F4645D8182570BF009682AA /* Netsoul.m in Sources */,
 				8F4645AE182566CD009682AA /* main.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 				8F4645D2182566CE009682AA /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		8F4645D3182566CE009682AA /* Build configuration list for PBXNativeTarget "MySoulTests" */ = {
 			isa = XCConfigurationList;
 				8F4645D5182566CE009682AA /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 /* End XCConfigurationList section */
 	};
+//
+//  Netsoul.h
+//  MySoul
+//
+//  Created by Pierre Pagnoux on 02/11/13.
+//  Copyright (c) 2013 Pierre Pagnoux. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface Netsoul : NSObject
+
+@property NSString *login;
+@property NSString *pass;
+
+- (BOOL)connect;
+- (void)disconnect;
+
+@end
+//
+//  Netsoul.m
+//  MySoul
+//
+//  Created by Pierre Pagnoux on 02/11/13.
+//  Copyright (c) 2013 Pierre Pagnoux. All rights reserved.
+//
+
+#import "Netsoul.h"
+#import <sys/socket.h>
+#import <arpa/inet.h>
+
+#define HOST "163.5.255.5"
+#define PORT 4242
+#define BUFFER_SIZE 500
+
+@interface Netsoul()
+
+@property int socket;
+@property char *buffer;
+
+@property char *hash;
+@property char *ip;
+@property char *port;
+
+@property char *auth;
+
+@end
+
+@implementation Netsoul
+-(id)init {
+    self = [super init];
+
+    if (self) {
+        self.buffer = malloc(BUFFER_SIZE + 1);
+    }
+
+    return self;
+}
+
+-(BOOL)connect {
+    self.socket = socket(PF_INET, SOCK_STREAM, 6);
+    if (self.socket < 0)
+      {
+        NSLog(@"[CONNECT] Failed to get socket!");
+        return FALSE;
+      }
+
+    struct sockaddr_in server;
+
+    memset(&server, 0, sizeof (struct sockaddr_in));
+    server.sin_family = AF_INET;
+    server.sin_addr.s_addr = inet_addr(HOST);
+    server.sin_port = htons(PORT);
+
+    if (connect(self.socket,
+                (struct sockaddr*)&server,
+                sizeof (struct sockaddr)))
+      {
+        NSLog(@"[CONNECT] Error while connecting to the server. %s.",
+              strerror(errno));
+        return FALSE;
+      }
+
+    ssize_t length = 0;
+
+    length = recv(self.socket, self.buffer, BUFFER_SIZE, 0);
+    self.buffer[length] = '\0';
+
+    NSLog(@"Received(%zu bytes) : '%s'", length, self.buffer);
+
+    char *ptr = self.buffer;
+    if (!strcmp(strsep(&ptr, " "), "salut"))
+        NSLog(@"[SERVER] 'salut' matched");
+    else
+      {
+        NSLog(@"[SERVER] 'salut' not matched. Fail.");
+        return FALSE;
+      }
+
+    NSLog(@"[SERVER] Socket: %s", strsep(&ptr, " "));
+    self.hash = strdup(strsep(&ptr, " "));
+    self.ip = strdup(strsep(&ptr, " "));
+    self.port = strdup(strsep(&ptr, " "));
+
+    NSLog(@"[SERVER] Hash: %s", self.hash);
+    NSLog(@"[SERVER] IP: %s", self.ip);
+    NSLog(@"[SERVER] Port: %s", self.port);
+
+    return TRUE;
+}
+
+-(void)signIn: (NSString*)login :(NSString*)pass {
+    NSLog(@"Login in ...");
+}
+
+-(void)dealloc {
+    free(self.buffer);
+    free(self.hash);
+    free(self.ip);
+    free(self.port);
+}
+
+-(void)disconnect {
+    if (shutdown(self.socket, SHUT_RDWR) < 0)
+        NSLog(@"[DISCONNECT] %s", strerror(errno));
+    free(self.hash);
+    free(self.ip);
+    free(self.port);
+}
+@end

MySoul/ViewController.m

 
 @implementation ViewController
 
+-(id)init {
+    self = [super init];
+    self.client = [[Netsoul alloc] init];
+
+    return self;
+}
+
 - (void)viewDidLoad
 {
     [super viewDidLoad];
 }
 
 - (IBAction)activation:(UISwitch *)sender {
-    NSLog(@"Connection status : %@", sender.isOn ? @"YES" : @"NO");
-    [self.client connect];
+    NSLog(@"Connection switch : %@", sender.isOn ? @"YES" : @"NO");
+
+    if (sender.isOn)
+      {
+        if (![self.client connect])
+          {
+            [sender setOn:FALSE animated:YES];
+            [self.client disconnect];
+          }
+      }
+    else
+        [self.client disconnect];
 }
 
 @end