Commits

Juri Pakaste  committed af5aa17

Fix the REP socket stuff in the iOS app. Now it binds and sends replies. Also fixed the text view to be slightly better (interaction enabled, not editable.)

  • Participants
  • Parent commits 0525326

Comments (0)

Files changed (1)

File ZeroMQTest/ZeroMQTest/ZTAppDelegate.m

     [self appendText:[NSString stringWithFormat:@"REQ: %@", msgString]];
 }
 
+- (void)showReplyError:(NSString*)errorString
+{
+    NSParameterAssert([NSThread isMainThread]);
+    [self appendText:errorString];
+}
+
 - (void)readZmqFromSocket:(ZMQSocket*)socket
 {
+    NSLog(@"hello");
     @autoreleasepool {
         NSParameterAssert(! [NSThread isMainThread]);
         while (! [[NSThread currentThread] isCancelled])
         {
+            NSLog(@"Receiving data from %@", socket);
             NSData *msg = [socket receiveDataWithFlags:0];
             NSString *msgString = [[NSString alloc] initWithData:msg encoding:NSUTF8StringEncoding];
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self showRequestMessage:msgString]; 
             });
+            NSLog(@"sending reply");
+            NSString *reply = [NSString stringWithFormat:@"Reply from iPhone at %@", [NSDate date]];
+            NSData *replyData = [reply dataUsingEncoding:NSUTF8StringEncoding];
+            if ([socket sendData:replyData withFlags:ZMQ_NOBLOCK] == -1)
+            {
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    [self showReplyError:@"FAIL: Couldn't send reply"];
+                });
+            }
         }
     }
 }
     [ZMQContext getZMQVersionMajor:&major minor:&minor patch:&patch];
 
     self.zmqContext = [[ZMQContext alloc] initWithIOThreads:1];
-    self.readThread = [[NSThread alloc] initWithTarget:self selector:@selector(readZmqFromSocket:) object:[self.zmqContext socketWithType:ZMQ_REP]];
+    ZMQSocket *repSock = [self.zmqContext socketWithType:ZMQ_REP];
+    [repSock bindToEndpoint:@"tcp://127.0.0.1:5556"];
+    self.readThread = [[NSThread alloc] initWithTarget:self selector:@selector(readZmqFromSocket:) object:repSock];
+    [self.readThread start];
     self.reqSocket = [self.zmqContext socketWithType:ZMQ_REQ];
     // assumes you're running in simulator
     [self.reqSocket connectToEndpoint:@"tcp://127.0.0.1:5555"];
     messageViewController.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     UITextView *msgTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 350)];
     [messageViewController.view addSubview:msgTextView];
-    msgTextView.userInteractionEnabled = NO;
+    msgTextView.editable = NO;
     self.msgTextView = msgTextView;
 
     UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];