This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ValveCantCount's profile picture

Concept: OS using Objective-C

Started by ValveCantCount, 09 May 2015 - 02:32 AM
ValveCantCount #1
Posted 09 May 2015 - 04:32 AM
I had an idea: to use HTTP api, PHP, and Objective-C to make an OS. I have the messages working - the computer detecting input, sending it to a web server, and my Objective-C program picking it up.

Here's the output(on the Xcode debugger) I get from doing the following
leftclick
rightclick
"a" key
"b" key
"c" key
scroll up
scroll up
scroll down
scroll down
scroll down
drag(leftclick)
drag(leftclick)
drag(rightclick)
drag(rightclick)
drag(rightclick)
rightclick
rightclick
insert floppy disk on left side
remove floppy disk on left side:


ComputerCraft[16374:8296782] Click With Button: 1 At x: 18 y: 5
ComputerCraft[16374:8296782] label=cs&data=Click With Button: 1 At x: 18 y: 5
ComputerCraft[16374:8296782] Click With Button: 2 At x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Click With Button: 2 At x: 14 y: 10
ComputerCraft[16374:8296782] key pressed with keycode: 30
ComputerCraft[16374:8296782] label=cs&data=key pressed with keycode: 30
ComputerCraft[16374:8296782] key pressed with keycode: 48
ComputerCraft[16374:8296782] label=cs&data=key pressed with keycode: 48
ComputerCraft[16374:8296782] key pressed with keycode: 46
ComputerCraft[16374:8296782] label=cs&data=key pressed with keycode: 46
ComputerCraft[16374:8296782] Scroll in direction: 1 x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Scroll in direction: 1 x: 14 y: 10
ComputerCraft[16374:8296782] Scroll in direction: 1 x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Scroll in direction: 1 x: 14 y: 10
ComputerCraft[16374:8296782] Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] label=cs&data=Scroll in direction: -1 x: 14 y: 10
ComputerCraft[16374:8296782] Drag With Button: 1 At x: 22 y: 12
ComputerCraft[16374:8296782] label=cs&data=Drag With Button: 1 At x: 22 y: 12
ComputerCraft[16374:8296782] Drag With Button: 1 At x: 23 y: 12
ComputerCraft[16374:8296782] label=cs&data=Drag With Button: 1 At x: 23 y: 12
ComputerCraft[16374:8296782] Drag With Button: 2 At x: 23 y: 12
ComputerCraft[16374:8296782] label=cs&data=Drag With Button: 2 At x: 23 y: 12
ComputerCraft[16374:8296782] Click With Button: 2 At x: 17 y: 12
ComputerCraft[16374:8296782] label=cs&data=Click With Button: 2 At x: 17 y: 12
ComputerCraft[16374:8296782] Drag With Button: 2 At x: 20 y: 12
ComputerCraft[16374:8296782] label=cs&data=Drag With Button: 2 At x: 20 y: 12
ComputerCraft[16374:8296782] Disk Inserted On Disk Inserted On left Side Side
ComputerCraft[16374:8296782] label=cs&data=Disk Inserted On left Side
ComputerCraft[16374:8296782] Disk Ejected On Disk Ejected On left Side Side
ComputerCraft[16374:8296782] label=cs&data=Disk Ejected On left Side

I'm still working on the actual handling and returns, but what do you think of the concept? I'm also looking for someone else to work with on the project, so if you're interested then please PM me.

Here's the implementation code, if you want it:
SpoilerThere are a couple methods that are there but are either unimplemented or unused, for example
-(NSArray *)coordinatesFromString:(NSString *)coordString;

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate
@synthesize parse;
@synthesize parsed;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
	// Insert code here to tear down your application
}


/* Start of HTTP Code */

-(void)sendRequestWithLabel:(NSString *)label data:(NSString *)data{
	NSString *postString = [NSString stringWithFormat:@"label=%@&data=%@", label, data];
	NSLog(postString);
	NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

	NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
	NSMutableURLRequest *theRequest = [NSMutableURLRequest new];

	[theRequest setURL:[NSURL URLWithString:@"http://www.softberrykai.com/cc/objc.php"]];
	[theRequest setHTTPMethod:@"POST"];
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
	[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	[theRequest setHTTPBody:postData];

	NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

	if (theConnection) {

		recievedData = [NSMutableData data];

	}

	else {
		// Inform the user that the connection failed.
	}

}


-(void)sendRequestWithTimer:(NSTimer *)theTimer{
	NSString *postString = [NSString stringWithFormat:@"label=%@&data=%@", [[NSString alloc] initWithString:(NSString *)[[theTimer userInfo] objectForKey:@"label"]], [[NSString alloc] initWithString:(NSString *)[[theTimer userInfo] objectForKey:@"data"]]];

	NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

	NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
	NSMutableURLRequest *theRequest = [NSMutableURLRequest new];

	[theRequest setURL:[NSURL URLWithString:@"http://www.softberrykai.com/cc/objc.php"]];
	[theRequest setHTTPMethod:@"POST"];
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
	[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	[theRequest setHTTPBody:postData];

	NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

	if (theConnection) {

		recievedData = [NSMutableData data];

	}

	else {
		// Inform the user that the connection failed.
	}

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	[recievedData setLength:0];
	NSString *receivedDataString = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];
	recievedString = receivedDataString;
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	// Append the new data to receivedData.
	// receivedData is an instance variable declared elsewhere.

	[recievedData appendData:data];
	NSString *receivedDataString = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];
	[_returnedValue setStringValue:[NSString stringWithFormat:@"Feedback Recieved: '%@'", receivedDataString]];
	recievedString = receivedDataString;
	[self dataRecieved];
}

/* End of HTTP Code */

-(IBAction)transcieveData:(NSButton *)sender{
	[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(sendRequestWithTimer:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"cr",@"label",@" ",@"data",nil] repeats:YES];
}




-(NSArray *)coordinatesFromString:(NSString *)coordString{
	NSRange open = [coordString rangeOfString:@"x"];
	NSRange close = [coordString rangeOfString:@"y"];
	NSRange nr = NSMakeRange(open.location + 1, close.location - open.location - 1);
	NSString *xv = [coordString substringWithRange:nr];
	open = [coordString rangeOfString:@"y"];
	close = [coordString rangeOfString:@"|"];
	nr = NSMakeRange(open.location + 1, close.location - open.location - 1);
	NSString *yv = [coordString substringWithRange:nr];
	int x = [xv intValue];
	int y = [yv intValue];
	open = [coordString rangeOfString:@"b"];
	close = [coordString rangeOfString:@"c"];
	nr = NSMakeRange(open.location + 1, close.location - open.location - 1);
	NSString *bs = [coordString substringWithRange:nr];
	int b = [bs intValue];

	NSArray *coordinates = [NSArray arrayWithObjects:[NSNumber numberWithInt:x], [NSNumber numberWithInt:y], [NSNumber numberWithInt:b], nil];
	return coordinates;
}

- (NSArray *)parseString:(NSString *)string withMarkers:(NSArray *)markers {
	__block NSMutableArray *returnv = [NSMutableArray new];
	__block NSString *first = @"";
	__block NSString *second = @"";
	[markers enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop){
		second = obj;
		if ((int)idx != 0) {
			NSRange open = [string rangeOfString:first];
			NSRange close = [string rangeOfString:second];
			NSRange oc = NSMakeRange(open.location + 1, close.location - open.location - 1);
			NSString *segment = [string substringWithRange:oc];
			[returnv addObject:segment];
		}
		first = second;
	}];

	return [returnv copy];
}

-(void)processClickAtX:(int)x Y:(int)y{

}

