2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: DNSServiceReg.m,v $
26 Revision 1.13 2003/08/12 19:55:07 cheshire
31 #import "RegistrationController.h"
33 #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
41 printf("Got a reply from the server with error %d\n", errorCode);
46 MyHandleMachMessage ( CFMachPortRef port, void * msg, CFIndex size, void * info )
48 DNSServiceDiscovery_handleReply(msg);
51 @implementation RegistrationController
53 - (void)registerDefaults
55 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
57 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp.", @"_ssh._tcp.", @"_tftp._tcp.", @"_http._tcp.", @"_printer._tcp.", @"_afpovertcp._tcp.", nil];
58 NSArray *nameArray = [NSArray arrayWithObjects:@"My ftp Server", @"My Computer", @"Testing Boot Image", @"A Web Server", @"SteveĆs Printer", @"Company AppleShare Server", nil];
59 NSArray *portArray = [NSArray arrayWithObjects:@"21", @"22", @"69", @"80", @"515", @"548", nil];
60 NSArray *domainArray = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", nil];
61 NSArray *textArray = [NSArray arrayWithObjects:@"", @"", @"image=mybootimage", @"path=/index.html", @"rn=lpt1", @"Vol=Public", nil];
63 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
64 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
65 [regDict setObject:portArray forKey:@"SrvPortKeys"];
66 [regDict setObject:domainArray forKey:@"SrvDomainKeys"];
67 [regDict setObject:textArray forKey:@"SrvTextKeys"];
69 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
74 srvtypeKeys = [[NSMutableArray array] retain]; //Define arrays for Type, Domain, and Name
75 srvnameKeys = [[NSMutableArray array] retain];
76 srvportKeys = [[NSMutableArray array] retain];
77 srvdomainKeys = [[NSMutableArray array] retain];
78 srvtextKeys = [[NSMutableArray array] retain];
80 registeredDict = [[NSMutableDictionary alloc] init];
82 [self registerDefaults];
86 - (void)awakeFromNib //BrowserController startup procedure
88 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
89 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
90 [srvportKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvPortKeys"]];
91 [srvdomainKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvDomainKeys"]];
92 [srvtextKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTextKeys"]];
94 [serviceDisplayTable reloadData]; //Reload (redraw) data in fields
100 - (IBAction)registerService:(id)sender
102 int selectedRow = [serviceDisplayTable selectedRow];
103 CFRunLoopSourceRef rls;
104 uint16_t registerPort;
105 CFMachPortRef cfMachPort;
106 CFMachPortContext context;
107 Boolean shouldFreeInfo;
108 dns_service_discovery_ref dns_client;
111 if (selectedRow < 0) {
117 context.retain = NULL;
118 context.release = NULL;
119 context.copyDescription = NULL;
121 registerPort = [[srvportKeys objectAtIndex:selectedRow] intValue];
123 dns_client = DNSServiceRegistrationCreate
125 [[srvnameKeys objectAtIndex:selectedRow] UTF8String],
126 [[srvtypeKeys objectAtIndex:selectedRow] UTF8String],
127 [[srvdomainKeys objectAtIndex:selectedRow] UTF8String],
129 [[srvtextKeys objectAtIndex:selectedRow] UTF8String],
134 port = DNSServiceDiscoveryMachPort(dns_client);
138 //printf("port is %d\n", port);
140 cfMachPort = CFMachPortCreateWithPort ( kCFAllocatorDefault, port, ( CFMachPortCallBack ) MyHandleMachMessage,&context,&shouldFreeInfo );
142 rls = CFMachPortCreateRunLoopSource(NULL, cfMachPort, 0);
143 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
145 [registeredDict setObject:[NSNumber numberWithUnsignedInt:(unsigned int)dns_client] forKey:[srvtypeKeys objectAtIndex:selectedRow]];
147 printf("Could not obtain client port\n");
152 - (IBAction)unregisterService:(id)sender
154 int selectedRow = [serviceDisplayTable selectedRow];
155 NSString *key = [srvtypeKeys objectAtIndex:selectedRow];
157 NSNumber *refPtr = [registeredDict objectForKey:key];
158 dns_service_discovery_ref ref = (dns_service_discovery_ref)[refPtr unsignedIntValue];
161 DNSServiceDiscoveryDeallocate(ref);
162 [registeredDict removeObjectForKey:key];
166 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
171 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
173 return [srvtypeKeys count];
176 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
178 if (theColumn == typeColumn) {
179 return [srvtypeKeys objectAtIndex:rowIndex];
181 if (theColumn == nameColumn) {
182 return [srvnameKeys objectAtIndex:rowIndex];
184 if (theColumn == portColumn) {
185 return [srvportKeys objectAtIndex:rowIndex];
187 if (theColumn == domainColumn) {
188 return [srvdomainKeys objectAtIndex:rowIndex];
190 if (theColumn == textColumn) {
191 return [srvtextKeys objectAtIndex:rowIndex];
195 } //End of mandatory TableView methods
197 - (IBAction)removeSelected:(id)sender
199 // remove the selected row and force a refresh
201 int selectedRow = [serviceDisplayTable selectedRow];
205 [srvtypeKeys removeObjectAtIndex:selectedRow];
206 [srvnameKeys removeObjectAtIndex:selectedRow];
207 [srvportKeys removeObjectAtIndex:selectedRow];
208 [srvdomainKeys removeObjectAtIndex:selectedRow];
209 [srvtextKeys removeObjectAtIndex:selectedRow];
211 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
212 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
213 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
214 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
215 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
217 [serviceDisplayTable reloadData];
221 - (IBAction)addNewService:(id)sender
223 // add new entries from the edit fields to the arrays for the defaults
225 if ([[serviceTypeField stringValue] length] && [[serviceNameField stringValue] length] && [[serviceDomainField stringValue] length]&& [[servicePortField stringValue] length]) {
226 [srvtypeKeys addObject:[serviceTypeField stringValue]];
227 [srvnameKeys addObject:[serviceNameField stringValue]];
228 [srvportKeys addObject:[servicePortField stringValue]];
229 [srvdomainKeys addObject:[serviceDomainField stringValue]];
230 [srvtextKeys addObject:[serviceTextField stringValue]];
232 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
233 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
234 [[NSUserDefaults standardUserDefaults] setObject:srvportKeys forKey:@"SrvPortKeys"];
235 [[NSUserDefaults standardUserDefaults] setObject:srvdomainKeys forKey:@"SrvDomainKeys"];
236 [[NSUserDefaults standardUserDefaults] setObject:srvtextKeys forKey:@"SrvTextKeys"];
238 [serviceDisplayTable reloadData];