shlist

share and manage lists between multiple people
Log | Files | Refs

commit aa3d22bff3e0a42a3106288b8bba6f7375edf6e2
parent 973662d4dabe1457d952a0ce2856eddec1dc6a01
Author: Kyle Milz <kyle@Kyles-MacBook-Pro.local>
Date:   Thu, 10 Sep 2015 01:14:57 -0600

ios-ng: add function for generic message sending

Diffstat:
Mios-ng/shlist/Base.lproj/Main.storyboard | 4++--
Mios-ng/shlist/NewListViewController.m | 2+-
Mios-ng/shlist/SharedList.h | 2+-
Mios-ng/shlist/SharedListsTableViewController.m | 149++++++++++++++-----------------------------------------------------------------
Mios-ng/shlist/ShlistServer.h | 4++--
Mios-ng/shlist/ShlistServer.m | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
6 files changed, 111 insertions(+), 133 deletions(-)

diff --git a/ios-ng/shlist/Base.lproj/Main.storyboard b/ios-ng/shlist/Base.lproj/Main.storyboard @@ -245,7 +245,7 @@ </objects> <point key="canvasLocation" x="1967" y="61"/> </scene> - <!--Lists--> + <!--Shlist--> <scene sceneID="hc1-Lv-WtP"> <objects> <tableViewController id="x0J-ua-E8Q" customClass="SharedListsTableViewController" sceneMemberID="viewController"> @@ -311,7 +311,7 @@ </connections> </tableView> <toolbarItems/> - <navigationItem key="navigationItem" title="Lists" id="uhM-V6-aB7"> + <navigationItem key="navigationItem" title="Shlist" id="uhM-V6-aB7"> <barButtonItem key="leftBarButtonItem" systemItem="edit" id="XHh-J3-UVD"/> <barButtonItem key="rightBarButtonItem" systemItem="add" id="hNc-gt-QbU"> <connections> diff --git a/ios-ng/shlist/NewListViewController.m b/ios-ng/shlist/NewListViewController.m @@ -35,7 +35,7 @@ self.shared_list = [[SharedList alloc] init]; self.shared_list.list_name = self.textField.text; // self.shared_list.list_date = self.datePicker.date; - self.shared_list.list_members = @"Test, Test"; + self.shared_list.list_members = @"You"; NSLog(@"NewListViewController::prepareForSegue(): %@", self.textField.text); } diff --git a/ios-ng/shlist/SharedList.h b/ios-ng/shlist/SharedList.h @@ -7,7 +7,7 @@ // UILabel *names; @property NSString *list_name; -@property NSString *list_id; +@property NSData *list_id; @property NSString *list_members; @property NSDate *list_date; diff --git a/ios-ng/shlist/SharedListsTableViewController.m b/ios-ng/shlist/SharedListsTableViewController.m @@ -9,7 +9,6 @@ @interface SharedListsTableViewController () @property (strong, nonatomic) ShlistServer *server; -@property (strong, nonatomic) NSData *device_id; @end @@ -17,49 +16,15 @@ - (void) load_initial_data { - // register if we've never registered before - // load local shared list data from db - // sync with server and check if there's any updates - - // initialize connection + // create one and only server instance, this gets passed around _server = [[ShlistServer alloc] init]; _server->shlist_tvc = self; - - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *documentsDirectory = [paths objectAtIndex:0]; - NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"shlist_key"]; - - // NSError *error = nil; - // [[NSFileManager defaultManager] removeItemAtPath:destinationPath error:&error]; - - if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) { - // do a fake registration - NSData *msg_register = [NSData dataWithBytes:"\x00\x00\x00\x0a" "4037082094" length:15]; - [_server writeToServer:msg_register]; - NSLog(@"Sent registration"); - } - - // send bulk shared list update - NSMutableData *msg = [NSMutableData data]; - [msg appendBytes:"\x00\x03" length:2]; - - // read device id from filesystem into memory - _device_id = [NSData dataWithContentsOfFile:destinationPath]; - // write length of device id as uint16 - uint16_t dev_id_len_network = htons([_device_id length]); - [msg appendBytes:&dev_id_len_network length:2]; - - // append device id itself - [msg appendData:_device_id]; - - // NSLog(@"SharedListsTableViewController::load_initial_data() device id lenth = %i", device_id_length); - - // NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:@"SBFormattedPhoneNumber"]; - // NSLog(@"%@\n", num); - - // ShlistServer *server = [[ShlistServer alloc] init]; - [_server writeToServer:msg]; + if ([_server prepare]) { + NSLog(@"info: server connection prepared"); + // bulk update, doesn't take a payload + [_server send_message:3 contents:nil]; + } } - (IBAction) unwindToList:(UIStoryboardSegue *)segue @@ -74,25 +39,9 @@ [self.shared_lists addObject:list]; [self.tableView reloadData]; - // new list message - NSMutableData *msg = [NSMutableData data]; - [msg appendBytes:"\x00\x01" length:2]; - - // length = device id + list name + null separator - uint16_t length_network_endian = htons([_device_id length] + [list.list_name length] + 1); - [msg appendBytes:&length_network_endian length:2]; - - // append device id - [msg appendData:_device_id]; - - // append null separator - [msg appendBytes:"\0" length:1]; - - // append new list name - [msg appendData:[list.list_name dataUsingEncoding:NSUTF8StringEncoding]]; - - // send message - [_server writeToServer:msg]; + // send new list message with new list name as payload + NSData *payload = [list.list_name dataUsingEncoding:NSUTF8StringEncoding]; + [_server send_message:1 contents:payload]; NSLog(@"unwindToList(): done"); } @@ -190,18 +139,18 @@ return @"you're not in any lists"; } else if ([self.shared_lists count] == 1) { - return @"list you are in"; + return @"shared list"; } - return @"lists you are in"; + return @"shared lists"; } else if (section == 1) { if ([self.indirect_lists count] == 0) { - return @"your friends don't have any lists"; + return @"no other shared lists"; } else if ([self.indirect_lists count] == 1) { - return @"list your friends are in"; + return @"other shared list"; } - return @"lists your friends are in"; + return @"other shared lists"; } return @""; } @@ -227,7 +176,9 @@ } // Override to support editing the table view. -- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +- (void) tableView:(UITableView *)tableView + commitEditingStyle:(UITableViewCellEditingStyle)editingStyle + forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source @@ -238,25 +189,8 @@ NSLog(@"info: leaving list '%@'", selected_list.list_name); - // delete list message - NSMutableData *msg = [NSMutableData data]; - [msg appendBytes:"\x00\x05" length:2]; - - // length = device id + null separator + list id - uint16_t length_network_endian = htons([_device_id length] + [selected_list.list_id length] + 1); - [msg appendBytes:&length_network_endian length:2]; - - // append device id - [msg appendData:_device_id]; - - // append null separator - [msg appendBytes:"\0" length:1]; - - // append new list name - [msg appendData:[selected_list.list_id dataUsingEncoding:NSUTF8StringEncoding]]; - - // send message - [_server writeToServer:msg]; + // send leave list message + [_server send_message:5 contents:selected_list.list_id]; // [self.shared_lists removeObjectAtIndex:[indexPath row]]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { @@ -267,25 +201,8 @@ NSLog(@"info: joining list '%@'", selected_list.list_name); - // join list message - NSMutableData *msg = [NSMutableData data]; - [msg appendBytes:"\x00\x04" length:2]; - - // length = device id + null separator + list id - uint16_t length_network_endian = htons([_device_id length] + [selected_list.list_id length] + 1); - [msg appendBytes:&length_network_endian length:2]; - - // append device id - [msg appendData:_device_id]; - - // append null separator - [msg appendBytes:"\0" length:1]; - - // append new list name - [msg appendData:[selected_list.list_id dataUsingEncoding:NSUTF8StringEncoding]]; - - // send message - [_server writeToServer:msg]; + // send join list message + [_server send_message:4 contents:selected_list.list_id]; } } @@ -304,7 +221,8 @@ */ // In a storyboard-based application, you will often want to do a little preparation before navigation -- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { +- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. @@ -319,25 +237,8 @@ // has to be done before issuing network request _server->shlist_ldvc = segue.destinationViewController; - // update list items message type - NSMutableData *msg = [NSMutableData data]; - [msg appendBytes:"\x00\x06" length:2]; - - // length = device id + list name + null separator - uint16_t length_network_endian = htons([_device_id length] + [list.list_id length] + 1); - [msg appendBytes:&length_network_endian length:2]; - - // append device id - [msg appendData:_device_id]; - - // append null separator - [msg appendBytes:"\0" length:1]; - - // append new list name - [msg appendData:[list.list_id dataUsingEncoding:NSUTF8StringEncoding]]; - - // send message - [_server writeToServer:msg]; + // send update list items message + [_server send_message:6 contents:list.list_id]; } // DetailObject *detail = [self detailForIndexPath:path]; diff --git a/ios-ng/shlist/ShlistServer.h b/ios-ng/shlist/ShlistServer.h @@ -13,7 +13,7 @@ } - -- (void) writeToServer:(NSData *)data; +- (bool) prepare; +- (void) send_message:(uint16_t)msg_type contents:(NSData *)data; @end \ No newline at end of file diff --git a/ios-ng/shlist/ShlistServer.m b/ios-ng/shlist/ShlistServer.m @@ -7,6 +7,9 @@ @property (strong, retain) NSMutableData *data; @property (strong, retain) AddressBook *address_book; +@property (strong, nonatomic) NSString *phone_number; +@property (strong, nonatomic) NSData *device_id; + @end @implementation ShlistServer @@ -40,6 +43,72 @@ return self; } +- (bool) prepare +{ + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *documentsDirectory = [paths objectAtIndex:0]; + + // NSString *phone_num_file = [documentsDirectory stringByAppendingPathComponent:@"phone_num"]; + NSString *device_id_file = [documentsDirectory stringByAppendingPathComponent:@"shlist_key"]; + + // NSError *error = nil; + // [[NSFileManager defaultManager] removeItemAtPath:destinationPath error:&error]; + + // TODO: also check the length of the file + if (![[NSFileManager defaultManager] fileExistsAtPath:device_id_file]) { + // no device id file found, send a registration message + NSMutableData *msg = [NSMutableData data]; + + // message type 0 + [msg appendBytes:"\x00\x00" length:2]; + + // phone number length is 9 + uint16_t length_network = htons(9); + [msg appendBytes:&length_network length:2]; + + // actual phone number + const char *phone_number = "4037082094"; + _phone_number = @"4037082094"; + [msg appendBytes:phone_number length:9]; + + [self writeToServer:msg]; + NSLog(@"info: sent registration message"); + + // we don't have a device id so we can't do anything yet + return false; + } + + // read device id from filesystem into memory + _device_id = [NSData dataWithContentsOfFile:device_id_file]; + + return true; +} + +- (void) send_message:(uint16_t)msg_type contents:(NSData *)payload +{ + NSMutableData *msg = [NSMutableData data]; + + uint16_t msg_type_network = htons(msg_type); + [msg appendBytes:&msg_type_network length:2]; + + int payload_length = 0; + if (payload) + // include null separator in this length + payload_length = [payload length] + 1; + + uint16_t msg_len_network = htons([_device_id length] + payload_length); + [msg appendBytes:&msg_len_network length:2]; + + [msg appendData:_device_id]; + + if (payload) { + [msg appendBytes:"\0" length:1]; + [msg appendData:payload]; + } + + [self writeToServer:msg]; +} + - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { switch (eventCode) { @@ -119,6 +188,12 @@ // if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) { [data writeToFile:destinationPath atomically:YES]; // } + + // set this so we're ready to send other message types + _device_id = data; + + // do a bulk list update + [self send_message:3 contents:nil]; } if (msg_type == 1) { @@ -242,13 +317,15 @@ NSArray *phone_numbers = [list_fields subarrayWithRange:NSMakeRange(2, field_count - 2)]; for (id phone_number in phone_numbers) { - /* try to find the list member in our address book */ + // try to find the list member in our address book NSString *name = _address_book.name_map[phone_number]; if (name) [friends addObject:name]; + else if ([phone_number compare:_phone_number]) + [friends addObject:@"You"]; else - /* didn't find it, you don't know this person */ + // didn't find it, you don't know this person others++; } @@ -271,7 +348,7 @@ SharedList *shared_list = [[SharedList alloc] init]; shared_list.list_name = [list_fields objectAtIndex:0]; - shared_list.list_id = [list_fields objectAtIndex:1]; + shared_list.list_id = [[list_fields objectAtIndex:1] dataUsingEncoding:NSUTF8StringEncoding]; shared_list.list_members = members_str; [output addObject:shared_list];