-(void)dataRecieved{
	NSString *s = @"";
	if ([recievedString containsString:@"eventclick"]) {
		//General Click Handling


		self.parse = [NSArray arrayWithObjects:@"b", @"x", @"y", @"|", nil];
		parsed = [self parseString:recievedString withMarkers:self.parse];
		int b = [[parsed objectAtIndex:0] intValue];
		int x = [[parsed objectAtIndex:1] intValue];
		int y = [[parsed objectAtIndex:2] intValue];
		s = [NSString stringWithFormat:@"Click With Button: %i At x: %i y: %i",b,x,y];
		NSLog(@"Click With Button: %i At x: %i y: %i",b,x,y);
		[self sendRequestWithLabel:@"cs" data:s];





	}
	else if ([recievedString containsString:@"eventstartup"]) {
		//Initialization
	}
	else if ([recievedString containsString:@"eventkey"]){
		//Handle Key Press
		parsed = [self parseString:recievedString withMarkers:[NSArray arrayWithObjects:@"k", @"|", nil]];
		int k = [[parsed objectAtIndex:0] intValue];
		s = [NSString stringWithFormat:@"key pressed with keycode: %i", k];
		NSLog(@"key pressed with keycode: %i", k);
		[self sendRequestWithLabel:@"cs" data:s];
	}
	else if ([recievedString containsString:@"eventscroll"]){
		//Handle Scroll
		parsed = [self parseString:recievedString withMarkers:[NSArray arrayWithObjects:@"d",@"x",@"y",@"|", nil]];
		int d = [[parsed objectAtIndex:0] intValue];
		int x = [[parsed objectAtIndex:1] intValue];
		int y = [[parsed objectAtIndex:2] intValue];
		s = [NSString stringWithFormat:@"Scroll in direction: %i x: %i y: %i",d,x,y];
		NSLog(@"Scroll in direction: %i x: %i y: %i",d,x,y);
		[self sendRequestWithLabel:@"cs" data:s];
	}
	else if ([recievedString containsString:@"eventdrag"]){
		//Handle Mouse Drag
		parsed = [self parseString:recievedString withMarkers:[NSArray arrayWithObjects:@"b", @"x", @"y", @"|", nil]];
		int b = [[parsed objectAtIndex:0] intValue];
		int x = [[parsed objectAtIndex:1] intValue];
		int y = [[parsed objectAtIndex:2] intValue];
		s = [NSString stringWithFormat:@"Drag With Button: %i At x: %i y: %i",b,x,y];
		NSLog(@"Drag With Button: %i At x: %i y: %i",b,x,y);
		[self sendRequestWithLabel:@"cs" data:s];
	}
	else if ([recievedString containsString:@"eventdiskinsert"]){
		//Handle Disk Insertion
		parsed = [self parseString:recievedString withMarkers:[NSArray arrayWithObjects:@"s", @"|", nil]];
		NSString *s = [parsed objectAtIndex:0];
		s = [NSString stringWithFormat:@"Disk Inserted On %@ Side",s];
		NSLog(@"Disk Inserted On %@ Side",s);
		[self sendRequestWithLabel:@"cs" data:s];
	}
	else if ([recievedString containsString:@"eventdiskeject"]){
		parsed = [self parseString:recievedString withMarkers:[NSArray arrayWithObjects:@"s", @"|", nil]];
		NSString *s = [parsed objectAtIndex:0];
		s = [NSString stringWithFormat:@"Disk Ejected On %@ Side",s];
		NSLog(@"Disk Ejected On %@ Side",s);
		[self sendRequestWithLabel:@"cs" data:s];
	}
}

-(void)displayArray:(NSArray *)array{

}

@end

Edited on 09 May 2015 - 07:58 AM
Lyqyd #2
Posted 09 May 2015 - 04:45 AM
Moved to General.
Creator #3
Posted 09 May 2015 - 09:08 AM
So you are trying to make online OS, and only the client for it will be on the local PC?
Edited on 09 May 2015 - 07:08 AM
ValveCantCount #4
Posted 09 May 2015 - 09:52 AM
So you are trying to make online OS, and only the client for it will be on the local PC?
That's the basic idea.

In the long run, I'll be using a server-side database to handle, sort, and identify all incoming posts, and do similar IDing with objc. The plan is subject to change, of course, I don't know exactly where this will lead to.

I might have multiple instances running and distribute the data equally to reduce load, if it comes to it(which is completely ridiculous anyways)
Edited on 09 May 2015 - 07:54 AM
Creator #5
Posted 09 May 2015 - 10:25 AM
One idea is to use a login system since multiple users may be using it at the same time and you want to be able to differentiate them.
Engineer #6
Posted 09 May 2015 - 02:41 PM
What i internet is down for whatever reason, will the client then handle the events itself? If that is done as "back-up" system, why would you connect to the internet to handle events in the first place? What is the point I am not getting?
flaghacker #7
Posted 09 May 2015 - 04:34 PM
What i internet is down for whatever reason, will the client then handle the events itself? If that is done as "back-up" system, why would you connect to the internet to handle events in the first place? What is the point I am not getting?

No, I think the OS will be programmed in C. No backup in Lua…
FUNCTION MAN! #8
Posted 09 May 2015 - 04:53 PM
What i internet is down for whatever reason, will the client then handle the events itself? If that is done as "back-up" system, why would you connect to the internet to handle events in the first place? What is the point I am not getting?

No, I think the OS will be programmed in C. No backup in Lua…

Objective C.
lern de difference
Engineer #9
Posted 09 May 2015 - 06:15 PM
Objective C.
lern de difference
Oh pretty please, you dont get it do you?
If your internet shuts down for any reason, goodluck with this OS, because it is not going to work out….
Frankly, unless there is some backup mechanism, this seems pointless really. Its cool to program in a programming language you prefer over Lua, but the downside is that everything is going to be sent to a server which executes and then a response comes back. The concept is cool, but practically I think bad…

