0

i will explain the best i can if u need further explanation or detail please comment..

I am having a local python server running on my computer that gives messages to client based on the request.

I have a MAC client application in which i have 3 classes -

  1. Chatlogic class- this class initialises the connection and sends and receives the messages from the server.

  2. Login class - this maintains the login of user to the application , in this class i have a instance of the Chatlogic class, i can send messages through the object of the chatlogic class like this [chatLogicObject sendmessage:something]

My problem is this = When ever i receive it comes in the chatLogic class instance and not in the LoginClass so i have a method called -(void)messageReceived in login class that should override the same method in the chatLogic class (But this does not work). How can i receive the method in the Loginclass ?

To avoid confusion i have added my chatlogic class

#import <Foundation/Foundation.h>

@interface chatLogic : NSObject <NSStreamDelegate>

@property (copy)NSOutputStream *outputStream;
@property (copy)NSInputStream *inputStream;
@property (copy)NSString *streamStatus;

-(void)initNetworkCommunications;
-(void)sendMessage:(id)sender;
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent;
-(void)messageReceived:(NSString*)message;  //i want this method to be used in some other classes


@end

The implementation file is as follows

import "chatLogic.h"

@implementation chatLogic

@synthesize inputStream ; @synthesize outputStream ; @synthesize streamStatus;

-(id)init{

if (self == [super init]) {



}
return self;

}

-(void)initNetworkCommunications{

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.22", 80, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];    

}

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent{

switch (streamEvent) {
    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        streamStatus = [[NSString alloc]initWithFormat:@"Stream opened"];
        break;
    case NSStreamEventHasBytesAvailable:
        if (aStream == inputStream) {

            uint8_t buffer[1024];
            int len;

            while ([inputStream hasBytesAvailable]) {

                len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0) {
                    NSString *output = [[NSString alloc]initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                    if (nil != output) {
                        NSLog(@"server said: %@",output);
                        [self messageReceived:output];
                    }
                }
            }
        }
    case NSStreamEventErrorOccurred:
    {
        NSLog(@"cannot connect to the host!");
        break;

    }

    case NSStreamEventEndEncountered:
    {
        break;
    }

    default:
    {   
        NSLog(@"unknown event!!");
    }
}

}

-(void)sendMessage:(id)sender{

}

-(void)messageReceived:(NSString*)message{

NSLog(@"the received message in chat logic class");

}

-(void)dealloc{

[inputStream close];
[outputStream close];
[inputStream release];
[outputStream release];
[super dealloc];

}

@end

Gaurav_soni
  • 6,064
  • 8
  • 32
  • 49

2 Answers2

1

If I understood everything correctly, you are talking about delegation here. You should define a ChatLogicDelegate protocol with a method like ( -(void)didReceiveMessage:(NSString*)message ) , add a delegate property to the ChatLogic class and when instantiating it, setting the loginClassObject to be the delegate of chatLogicObject. In the ChatLogic class, call [delegate didReceiveMessage:json] whenever you have to and the loginClassObject is going to get the message.

Apple's documentation on delegation

1

I think you want to receive same message in more than one class instances. The best way to do is using NSNotificationCentre. As soon as the message is received in one class you can post a notification which in turn will be heard by all the listeners who have registered for that notification.e

Delegation is also fine but is usually used when you are sure who will be the listeners(I am not sure what is happening in your case.). Hoping this helps.

Update:

Yes, you can send an object as well which can contain some information.. you can see how to post notification with objects using this link https://stackoverflow.com/a/4127535/919545

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • I see , can we pass some value also to some other class ? i see that notification centre can be used to just get notified to do something. – Gaurav_soni Jun 09 '12 at 15:05
  • 1
    Yes, you can send an object as well which can contain some information.. you can see how to post notification with objects using this link http://stackoverflow.com/a/4127535/919545 – Ankit Srivastava Jun 09 '12 at 15:38