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 -
Chatlogic class- this class initialises the connection and sends and receives the messages from the server.
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