discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Need help with serialization/deserialization interface.


From: Vaisburd, Haim
Subject: RE: Need help with serialization/deserialization interface.
Date: Thu, 25 Jan 2007 18:27:25 -0800

I'm answering to myself, but asking another question.

I wrote before: 
> Is there something that provides a stream of bytes and gets a new
chunk behind the scenes?

It took me a while to realize that [NSInputStream -read:maxLength:] does
the same
blocking read as the read() system call does, so if I do not want
notification
events, I do not need them. Writing getc() functionality on the top of
it appeared
to be simple.

However, I could not figure out how to get event notifications related
to NSStreams
when I do want them.

Here is a complete program, it's an application since I did not know how
to properly
launch the run loop in a tool and whether I need it at all. I tried the
same with a
tool, too, and got the same result.

I assigned a delegate object to the NSInputStream and hoped to see
messages
(I wanted to properly respond to errors on opening socket streams), but
the
handler method did not get called.

Can you tell me, please, what did I do wrong?

-------------------------- begin ------------------------------
#include <AppKit/AppKit.h>

@interface StreamListener : NSObject
- (void) stream: (NSStream *)theStream handleEvent:
(NSStreamEvent)streamEvent;
@end

@implementation StreamListener
- (void) stream: (NSStream *)theStream handleEvent:
(NSStreamEvent)streamEvent
{
  // This printing never appears ------ //
  NSLog(@"NSStreamEvent=%d", streamEvent);
  sleep(2);
}
@end

@interface AppController : NSObject
- (void) applicationDidFinishLaunching: (NSNotification *)not;
@end

@implementation AppController

- (void) applicationDidFinishLaunching: (NSNotification *)not
{
    NSString * path = @"/tmp/aaa";
    NSLog( @"Setting stream from local socket %@", path );

    NSInputStream * iStream = 0;
    NSOutputStream * oStream = 0;
    [NSStream getLocalStreamsToPath: path inputStream: &iStream
              outputStream: &oStream];

    StreamListener * listener = [StreamListener new];
    [iStream setDelegate: listener];

    [iStream scheduleInRunLoop: [NSRunLoop currentRunLoop]
             forMode: NSDefaultRunLoopMode];

    [iStream open];

    char buf[10];
    int nread = [iStream read: buf maxLength: 10];
}

@end

int main (int argc, const char **argv)
{
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    
    [NSApplication sharedApplication];
    [NSApp setDelegate: [AppController new]];

    return NSApplicationMain(argc, argv);
}
--------------------- end -------------------------------

The file /tmp/aaa does not exist, so I expected to get my delegate
called with event NSStreamEventErrorOccurred.

However the application complains from somewhere else and delegate
seems to not get called:

2007-01-25 18:09:27.535 StreamAppTest[16521] Setting stream from local
socket /tmp/aaa
2007-01-25 18:09:27.536 StreamAppTest[16521] stream error: - No such
file or directory
2007-01-25 18:09:27.536 StreamAppTest[16521] stream error: - Transport
endpoint is not connected


Thank you for your help and patience,
Tima.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]