On a side note: learn*
ValveCantCount #10
Posted 09 May 2015 - 07:19 PM
Objective C.
lern de difference
Oh pretty please, you dont get it do you?
If your internet shuts down for any reason, goodluck with this OS, because it is not going to work out….
Frankly, unless there is some backup mechanism, this seems pointless really. Its cool to program in a programming language you prefer over Lua, but the downside is that everything is going to be sent to a server which executes and then a response comes back. The concept is cool, but practically I think bad…

On a side note: learn*

Yes, if my Internet or the client side Internet is down, then it won't work. Keep in mind that his is a concept, and the main reason I'm making it is for personal satisfaction. Besides, people have Internet most of the time.
Engineer #11
Posted 09 May 2015 - 08:03 PM
Yes, if my Internet or the client side Internet is down, then it won't work. Keep in mind that his is a concept, and the main reason I'm making it is for personal satisfaction. Besides, people have Internet most of the time.
Like I have said previously it is a cool concept, but practically it is not usefull. Making an assumption like: people have Internet most of the tie is technically a bad thing to do because we, as programmers, should consider every possibility IF we want to support that.

I want to encourage you to continue anyway, because it is for your satisfaction plus then it is proved we can program in other programming languages. If you consider me as someone who is against this project, I am not. Do what you want to do and whenever you like. I am just giving my feedback as a friendly guy, if anything came out wrong, then I will apologise for that.
ValveCantCount #12
Posted 09 May 2015 - 10:56 PM
Yes, if my Internet or the client side Internet is down, then it won't work. Keep in mind that his is a concept, and the main reason I'm making it is for personal satisfaction. Besides, people have Internet most of the time.
Like I have said previously it is a cool concept, but practically it is not usefull. Making an assumption like: people have Internet most of the tie is technically a bad thing to do because we, as programmers, should consider every possibility IF we want to support that.

I want to encourage you to continue anyway, because it is for your satisfaction plus then it is proved we can program in other programming languages. If you consider me as someone who is against this project, I am not. Do what you want to do and whenever you like. I am just giving my feedback as a friendly guy, if anything came out wrong, then I will apologise for that.

No no, thanks for reminding me about people without Internet. I will probably add some kind d backup or maybe single player support for a local client(with LAN)
DannySMc #13
Posted 13 May 2015 - 12:19 PM
This I can't see working or even being slightly useful… Imagine the response time for running calls to the computer… anyway:

I do not get why you don't just use C? :s Considering Lua compiles cleanly as C? You could easily fix the bits and bobs and make it work client side?
Creator #14
Posted 13 May 2015 - 02:08 PM
This I can't see working or even being slightly useful… Imagine the response time for running calls to the computer… anyway:

I do not get why you don't just use C? :s Considering Lua compiles cleanly as C? You could easily fix the bits and bobs and make it work client side?

He's got a point about response times. That may really be bothering. What I thought about was to use it as an external storage. For example, I have 100Mb of space on your system. I can then upload files and use them directly from computercraft.
DannySMc #15
Posted 13 May 2015 - 04:47 PM
This I can't see working or even being slightly useful… Imagine the response time for running calls to the computer… anyway:

I do not get why you don't just use C? :s Considering Lua compiles cleanly as C? You could easily fix the bits and bobs and make it work client side?

He's got a point about response times. That may really be bothering. What I thought about was to use it as an external storage. For example, I have 100Mb of space on your system. I can then upload files and use them directly from computercraft.

I have started a script for this xD, allows you to run virtual commands and use storage, like a http based rednet file browser / nsh kinda thing :P/>
Creator #16
Posted 13 May 2015 - 06:31 PM
Rather ftp.
DannySMc #17
Posted 13 May 2015 - 09:46 PM
Rather ftp.

Should I make an FTP type program? Would you actually use it?:D/> I had a small test one made but never actually released it?:P/> Should I?::DDD
ValveCantCount #18
Posted 14 May 2015 - 01:29 AM
This I can't see working or even being slightly useful… Imagine the response time for running calls to the computer… anyway:

I do not get why you don't just use C? :s Considering Lua compiles cleanly as C? You could easily fix the bits and bobs and make it work client side?

He's got a point about response times. That may really be bothering. What I thought about was to use it as an external storage. For example, I have 100Mb of space on your system. I can then upload files and use them directly from computercraft.

The response time is definitely a potential problem. ATM it isn't too bad, but I'll work out a solution if it comes to that.

One possibility is to send the user input info directly to the program, but with my server setup that would be a pain.
Edited on 13 May 2015 - 11:30 PM