Permalink
Browse files

init commit

  • Loading branch information...
0 parents commit f9d1bc6e7369c8625155d8242b6e4efd7ea81366 @robin robin committed Dec 1, 2008
@@ -0,0 +1,3 @@
+build/
+CocoaWebResource.xcodeproj/splyb.*
+Frameworks/
@@ -0,0 +1,22 @@
+//
+// CocoaWebResourceAppDelegate.h
+// CocoaWebResource
+//
+// Created by Robin Lu on 12/1/08.
+// Copyright __MyCompanyName__ 2008. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class CocoaWebResourceViewController;
+
+@interface CocoaWebResourceAppDelegate : NSObject <UIApplicationDelegate> {
+ UIWindow *window;
+ CocoaWebResourceViewController *viewController;
+}
+
+@property (nonatomic, retain) IBOutlet UIWindow *window;
+@property (nonatomic, retain) IBOutlet CocoaWebResourceViewController *viewController;
+
+@end
+
@@ -0,0 +1,33 @@
+//
+// CocoaWebResourceAppDelegate.m
+// CocoaWebResource
+//
+// Created by Robin Lu on 12/1/08.
+// Copyright __MyCompanyName__ 2008. All rights reserved.
+//
+
+#import "CocoaWebResourceAppDelegate.h"
+#import "CocoaWebResourceViewController.h"
+
+@implementation CocoaWebResourceAppDelegate
+
+@synthesize window;
+@synthesize viewController;
+
+
+- (void)applicationDidFinishLaunching:(UIApplication *)application {
+
+ // Override point for customization after app launch
+ [window addSubview:viewController.view];
+ [window makeKeyAndVisible];
+}
+
+
+- (void)dealloc {
+ [viewController release];
+ [window release];
+ [super dealloc];
+}
+
+
+@end
@@ -0,0 +1,19 @@
+//
+// CocoaWebResourceViewController.h
+// CocoaWebResource
+//
+// Created by Robin Lu on 12/1/08.
+// Copyright __MyCompanyName__ 2008. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "HTTPServer.h"
+
+@interface CocoaWebResourceViewController : UIViewController <WebFileResourceDelegate> {
+ IBOutlet UILabel *urlLabel;
+ HTTPServer *httpServer;
+ NSMutableArray *fileList;
+}
+
+- (IBAction)toggleService:(id)sender;
+@end
@@ -0,0 +1,149 @@
+//
+// CocoaWebResourceViewController.m
+// CocoaWebResource
+//
+// Created by Robin Lu on 12/1/08.
+// Copyright __MyCompanyName__ 2008. All rights reserved.
+//
+
+#import "CocoaWebResourceViewController.h"
+
+@implementation CocoaWebResourceViewController
+
+// setup the docroot for the http server
+- (NSString*)setupDocroot
+{
+ NSString* docroot =[NSString stringWithFormat:@"%@/tmp/docroot", NSHomeDirectory()];
+ NSLog(docroot);
+ NSFileManager *manager = [NSFileManager defaultManager];
+ NSError *error;
+ if(![manager removeItemAtPath:docroot error:&error])
+ {
+ NSLog(@"Can not remove old docroot: %@", error);
+ }
+ NSString *path = [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], @"docroot"];
+ [manager createSymbolicLinkAtPath:docroot withDestinationPath:path error:&error];
+ return docroot;
+}
+
+// load file list
+- (void)loadFileList
+{
+ [fileList removeAllObjects];
+ NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
+ NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
+ enumeratorAtPath:docDir];
+ NSString *pname;
+ while (pname = [direnum nextObject])
+ {
+ [fileList addObject:pname];
+ }
+}
+
+// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
+- (void)viewDidLoad {
+ fileList = [[NSMutableArray alloc] init];
+ [self loadFileList];
+
+ // setup the docroot for the http server
+ NSString * docroot = [self setupDocroot];
+
+ httpServer = [[HTTPServer alloc] init];
+ [httpServer setType:@"_http._tcp."];
+
+ [httpServer setPort:8080];
+ [httpServer setName:@"CocoaWebResource"];
+ [httpServer setDocumentRoot:docroot];
+ httpServer.fileResourceDelegate = self;
+
+ [super viewDidLoad];
+}
+
+
+- (void)didReceiveMemoryWarning {
+ [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
+ // Release anything that's not essential, such as cached data
+}
+
+
+- (void)dealloc {
+ httpServer.fileResourceDelegate = nil;
+ [httpServer release];
+ [fileList release];
+ [super dealloc];
+}
+
+#pragma mark actions
+- (IBAction)toggleService:(id)sender
+{
+ NSError *error;
+ if ([(UISwitch*)sender isOn])
+ {
+ BOOL serverIsRunning = [httpServer start:&error];
+ if(!serverIsRunning)
+ {
+ NSLog(@"Error starting HTTP Server: %@", error);
+ }
+ [urlLabel setText:[NSString stringWithFormat:@"http://%@:%d", [httpServer hostName], [httpServer port]]];
+ }
+ else
+ {
+ [httpServer stop];
+ [urlLabel setText:@""];
+ }
+}
+
+#pragma mark WebFileResourceDelegate
+// number of the files
+- (NSInteger)numberOfFiles
+{
+ return [fileList count];
+}
+
+// the file name by the index
+- (NSString*)fileNameAtIndex:(NSInteger)index
+{
+ return [fileList objectAtIndex:index];
+}
+
+// provide full file path by given file name
+- (NSString*)filePathForFileName:(NSString*)filename
+{
+ NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
+ return [NSString stringWithFormat:@"%@/%@", docDir, filename];
+}
+
+// handle newly uploaded file. After uploading, the file is stored in
+// the temparory directory, you need to implement this method to move
+// it to proper location and update the file list.
+- (void)newFileDidUpload:(NSString*)name inTempPath:(NSString*)tmpPath
+{
+ if (name == nil || tmpPath == nil)
+ return;
+ NSString* docDir = [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
+ NSString *path = [NSString stringWithFormat:@"%@/%@", docDir, name];
+ NSFileManager *fm = [NSFileManager defaultManager];
+ NSError *error;
+ if (![fm moveItemAtPath:tmpPath toPath:path error:&error])
+ {
+ NSLog(@"can not move %@ to %@ because: %@", tmpPath, path, error );
+ }
+
+ [self loadFileList];
+
+}
+
+// implement this method to delete requested file and update the file list
+- (void)fileShouldDelete:(NSString*)fileName
+{
+ NSString *path = [self filePathForFileName:fileName];
+ NSFileManager *fm = [NSFileManager defaultManager];
+ NSError *error;
+ if(![fm removeItemAtPath:path error:&error])
+ {
+ NSLog(@"%@ can not be removed because:%@", path, error);
+ }
+ [self loadFileList];
+}
+
+@end
Oops, something went wrong.

0 comments on commit f9d1bc6

Please sign in to comment.