title: Vending an Object with Distributed Objects and Synchronizing access
body: <p>I'm using Distributed Objects in my application to provide communication between a tiny authentication agent program and the main application.  The main application spawns NSTask's, which in turn will invoke the authentication program, which in turn will call back to the main program to get some user credentials.  I know it sounds convoluted, but it has to work this way because of the way ssh gets passwords from a program in SSH_ASKPATH. </p>
  
  <p>In my main application I have a single instance of an object called PasswordDialog, which I vend as follows;</p>
  
  <pre><code>PasswordDialog *vendedPasswordDialog=[[PasswordDialog alloc] init];  
  [[NSConnection defaultConnection] setRootObject:vendedPasswordDialog];
  [[NSConnection defaultConnection] registerName:@"MyConnectionName"]
  </code></pre>
  
  <p>This happens once when the application starts up.</p>
  
  <p>In the authentication agent, I call back to this object as follows;</p>
  
  <pre><code>NSConnection *passwordDialogConnection = [NSConnection connectionWithRegisteredName:@"MyConnectionName" host:nil];
  
  PasswordDialog *pdProxy = (PasswordDialog*)[passwordDialogConnection rootProxy];
  
  NSString *responseStr = [pdProxy responseForPID:pidString host:hostnameString user:usernameString processInfo:[NSProcessInfo processInfo]];
  </code></pre>
  
  <p>The problem is that this doesn't work as I'd like. If there are multiple Authentication agents executing simultaneously I find that they can all access the same object and call the same method simultaneously.  For example if I put a log statement at the beginning of "responseForPID" and at the end I get output like this;</p>
  
  <pre><code>beginMethod PID 3618848 on thread &lt;NSThread: 0x30b050&gt;{name = (null), num = 1} for object &lt;PasswordDialog: 0x485460&gt;
  beginMethod PID 4882384 on thread &lt;NSThread: 0x30b050&gt;{name = (null), num = 1} for object &lt;PasswordDialog: 0x485460&gt;
  beginMethod PID 4872848 on thread &lt;NSThread: 0x30b050&gt;{name = (null), num = 1} for object &lt;PasswordDialog: 0x485460&gt;
  
  PID endMethod 4872848
  PID endMethod 4882384
  PID endMethod 3618848
  </code></pre>
  
  <p>Which seems like there are three different processes entering my method on the same thread on an object with the same address.  Obviously I'm missing some fundamental understanding of Distributed Objects and threads here, but that seems like magic? </p>
  
  <p>So eventually I get to my question(s)</p>
  
  <p>(a) can someone explain what is going on here (ie how I can get log statements as above)</p>
  
  <p>(b) Obviously I'm using Distributed objects incorrectly for my purpose. Can anyone suggest  how I could do it correctly.</p>
  
  <p>Thanks and thanks for reading to the end of this question!</p>
  
  <hr>
  
  <p>Edit:
  Just to clarify. My "responseForPID" method looks something like this;</p>
  
  <pre><code>- (NSString*) responseForPID:(NSString*) pid host:(NSString*)hostname user:(NSString*) username processInfo:(NSProcessInfo*) info {
  
  NSLog(@"beginMethod PID %@ on thread %@ for object %@",pid,[NSThread currentThread],self);
  
  .... code to get the password 
  
  NSLog(@"PID endMethod %@",pid);
  return passwordString;
  
  }
  </code></pre>