Cocoa Distributed Objects - objective-c

I've given myself a headache trying to figure out how to run this Distributed Objects demo. I can run it just fine locally on the same machine.
Here's the situation. I have a Server App that spawns a Client App [with OpenGLView] on a remote machine.
I can do this easy with AppleScript.
The Client App seems to Vend it's OpenGLView window OK:
clientPort = [[NSSocketPort alloc] initWithTCPPort:SERVER_PORT];
if(clientPort == nil) continue; else NSLog(#"Port OK");
clientConnection = [NSConnection connectionWithReceivePort:clientPort sendPort:nil];
if(clientConnection == nil) continue; else NSLog(#"Conn OK");
[[NSSocketPortNameServer sharedInstance] registerPort:clientPort name:#"DOTest3_0"];
//Vend Object
#try {
[clientConnection setRootObject:object];
NSLog([NSString stringWithFormat:#"Port %d: Vend OK", (SERVER_PORT + i)]);
return;
} #catch (...) {
NSLog([NSString stringWithFormat:#"Port %d: Vend Next", (SERVER_PORT + i)]);
}
The Server App finds the Port and Connection, but Raises a TimeOut exception:
// Create temporary Pointer to kGLView Object.
id <NSCoding, kGLViewProtocol> openGLView;
// Setup Port, Connection, & Proxy
portTest = (NSSocketPort *)[[NSSocketPortNameServer sharedInstance] portForName:#"DOTest3_0" host:#"*"];
if (portTest == nil ) continue ; else NSLog(#"Port OK");
connTest = [NSConnection connectionWithReceivePort:nil sendPort:portTest];
if (connTest == nil ) continue ; else NSLog(#"Conn OK");
openGLView = [[connTest rootProxy] retain];
if (openGLView == nil ) continue ; else NSLog(#"OpenGL OK");
[openGLView drawWithRotation: rotationAngle];
}
And I can't figure out for the life of Me WHY.
I get in the Console of the Client PC:
"Port OK"
"Conn OK"
"Port 8081: Vend OK"
I get in the Console of the Server PC:
"Port OK"
"Conn OK"
11/18/09 2:05:36 PM DOTest3[15278] [NSPortCoder sendBeforeTime:sendReplyPort:] timed out (10280263936.092180 280263936.092642) 1
Even if the TimeOuts are both set to 60 seconds.
Help!
-Stephen
Server: MacMini OS X 10.5
Client: MacPro OS X 10.6
Remote Login, Management, etc. are all enabled.
EDIT:
Taking NSResponder's suggestion, I've Vended the Controller, but it still isn't working.
Client/Vender:
-(void)vend:(id)object {
port = [[[NSSocketPort alloc] initWithTCPPort:[self tcpPort]]
retain];
conn = [[NSConnection connectionWithReceivePort:port sendPort:nil]
retain];
for (int i = 0; i < 10; i++) {
[[NSSocketPortNameServer sharedInstance] registerPort:port
name:[[self portName] stringByAppendingFormat:#"_%d", i]];
#try {
[conn setRootObject:object];
return;
} #catch (...) {
NSLog(#"Vend Next");
continue;
}
}
NSLog(#"Vend Failed");
}
Client Controller:
-(id)init {
self = [super init];
[self setRotationAngle:0.0f];
clientObj = [[Client alloc] initWithName:#"DOTest4"
Address:#"10.10.5.104" // mini
Port:48557];
[clientObj vend:self];
return self;
}
Server Controller:
-(IBAction)rotateClient:(id)sender {
NSArray *vendedObjects = [serverObj getVendedObjects];
id <NSCoding, ClientController_Protocol> proxy;
if (vendedObjects != nil) {
for (int i = 0; i < [vendedObjects count]; i++) {
proxy = [vendedObjects objectAtIndex:i];
[proxy rotate];
}
}
// release
[vendedObjects release];
}
Server/(grabs Vended Objects)
-(NSArray *)getVendedObjects {
NSArray *vendedObjects = [[[NSArray alloc] init] retain];
NSSocketPort *port;
NSConnection *conn;
for (int i = 0; i< 10; i++) {
// Get Port Object
port = (NSSocketPort *)[[NSSocketPortNameServer sharedInstance]
portForName:[[self portName] stringByAppendingFormat:#"_%d", i]
host:[self addressRemote]];
if (port == nil) continue;
// Create Connection with Timeouts
conn = [NSConnection connectionWithReceivePort:nil sendPort:port];
if (conn == nil) continue;
[conn setReplyTimeout:(NSTimeInterval)60.0];
[conn setRequestTimeout:(NSTimeInterval)60.0];
// Get VendedObject of Connection
vendedObjects = [[vendedObjects arrayByAddingObject:[conn rootProxy]] retain];
}
return vendedObjects;
}
Sigh... I'm sure I'm just overlooking something REALLY cocoa-basic here.
-S!

I've never seen anyone attempting to vend a view or a window across a DO link, and I'm surprised that it worked even on the local host. Anytime I've used DO, it's been from objects in the controller layer of the server to a corresponding controller in the client.

Related

(Objective-c/Mac OSX) How to distinguish managed AD users (AD user create mobile card) from local users on Mac OSX

<\RESOLVED>, Please see the first reply
My mac(10.9) has joined into a AD domain. In my program, I tried to recognize whether the current login user is local account or AD user. I can successfully distinguish them by using the following code.
+ (bool)isLocalUser:(NSString*)user
{
NSError *dirSearchError = nil;
ODRecord *foundUser = findUser(user, &dirSearchError);
if(foundUser !=nil)
{
return YES;
}else
{
return NO;
}
}
ODRecord *findUser(NSString *user, NSError **error)
{
NSLog(#"[MacLogonUI] findUser");
ODNode *searchNode = [ODNode nodeWithSession: [ODSession defaultSession]
type: kODNodeTypeLocalNodes
error: error];
if (searchNode == nil) {
return nil;
}
NSDictionary *nodeInfo = [searchNode nodeDetailsForKeys:nil error:error];
/* query this node for the user record we're interested in.
* We only need one result, which is why maximumResults is set to 1.
*/
ODQuery *userSearch = [ODQuery queryWithNode: searchNode
forRecordTypes: kODRecordTypeUsers
attribute: kODAttributeTypeRecordName
matchType: kODMatchEqualTo
queryValues: user
returnAttributes: kODAttributeTypeStandardOnly
maximumResults: 1
error: error];
if (userSearch == nil) {
return nil;
}
/* For this example we'll use a synchronous search. This could take a while
* so asynchronous searching is preferable.
*/
NSArray *foundRecords = [userSearch resultsAllowingPartial: NO error: error];
if (foundRecords == nil || [foundRecords count] == 0) {
return nil;
}
ODRecord *userRecord = [foundRecords objectAtIndex: 0];
return [[userRecord retain] autorelease];
}
While when the AD user create a mobile card, it is viewed as a managed user(from the System preference -> Users & Groups). The code also recognize this kind of AD user as local. How to deal with this kind of situation?
Do you guys have any idea of this problem?
I have solved this problem by myself. Hope the following code helps:
#import "DasUser.h"
#import <OpenDirectory/OpenDirectory.h>
#import <Collaboration/Collaboration.h>
#implementation DasUser
+ (bool)isLocalUser:(NSString*)user
{
NSError *dirSearchError = nil;
ODRecord *foundUser = findUser(user, &dirSearchError);
if(foundUser !=nil)
{
return YES;
}else
{
return NO;
}
}
ODRecord *findUser(NSString *user, NSError **error)
{
NSLog(#"[MacLogonUI] findUser");
CSIdentityAuthorityRef defaultAuthority = CSGetManagedIdentityAuthority();
CSIdentityClass identityClass = kCSIdentityClassUser;
CSIdentityQueryRef query = CSIdentityQueryCreate(NULL, identityClass, defaultAuthority);
CFErrorRef err = NULL;
CSIdentityQueryExecute(query, 0, &err);
CFArrayRef results = CSIdentityQueryCopyResults(query);
int numResults = CFArrayGetCount(results);
NSMutableArray * managedUsers = [NSMutableArray array];
for (int i = 0; i < numResults; ++i) {
CSIdentityRef identity = (CSIdentityRef)CFArrayGetValueAtIndex(results, i);
CBIdentity * identityObject = [CBIdentity identityWithCSIdentity:identity];
NSString* posixName = [identityObject posixName];
[managedUsers addObject:posixName];
}
CFRelease(results);
CFRelease(query);
ODNode *searchNode = [ODNode nodeWithSession: [ODSession defaultSession]
type: kODNodeTypeLocalNodes
error: error];
if (searchNode == nil) {
return nil;
}
/* query this node for the user record we're interested in.
* We only need one result, which is why maximumResults is set to 1.
*/
ODQuery *userSearch = [ODQuery queryWithNode: searchNode
forRecordTypes: kODRecordTypeUsers
attribute: kODAttributeTypeRecordName
matchType: kODMatchEqualTo
queryValues: user
returnAttributes: kODAttributeTypeStandardOnly
maximumResults: 1
error: error];
if (userSearch == nil) {
return nil;
}
/* For this example we'll use a synchronous search. This could take a while
* so asynchronous searching is preferable.
*/
NSArray *foundRecords = [userSearch resultsAllowingPartial: NO error: error];
if([foundRecords count]>0)
{
NSString *nameStr = [foundRecords[0] recordName];
NSLog(#"[MacLogonUI] findUser nameStr %#", nameStr);
int j;
for( j = 0; j<[managedUsers count]; j++)
{
if([nameStr isEqualToString:managedUsers[j]])
{
break;
}
}
if(j<[managedUsers count])
{
foundRecords = nil;
}
}
if (foundRecords == nil || [foundRecords count] == 0) {
return nil;
}
ODRecord *userRecord = [foundRecords objectAtIndex: 0];
return [[userRecord retain] autorelease];
}
#end
While when network of the mac is disconnected. The managed user can not be listed. Is there anybody has any idea of this?

Close connection when NSOutputStream has finished

How can a connection be closed when the NSOutputStream has finished sending data?
After searching around i have found that the event NSStreamEventEndEncountered is only called if the server drops the connection. not if the OutputStream has finished the data to send.
StreamStatus is Always returning 0 (connection closed) or 2 (connection open) but never 4 (writing data).
since both methods mentioned above are not telling me enough about the write process i am not able to find a way do determine if the Stream is still writing or if it has finished and i can close the connection now.
After 5 days of googleling and trying i am totally out of ideas... Any help appreciated. Thanks
EDIT ADDED CODE AS REQUESTED:
- (void)startSend:(NSString *)filePath
{
BOOL success;
NSURL * url;
assert(filePath != nil);
assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
assert( [filePath.pathExtension isEqual:#"png"] || [filePath.pathExtension isEqual:#"jpg"] );
assert(self.networkStream == nil); // don't tap send twice in a row!
assert(self.fileStream == nil); // ditto
// First get and check the URL.
...
....
.....
// If the URL is bogus, let the user know. Otherwise kick off the connection.
...
....
.....
if ( ! success) {
self.statusLabel.text = #"Invalid URL";
} else {
// Open a stream for the file we're going to send. We do not open this stream;
// NSURLConnection will do it for us.
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
assert(self.fileStream != nil);
[self.fileStream open];
// Open a CFFTPStream for the URL.
self.networkStream = CFBridgingRelease(
CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url)
);
assert(self.networkStream != nil);
if ([self.usernameText.text length] != 0) {
success = [self.networkStream setProperty:self.usernameText.text forKey:(id)kCFStreamPropertyFTPUserName];
assert(success);
success = [self.networkStream setProperty:self.passwordText.text forKey:(id)kCFStreamPropertyFTPPassword];
assert(success);
}
self.networkStream.delegate = self;
[self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
///////******** LINE ADDED BY ME TO DISONNECT FROM FTP AFTER CLOSING CONNECTION *********////////////
[self.networkStream setProperty:(id)kCFBooleanFalse forKey:(id)kCFStreamPropertyFTPAttemptPersistentConnection];
///////******** END LINE ADDED BY ME *********////////////
[self.networkStream open];
// Tell the UI we're sending.
[self sendDidStart];
}
}
- (void)stopSendWithStatus:(NSString *)statusString
{
if (self.networkStream != nil) {
[self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.networkStream.delegate = nil;
[self.networkStream close];
self.networkStream = nil;
}
if (self.fileStream != nil) {
[self.fileStream close];
self.fileStream = nil;
}
[self sendDidStopWithStatus:statusString];
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
// An NSStream delegate callback that's called when events happen on our
// network stream.
{
#pragma unused(aStream)
assert(aStream == self.networkStream);
switch (eventCode) {
case NSStreamEventOpenCompleted: {
[self updateStatus:#"Opened connection"];
} break;
case NSStreamEventHasBytesAvailable: {
assert(NO); // should never happen for the output stream
} break;
case NSStreamEventHasSpaceAvailable: {
[self updateStatus:#"Sending"];
// If we don't have any data buffered, go read the next chunk of data.
if (self.bufferOffset == self.bufferLimit) {
NSInteger bytesRead;
bytesRead = [self.fileStream read:self.buffer maxLength:kSendBufferSize];
if (bytesRead == -1) {
[self stopSendWithStatus:#"File read error"];
} else if (bytesRead == 0) {
[self stopSendWithStatus:nil];
} else {
self.bufferOffset = 0;
self.bufferLimit = bytesRead;
}
}
// If we're not out of data completely, send the next chunk.
if (self.bufferOffset != self.bufferLimit) {
NSInteger bytesWritten;
bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self stopSendWithStatus:#"Network write error"];
} else {
self.bufferOffset += bytesWritten;
}
}
} break;
case NSStreamEventErrorOccurred: {
[self stopSendWithStatus:#"Stream open error"];
} break;
case NSStreamEventEndEncountered: {
// FOR WHATEVER REASON THIS IS NEVER CALLED!!!!
} break;
default: {
assert(NO);
} break;
}
}
There can be two interpretations to your question. If what you are asking is "I have a NSOutputStream and I'm finished writing to it how do I signal this?" then the answer is as simple as call the close method on it.
Alternately, If what you are really saying is "I have a NSInputStream and I want to know when I've reached the end-of-stream" then you can look at hasBytesAvailable or streamStatus == NSStreamStatusAtEnd.
For your information, to actually get the status NSStreamStatusWriting you would need to be calling the streamStatus method from another thread while this thread is calling write:maxLength:.
--- Edit: Code Suggestion
The reason you would never get notified is that an output stream is never finished (unless it's a fixed size stream, which an FTP stream is not). It's the input stream that gets "finished" at which point you can close your output stream. That's the answer to your original question.
As a further suggestion, I would skip run loop scheduling and the "event processing" except for handling errors on the output stream. Then I would put the read/write code into a NSOperation subclass and send it off into a NSOperationQueue. By keeping a reference to the NSOperations in that queue you would be able to cancel them easily and even show a progress bar by adding a percentComplete property. I've tested the code below and it works. Replace my memory output stream with your FTP output stream. You will notice that I have skipped the validations, which you should keep of course. They should probably be done outside the NSOperation to make it easier to query the user.
#interface NSSendFileOperation : NSOperation<NSStreamDelegate> {
NSInputStream *_inputStream;
NSOutputStream *_outputStream;
uint8_t *_buffer;
}
#property (copy) NSString* sourceFilePath;
#property (copy) NSString* targetFilePath;
#property (copy) NSString* username;
#property (copy) NSString* password;
#end
#implementation NSSendFileOperation
- (void) main
{
static int kBufferSize = 4096;
_inputStream = [NSInputStream inputStreamWithFileAtPath:self.sourceFilePath];
_outputStream = [NSOutputStream outputStreamToMemory];
_outputStream.delegate = self;
[_inputStream open];
[_outputStream open];
_buffer = calloc(1, kBufferSize);
while (_inputStream.hasBytesAvailable) {
NSInteger bytesRead = [_inputStream read:_buffer maxLength:kBufferSize];
if (bytesRead > 0) {
[_outputStream write:_buffer maxLength:bytesRead];
NSLog(#"Wrote %ld bytes to output stream",bytesRead);
}
}
NSData *outputData = [_outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
NSLog(#"Wrote a total of %lu bytes to output stream.", outputData.length);
free(_buffer);
_buffer = NULL;
[_outputStream close];
[_inputStream close];
}
- (void) stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
// Handle output stream errors such as disconnections here
}
#end
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSOperationQueue *sendQueue = [[NSOperationQueue alloc] init];
NSSendFileOperation *sendOp = [[NSSendFileOperation alloc] init];
sendOp.username = #"test";
sendOp.password = #"test";
sendOp.sourceFilePath = #"/Users/eric/bin/data/english-words.txt";
sendOp.targetFilePath = #"/Users/eric/Desktop/english-words.txt";
[sendQueue addOperation:sendOp];
[sendQueue waitUntilAllOperationsAreFinished];
}
return 0;
}

Why does this serial communication code hang when talking to a USB-to-serial adapter?

I am writing an application that is supposed to control a projector from the desktop of a Mac Mini. I am writing the application in Objective-C using Xcode and termios.h to talk to the serial port. I am connecting from a USB-to-Serial adapter and I know the drivers are installed correctly because I can send HEX commands to the projector to control its functions.
I have code written that I believe should open the serial port, connect to the projector, send codes, and disconnect:
#implementation Projector
#synthesize helpUrl = _helpUrl;
#synthesize projectorConnected;
- (id)init {
//[self connectProjector];
if (TRUE == projectorConnected) {
// Success!
}
else {
// Error!
}
return self;
}
- (id)initWithHelpUrl:(NSString *)helpUrlString {
[self setHelpUrl:helpUrlString];
return [self init];
}
- (void)connectProjector {
[self setProjectorConnected:FALSE];
NSString *deviceNameString = #"/dev/tty.usbserial";
speed_t baudRate = B19200;
serialFileDescriptor = [self openSerialPort:[deviceNameString cStringUsingEncoding:NSASCIIStringEncoding] baud:baudRate];
if (serialFileDescriptor == -1) {
NSLog(#"Error opening serial port file!");
}
_fileHandle = [[NSFileHandle alloc] initWithFileDescriptor: serialFileDescriptor];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveSerialMessage:) name:NSFileHandleReadCompletionNotification object:_fileHandle];
//[_fileHandle readInBackgroundAndNotify];
// [self closeSerialPort];
}
- (int)openSerialPort:(const char *)serialPortFile baud:(speed_t)baudRate {
struct termios toptions;
int fd;
fd = open(serialPortFile, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("init_serialport: Unable to open port ");
return -1;
}
if (tcgetattr(fd, &gOriginalTTYAttrs) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
toptions = gOriginalTTYAttrs;
cfsetispeed(&toptions, baudRate);
cfsetospeed(&toptions, baudRate);
toptions.c_cflag &= PARENB;
toptions.c_cflag &= CSTOPB;
toptions.c_cflag &= CSIZE;
toptions.c_cflag |= CS8;
toptions.c_cflag &= CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL;
toptions.c_iflag &= (IXON | IXOFF | IXANY);
toptions.c_lflag &= (ICANON | ECHO | ECHOE | ISIG);
toptions.c_oflag &= OPOST;
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}
- (void)sendSerialMessage:(NSString *)message {
NSString *deviceNameString = #"/dev/tty.usbserial";
speed_t baudRate = B19200;
serialFileDescriptor = [self openSerialPort:[deviceNameString cStringUsingEncoding:NSASCIIStringEncoding] baud:baudRate];
if (serialFileDescriptor == -1) {
NSLog(#"Error opening serial port file!");
}
_fileHandle = [[NSFileHandle alloc] initWithFileDescriptor: serialFileDescriptor];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveSerialMessage:) name:NSFileHandleReadCompletionNotification object:_fileHandle];
//[_fileHandle readInBackgroundAndNotify];
// convert message string into NSData
NSString *dataString = [message stringByReplacingOccurrencesOfString:#" " withString:#""];
NSMutableData *dataToSend = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < 6; i++) {
byte_chars[0] = [dataString characterAtIndex:i*2];
byte_chars[1] = [dataString characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[dataToSend appendBytes:&whole_byte length:1];
}
// write to the serial port file
[_fileHandle writeData:dataToSend];
sleep(10);
[self closeSerialPort];
}
- (void)receiveSerialMessage:(NSNotification *)notification {
NSData* messageData = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
if ( [messageData length] == 0 ) {
[_fileHandle readInBackgroundAndNotify];
return;
}
NSString* receivedMessage = [[NSString alloc] initWithData:messageData encoding:NSASCIIStringEncoding];
[[NSNotificationCenter defaultCenter] postNotificationName:#"serialMessageReceived" object:receivedMessage];
[_fileHandle readInBackgroundAndNotify];
}
// Given the file descriptor for a serial device, close that device.
- (void)closeSerialPort {
///*
// Block until all written output has been sent from the device.
// Note that this call is simply passed on to the serial device driver.
// See tcsendbreak(3) ("man 3 tcsendbreak") for details.
if (tcdrain(serialFileDescriptor) == -1) {
NSLog(#"Error waiting for drain - %s(%d).", strerror(errno), errno);
}
// Traditionally it is good practice to reset a serial port back to
// the state in which you found it. This is why the original termios struct
// was saved.
if (tcsetattr(serialFileDescriptor, TCSANOW, &gOriginalTTYAttrs) == -1) {
NSLog(#"Error resetting tty attributes - %s(%d).\n", strerror(errno), errno);
}
//*/
close(serialFileDescriptor);
serialFileDescriptor = -1;
_fileHandle = nil;
}
- (BOOL)powerOn {
[self sendSerialMessage:#"02 00 00 00 00 02"];
// TODO: change to success/failure
return TRUE;
}
- (BOOL)powerOff {
[self sendSerialMessage:#"02 01 00 00 00 03"];
// TODO: change to success/failure
return TRUE;
}
- (BOOL)blankScreen {
[self sendSerialMessage:#"02 10 00 00 00 12"];
// TODO: change to success/failure
return TRUE;
}
- (BOOL)showScreen {
[self sendSerialMessage:#"02 11 00 00 00 13"];
// TODO: change to success/failure
return TRUE;
}
- (BOOL)requestHelp {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_helpUrl]];
// Perform request and get data back
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (nil != responseData) {
// TODO change this to check for response code is 200?
return TRUE;
/*
// get String value of response
NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// DEBUG
NSLog(#"Return string: %#", returnString);
// TODO: better way to do this?
NSString *successString = #"{\"status\":true}";
if ([returnString isEqualToString:successString]) {
// success
NSLog(#"Success!");
return TRUE;
}
else {
NSLog(#"Error bad response");
}
*/
}
else {
NSLog(#"Error: no response");
}
// return faliure!
return FALSE;
}
#end
I was wondering where I am going wrong though because when I run the program it hangs.
I've seen serious problems with Prolific's USB to serial adapter driver, particularly under Mac OS X Lion. You can try this alternative driver It's based on the osx-pl2303 open source driver code.
Personally, I would recommend getting a different USB to serial adapter. I like Keyspan's USA-19HS adapter and have never had a problem with their drivers on any version of Mac OS X. I've heard good things about FTDI too, but don't have personal experience with adapters based on that chipset.
See my earlier question for more info about this problem.

Mysterious EXC_BAD_ACCESS with no zombies or uninitialized variables

My app crashes on the following line:
sqlite3_prepare_v2(db, [sql UTF8String], -1, &pStmt, 0);
in the method of the FMDB sqlite wrapper:
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orVAList:(va_list)args {
if (![self databaseExists]) {
return 0x00;
}
if (inUse) {
[self warnInUse];
return 0x00;
}
[self setInUse:YES];
FMResultSet *rs = nil;
int rc = 0x00;
sqlite3_stmt *pStmt = 0x00;
FMStatement *statement = 0x00;
if (traceExecution && sql) {
NSLog(#"%# executeQuery: %#", self, sql);
}
if (shouldCacheStatements) {
statement = [self cachedStatementForQuery:sql];
pStmt = statement ? [statement statement] : 0x00;
}
int numberOfRetries = 0;
BOOL retry = NO;
if (!pStmt) {
do {
retry = NO;
const char *sqlStatement = [sql UTF8String];
rc = sqlite3_prepare_v2(db, sqlStatement, -1, &pStmt, 0);
if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
retry = YES;
usleep(20);
if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
NSLog(#"%s:%d Database busy (%#)", __FUNCTION__, __LINE__, [self databasePath]);
NSLog(#"Database busy");
sqlite3_finalize(pStmt);
[self setInUse:NO];
return nil;
}
}
else if (SQLITE_OK != rc) {
if (logsErrors) {
NSLog(#"DB Error: %d \"%#\"", [self lastErrorCode], [self lastErrorMessage]);
NSLog(#"DB Query: %#", sql);
#ifndef NS_BLOCK_ASSERTIONS
if (crashOnErrors) {
NSAssert2(false, #"DB Error: %d \"%#\"", [self lastErrorCode], [self lastErrorMessage]);
}
#endif
}
sqlite3_finalize(pStmt);
[self setInUse:NO];
return nil;
}
}
while (retry);
}
id obj;
int idx = 0;
int queryCount = sqlite3_bind_parameter_count(pStmt); // pointed out by Dominic Yu (thanks!)
while (idx < queryCount) {
if (arrayArgs) {
obj = [arrayArgs objectAtIndex:idx];
}
else {
obj = va_arg(args, id);
}
if (traceExecution) {
NSLog(#"obj: %#", obj);
}
idx++;
[self bindObject:obj toColumn:idx inStatement:pStmt];
}
if (idx != queryCount) {
NSLog(#"Error: the bind count is not correct for the # of variables (executeQuery)");
sqlite3_finalize(pStmt);
[self setInUse:NO];
return nil;
}
[statement retain]; // to balance the release below
if (!statement) {
statement = [[FMStatement alloc] init];
[statement setStatement:pStmt];
if (shouldCacheStatements) {
[self setCachedStatement:statement forQuery:sql];
}
}
// the statement gets closed in rs's dealloc or [rs close];
rs = [FMResultSet resultSetWithStatement:statement usingParentDatabase:self];
[rs setQuery:sql];
NSValue *openResultSet = [NSValue valueWithNonretainedObject:rs];
[openResultSets addObject:openResultSet];
statement.useCount = statement.useCount + 1;
[statement release];
[self setInUse:NO];
return rs;
}
The app crashes with EXC_BAD_ACCESS. I have tried to find out why by debugging with NSZombieEnabled and malloc_history, but it does not give me any answers. Also - the debugger tells me that the sql variable has a retain count of a very large number (which is probably because it is a static NSString) - so the EXC_BAD_ACCESS should not be because of the sql object being over-relesed.
Does anyone have any ideas on how to further debug this to find out what the problem is?
Solution: The problem was that my database was accessed by several threads. And even if all threads had synchronized access to the database handle, for sqlite versions prior to 3.3.1 (iOS uses 3.0) you can not safely use the same database handle across threads.
My solution was to create on-demand handles to the database for each thread that tries to access the database, like this:
- (ADatabaseConnection *)databaseConnection {
NSDictionary *dictionary = [[NSThread currentThread] threadDictionary];
NSString *key = #"aDatabaseConnection";
ADatabaseConnection *connection = [dictionary objectForKey:key];
if (connection == nil) {
connection = [[[ADatabaseConnection alloc] initWithDatabase:self] autorelease];
[dictionary setValue:connection forKey:key];
}
return connection;
}
Note that for sqlite versions >= 3.3.1, this is not needed as the same handle can be used across threads.
Another important thing to remember is that even if you use this approach to safely use the same database across threads, it might be wise to synchronize access to the database so that you do not access it simultaneously anyway to avoid database lock errors. I do both, use one handle for each thread and synchronize on the database.
It's not safe to use FMDatabase from multiple threads at the same time- so I've started work on a new class to help you make queries and updates from multiple threads using a pool. Right now it's on a branch, which you can view here:
https://github.com/ccgus/fmdb/tree/threadtests
Read the section titled "Using FMDatabasePool and Thread Safety."

Can't receive NSInputStream events in OCUnitTest

I'm trying to learn how to use the NSInputStream class on the iPhone using a unit test. I can get the NSStream to read data from a file using the polling method but for some reason the delegate/event method is not working.
I've posted the relevant code below. Please ignore memory leak errors and such since I'm just trying to ensure I know how to use the NSStream class in a sandboxed environment before rolling it into my larger project.
I'm wondering if maybe I'm missing something with regards to how the run loops work?
This is the logic test that creates a streamer class to read from a file.
#import "StreamingTests.h"
#import "Streamer.h"
#implementation StreamingTests
- (void) testStream {
NSLog(#"Starting stream test.");
Streamer * streamer = [[Streamer alloc] init];
streamer.usePolling = NO;
streamer.readingStream = YES;
NSThread * readThread = [[NSThread alloc] initWithTarget:streamer selector:#selector(startStreamRead:) object:nil];
[readThread start];
while(streamer.readingStream) {
[NSThread sleepForTimeInterval:0.5];
}
[readThread cancel];
}
#end
This is a simple test helper object that reads from an NSStream. When usePolling == YES it read data and outputs the appropriate NSLog messages. However, if usePolling == NO the delegate stream event function is never called.
#implementation Streamer
#synthesize readingStream, usePolling;
- (void) startStreamRead:(NSObject*) context {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(#"starting stream read.");
readingStream = YES;
/*
NSURL * url = [NSURL URLWithString:#"http://www.google.com"];
NSLog(#"Loading: %#",[url description]);
NSInputStream * inStream = [[NSInputStream alloc] initWithURL:url];
*/
NSInputStream * inStream = [[NSInputStream alloc] initWithFileAtPath:#"sample.ttc"];
if(!usePolling) {
[inStream setDelegate: self];
[inStream scheduleInRunLoop: [NSRunLoop currentRunLoop]
forMode: NSDefaultRunLoopMode];
}
[inStream open];
if(usePolling) {
while(1) {
if([inStream hasBytesAvailable]) {
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)inStream read:buf maxLength:1024];
NSLog(#"Read: %d",len);
}
NSStreamStatus status = [inStream streamStatus];
if(status != NSStreamStatusOpen && status != NSStreamStatusOpening) {
NSLog(#"Stream not open.");
break;
}
}
readingStream = NO;
NSStreamStatus status = [inStream streamStatus];
NSError * error = [inStream streamError];
NSLog(#"Status: %d Error Desc: %# Reason: %#",(int)status,[error localizedDescription], [error localizedFailureReason]);
[pool release];
}
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSMutableData * _data = nil;
NSNumber * bytesRead = nil;
NSLog(#"Event fired.");
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if(!_data) {
_data = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [(NSInputStream *)stream read:buf maxLength:1024];
if(len) {
[_data appendBytes:(const void *)buf length:len];
// bytesRead is an instance variable of type NSNumber.
//[bytesRead setIntValue:[bytesRead intValue]+len];
NSLog(#"Read %d bytes",len);
} else {
NSLog(#"no buffer!");
}
break;
}
case NSStreamEventEndEncountered:
{
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[stream release];
stream = nil; // stream is ivar, so reinit it
readingStream = NO;
break;
}
default:
{
NSLog(#"Another event occurred.");
break;
}
// continued ...
}
}
#end
Thanks in advance,
b
The reason for it should be that the run loop is blocked since the unit test is executing. You could refer to the NSRunLoop documentation where the method
runUntilDate:
might help you to run the main run loop in the thread of execution of the unit test like this:
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
This lets the run loop run for 1 second giving it time to process part of your file. It should be noted that this does not provide a reliable way for unit testing (since the time interval might differ depending on run loop size) and may then be unsuitable. By giving your unit an interface that could be used to check the status of the input stream read operation (with a reading finished state) such as
-(BOOL)hasFinishedReadingFile
the unit test could repeatedly execute the run loop until the above method returns TRUE and the file is read completely.
Addition: This question on stackoverflow also deals with the problem in a different way.