Ignore:
Timestamp:
Aug 26, 2007, 9:56:40 AM (13 years ago)
Author:
rhwood@…
Message:

Add the MPAgent (PalletHelper) calls and delegate methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • users/rhwood/Pallet/PortAuthority.m

    r23856 r28253  
    4545- (void)awakeFromNib
    4646{
     47        // Get a MacPorts Interpeter
     48        interpeter = [[MPInterp alloc] init];
     49        if (![interpeter loadPackage:MPPackageName version:MPPackageVersion usingCommand:MPPackageInit]) {
     50                NSLog(@"Failed to load interpeter");
     51                exit(1);
     52        }
     53        if (![interpeter redirectCommand:MPUIPuts toObject:self]) {
     54                NSLog(@"Failed to redirect interpeter input");
     55                exit(1);
     56        }
     57        NSLog(@"Printing system_options");
     58        NSLog([[interpeter getVariable:[MPObject objectWithString:@"system_options"]] stringValue]);
     59        NSLog(@"system_options printed");
    4760        // Load/set preferences/defaults
    4861        if (![[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"portInstallationPath"]) {
     
    6982        portIsRunning = NO;
    7083        launcher = [[NSBundle mainBundle] pathForResource:@"Launcher" ofType:nil];
     84        agentTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
    7185        authPortTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
    7286        killTask = [[AuthorizedExecutable alloc] initWithExecutable:launcher];
     87        [agentTask setDelegate:self];
    7388        [authPortTask setDelegate:self];
    7489        [killTask setDelegate:self];
     
    7994        [[portsList headerView] setMenu:portsListHeaderMenu];
    8095        [[portsList cornerView] setMenu:portsListHeaderMenu];
    81        
    8296        // TESTING TESTING TESTING
    8397        NSLog(@"Ports Database path: %@", [self configurationSetting:@"portdbpath"]);
     
    89103- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    90104{
     105        // configure and register the connection to the PalletHelper
     106        connection = [NSConnection defaultConnection];
     107    [connection setRootObject: self];
     108    [connection enableMultipleThreads];
     109    if ([connection registerName:MPAppMessagePort] == NO) {
     110        NSRunAlertPanel(@"Pallet", @"Could not register Pallet connection on this host.", nil, @"Quit", nil);
     111        [NSApp terminate: self];
     112    }
     113       
    91114        // sync or selfupdate as required
    92115        if ([[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"updateMacPortsOnStartup"] boolValue] == YES) {
     
    95118                [self syncPortsList:nil];
    96119        }
    97         // List ports
     120        // List ports   
    98121        [self getPortIndexes];
    99122        [self populatePortIndex];
     
    101124
    102125#pragma mark PORT INDEX
     126
     127- (NSDictionary *)tPorts
     128{
     129        if (!_ports) {
     130                NSPropertyListFormat format;
     131                NSString *error;
     132                NSData *portsData = [[self agent] portsData];
     133                _ports= [[NSPropertyListSerialization propertyListFromData:portsData
     134                                                                                                  mutabilityOption:NSPropertyListMutableContainersAndLeaves
     135                                                                                                                        format:&format
     136                                                                                                  errorDescription:&error]
     137                        retain];
     138        }
     139        NSLog(_ports);
     140        return _ports;
     141}
    103142
    104143- (void)getPortIndexes
     
    114153        source = [[NSMutableString alloc] init];
    115154        sources = [[NSMutableDictionary alloc] init];
    116         path = [macPortsPath stringByAppendingPathComponent:@"etc/ports/sources.conf"];
     155        path = [macPortsPath stringByAppendingPathComponent:@"etc/macports/sources.conf"];
    117156        file = [[NSString alloc] initWithContentsOfFile:path];
    118157        conf = [[NSArray alloc] initWithArray:[file componentsSeparatedByString:@"\n"]];
     
    142181{
    143182        NSMutableString *path;
     183        NSRange range;
    144184        path = [[NSMutableString alloc] init];
    145         if ([[source substringToIndex:4] isEqualToString:@"file"]) {
    146                 [path setString:[source substringFromIndex:7]];
    147         } else if ([[source substringToIndex:5] isEqualToString:@"rsync"]) {
    148                 [path setString:source];
    149                 [path replaceOccurrencesOfString:@"/" withString:@"_" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [path length])];
    150                 [path replaceOccurrencesOfString:@":__" withString:@"." options:NSCaseInsensitiveSearch range:NSMakeRange(0, [path length])];
    151                 [path setString:[[macPortsPath stringByAppendingPathComponent:@"var/db/dports/sources"] stringByAppendingPathComponent:path]];
     185        range = [source rangeOfString:@"://"];
     186        [path setString:[source substringFromIndex:(range.location + range.length)]];
     187        // if the source URL is not a file:// type URL, prepend it with ${prefix}/var/macports/sources
     188        if (![[source substringToIndex:4] isEqualToString:@"file"]) {
     189                [path setString:[[macPortsPath stringByAppendingPathComponent:@"var/macports/sources"] stringByAppendingPathComponent:path]];
    152190        }
    153191        return [path stringByAppendingPathComponent:@"PortIndex"];
     
    304342                int tclResult;
    305343                Tcl_Interp *tcl;
    306                 NSString *script = [[NSString alloc] initWithFormat:@"source %@/etc/ports/ports.conf\nproc %@ x {puts [list %2$@ $x]}", macPortsPath, setting, nil];
     344                NSString *script = [[NSString alloc] initWithFormat:@"source %@/etc/macports/macports.conf\nproc %@ x {puts [list %2$@ $x]}", macPortsPath, setting, nil];
    307345                NSLog(script);
    308346                tcl = Tcl_CreateInterp();
     
    572610}
    573611
     612#pragma mark MACPORTS INTERPETER
     613
     614- (NSDictionary *)listPorts:(NSString *)query
     615{   
     616    Tcl_Obj **objv;
     617    int count = 0, i=0;
     618    MPObject *result;
     619    NSString *error;
     620    NSMutableDictionary *portsData;
     621
     622        //    [_ports removeAllObjects];
     623    result = [interpeter evaluateCommand:[MPObject objectWithString:MPSearchCommand]
     624                                                          withObject:[MPObject objectWithString:query]];
     625    Tcl_ListObjGetElements(NULL, [result tclObj], &count, &objv);       
     626    while (++i < count) {       
     627        NSMutableDictionary *portDict = [NSMutableDictionary dictionary];
     628        Tcl_Obj **innerobjs;
     629        int innercount = 0, j = 0;   
     630        Tcl_ListObjGetElements(NULL, objv[i++], &innercount, &innerobjs);
     631        while (j < innercount)
     632        {
     633            MPObject *keyObject = [MPObject objectWithTclObj: innerobjs[j++]];
     634            MPObject *valueObject = [MPObject objectWithTclObj: innerobjs[j++]];
     635            NSString *key = [keyObject stringValue];
     636            id value;
     637            if ([key isEqualToString: MPCategoriesKey] || [key isEqualToString: MPMaintainersKey]) {
     638                NSEnumerator *enm = [[[valueObject stringValue] componentsSeparatedByString: @" "] objectEnumerator];
     639                NSString *component;
     640                value = [NSMutableArray array];
     641                while (component = [enm nextObject]) {
     642                    if (![value containsObject: component]) {
     643                        [value addObject: component];
     644                    }
     645                }
     646            } else if ([key rangeOfString: MPDependsKey].location != NSNotFound) {
     647                NSEnumerator *dependencyEnm = [[[valueObject stringValue] componentsSeparatedByString: @" "] objectEnumerator];
     648                NSString *component;
     649                value = [NSMutableArray array];
     650                while (component = [dependencyEnm nextObject]) {
     651                    NSString *dependencyName = [[component componentsSeparatedByString: @":"] objectAtIndex: 2];
     652                    if (![value containsObject: dependencyName]) {
     653                        [value addObject: dependencyName];
     654                    }
     655                }
     656                key = MPDependsKey;
     657            } else {
     658                value = [valueObject stringValue];
     659            }
     660            [portDict setObject:value forKey:key];
     661        }
     662        [portsData setObject:portDict forKey:[portDict objectForKey:MPNameKey]];
     663    }
     664    return [NSDictionary dictionaryWithDictionary:portsData];
     665}
     666
     667- (MPObject *) ui_puts: (NSArray *)array
     668{
     669    NSDictionary *message = [[array objectAtIndex: 1] dictionaryValue];
     670    if (message == nil)
     671        return [MPObject objectWithString: MPNoResult];
     672       
     673    NSString *data = [message objectForKey: @"data"];
     674    NSString *priority = [message objectForKey: @"priority"];
     675    if (data == nil || priority == nil)
     676        return [MPObject objectWithString: MPNoResult];
     677       
     678    id delegate = [[[NSThread currentThread] threadDictionary] objectForKey: @"delegate"];
     679    //[delegate displayMessage: message forPortName: [_currentOp objectForKey: @"portName"]];
     680    return [MPObject objectWithString: MPYesResult];
     681}
     682
     683#pragma mark MACPORTS AGENT
     684
     685- (id <MPAgentProtocol>)agent {
     686        if (agentIsBusy) {
     687                return nil;
     688        }
     689        agentIsBusy = YES;
     690        if (!agent) {
     691                NSString *agentPath = [[NSBundle mainBundle] pathForResource:@"PalletHelper" ofType:@""];
     692                int i;
     693//              [self launchAuthorizedExecutableWithArguments:];
     694                [agentTask setArguments:[NSArray arrayWithObjects:agentPath, nil]];
     695                [agentTask authorizeWithQuery];
     696                [agentTask start];
     697                for (i = 0; i < 10; i++) {
     698                        connection = [[NSConnection connectionWithRegisteredName:MPAgentMessagePort host:nil] retain];
     699                        if (connection) {
     700                                break;
     701                        }
     702                        sleep(1);
     703                }
     704                if (!connection) {
     705                        NSRunAlertPanel(@"Pallet", @"Could not connect to PalletHelper.", nil, nil, nil);
     706                        // exit(0); don't exit as I want to see the bloody console window
     707                }
     708                agent = [[connection rootProxy] retain];
     709                [(NSDistantObject *)agent setProtocolForProxy:@protocol(MPAgentProtocol)];
     710                [connection setRootObject:self];
     711                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionDidDie:) name:NSConnectionDidDieNotification object:connectionInvalid];
     712        }
     713        agentIsBusy = NO;
     714        return agent;
     715}
     716
     717- (void)connectionDidDie:(id)server
     718{
     719    NSRunAlertPanel(@"Pallet", @"Connection to PalletHelper died!", nil, nil, nil);
     720    agent = nil;
     721    // resetting agent to nil will cause a new instance of the agent to
     722    // be spawned by [PortAuthority agent] next time someone tries to access it
     723}
     724
     725- (oneway void)displayMessage:(in bycopy NSDictionary *)message forPortName:(in bycopy NSString *)portName {
     726        [self appendOutput:[message objectForKey:@"data"]];
     727}
     728
     729- (void)executeTarget:(NSString *)target forPortName:(NSString *)portName {
     730        NSDictionary *operation = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
     731                portName, @"portName",
     732                target, "@target",
     733                nil];
     734        if (!operations) {
     735                operations = [[NSMutableArray alloc] init];
     736        }
     737        [operations addObject:operation];
     738        [[self agent] executeTarget:target forPortName:portName];
     739}
     740
     741- (BOOL)shouldPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName {
     742        return YES;
     743}
     744
     745- (oneway void)willPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName {
     746        [progressIndicator startAnimation:nil];
     747        [NSApp setApplicationIconImage:[NSImage imageNamed:@"ApplicationIconBusy"]];
     748}
     749
     750- (oneway void)didPerformTarget:(in bycopy NSString *)target forPortName:(in bycopy NSString *)portName withResult:(in bycopy NSString *)result {
     751        [progressIndicator stopAnimation:nil];
     752        [NSApp setApplicationIconImage:[NSImage imageNamed:@"ApplicationIcon"]];
     753        [status setStringValue:@""];
     754}
     755
    574756#pragma mark AUTHORIZED EXECUTABLE DELEGATES / TASK WRAPPER CONTROLLER
    575757// This callback is implemented as part of conforming to the ProcessController protocol.
Note: See TracChangeset for help on using the changeset viewer.