Skip to content

Instantly share code, notes, and snippets.

@kambala-decapitator
Created November 20, 2022 13:59
Show Gist options
  • Save kambala-decapitator/17cea58b05572905991a5d29172a8fb4 to your computer and use it in GitHub Desktop.
Save kambala-decapitator/17cea58b05572905991a5d29172a8fb4 to your computer and use it in GitHub Desktop.
test Kodi RPC
//
// main.m
// TestInputStream
//
// Created by Andrey Filipenkov on 12.10.2022.
//
#import <Foundation/Foundation.h>
NSString *server;
int httpPort;
@interface A : NSObject <NSStreamDelegate>
@end
@implementation A
- (void)printResponse:(NSData *)data error:(NSError *)error {
if (error) {
NSLog(@"request error: %@", error);
return;
}
NSLog(@"raw response: %@", data ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : @"<empty>");
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
switch (eventCode) {
case NSStreamEventOpenCompleted: {
NSLog(@"stream opened, trying to fetch Kodi version...");
__auto_type urlStr = [NSString stringWithFormat:@"http://%@:%d/jsonrpc", server, httpPort];
__auto_type req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
req.HTTPMethod = @"POST";
req.HTTPBody = [NSJSONSerialization dataWithJSONObject:@{
@"jsonrpc": @"2.0",
@"method": @"Application.GetProperties",
@"id": @42,
@"params": @{
@"properties": @[
@"version",
@"volume",
@"name",
],
},
} options:kNilOptions error:NULL];
[req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[[NSURLSession.sharedSession dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[self printResponse:data error:error];
NSLog(@"now trying another approach...");
error = nil;
data = [NSURLConnection sendSynchronousRequest:req returningResponse:NULL error:&error];
[self printResponse:data error:error];
exit(0);
}] resume];
break;
}
case NSStreamEventHasBytesAvailable:
NSLog(@"stream has incoming data");
break;
case NSStreamEventErrorOccurred:
NSLog(@"stream error: %@", aStream.streamError);
exit(1);
break;
case NSStreamEventEndEncountered:
NSLog(@"stream ended");
exit(0);
break;
default:
NSLog(@"other stream event: %lu", eventCode);
break;
}
}
@end
int main(int argc, const char * argv[]) {
if (argc < 2) {
NSLog(@"please pass host IP/name");
return 1;
}
@autoreleasepool {
server = [NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
httpPort = argc < 3 ? 8080 : atoi(argv[2]);
CFReadStreamRef readStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(server), argc < 4 ? 9090 : atoi(argv[3]), &readStream, NULL);
__auto_type streamDelegate = [A new];
__auto_type inStream = (__bridge NSInputStream*)readStream;
inStream.delegate = streamDelegate;
[inStream scheduleInRunLoop:NSRunLoop.mainRunLoop forMode:NSDefaultRunLoopMode];
[inStream open];
[NSRunLoop.mainRunLoop run];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment