[erlang-questions] client in erlang server in objective C

Dmitry Klionsky dm.klionsky@REDACTED
Wed Jul 18 12:07:55 CEST 2012


Decided to help you a little further:

The problem is you can't just send("button", ...), because the "button" 
is not a symbol, but a NSButton's class (or something like this) instance.
I'm not sure because I've never done any MacOX programming, but iOS.
And in order to send a message to an instance you need to know it. So, 
in general, your workflow should be like this:

Erlang side:
%% create button
send
{create_instance, {string, "NSButton"}}
receive
{ok, Instance}

%% set button's title
send
{perform, {integer, Button} {string, "setTitle:"}, {string, "click me"}}
receive
ok


Objective-C side

On receiving {create_instance, {string, Clazz}}:

// note that `Class' is a reserved word
id instance = [[NSClassFromString(Clazz) alloc] init];
if (instance) {
    reply {ok, instance};
} else {
    reply {error, @"No such class"}
}

On receiving {perform, {integer, Instance}, {string, Selector}, {string, 
Object}}:
SEL sel = NSSelectorFromString(Selector);
if (sel) {
    if ([Instance respondsToSelector(sel)]) {
          [Instance performSelector:sel withObject:Object];
       reply ok;
    } else {
         reply {error, @"No such selector"};
    }
} else {
   reply {error, @"Invalid selector"}
}

This was a little messy, but hopefully you will get the idea.


On 07/18/2012 11:42 AM, Dmitry Klionsky wrote:
> Hi Joe,
>
> NSSelectorFromString, NSClassFromString and others are your friends here.
>
> For more detail have a look at:
>
> Objective-C Runtime Programming Guide
> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html 
>
>
> Objective-C Programming Language
> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html 
>
>
> Also I would also suggest you to evaluate the protobuffs. In my 
> opinion the protobuffs are more concise and clear.
> There a couple of Objective-C implementations available:
> http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers
> https://github.com/booyah/protobuf-objc
>
>
> Best regards,
> Dmitry Klionsky
>
>
> On 07/18/2012 11:10 AM, Joe Armstrong wrote:
>> That's a very interesting idea. I haven't used thrift - but if the 
>> server
>> side code exists in Objective C this would be very interesting.
>>
>> (I tried to build thrift but the build failed :-)
>>
>>
>> I've now actually got an erlang client talking to an objective C
>> server - using AsnycSocket that Bob suggested but I can only exchange 
>> strings.
>>
>> Now I need to figure out how to do build dynamic method calls in 
>> objective C.
>>
>> In objective C I'd write
>>
>>       [button setTitle:@"click me"]
>>
>> In Erlang I'd like to encode this as a string
>> send it to objective C decode it and evaluate it.
>>
>> I'd like to do something like
>>
>>      send("button", "setTitle", [{string,"click me"}])
>>
>> In erlang
>>
>>     or to encode   [foo this:123 that:@"yea"] as
>>
>>     send("foo", "this:that", [{integer,123},{string,"yea"}])
>>
>> Then I'd like to serialize this as a string (in Erlang) and decode it
>> and evaluate it in Objective C
>>
>>      Any ideas how to do this?
>>
>> /Joe
>>
>>
>> On Tue, Jul 17, 2012 at 11:48 PM, Anthony Molinaro
>> <anthonym@REDACTED>  wrote:
>>> What about thrift http://thrift.apache.org/
>>>
>>> It's RPC style so you describe function calls, erlang clients are easy,
>>> the objective-c server would be generated for you (other than the
>>> body of the function).
>>>
>>> I've been using thrift to talk java->erlang and erlang->java, so
>>> talking erlang->objective-c should be straightforward.
>>>
>>> Some examples http://wiki.apache.org/thrift/ThriftUsageObjectiveC
>>>
>>> -Anthony
>>>
>>> On Sat, Jul 14, 2012 at 06:27:49PM +0200, Joe Armstrong wrote:
>>>> I want the following:
>>>>
>>>>     - Client in erlang (easy)
>>>>    - Server in objective C (I'm thinking Mac OS-X lion - xcode 4.2 
>>>> - cocoa)
>>>>    -  socket communication
>>>>
>>>> [[ I'm a complete beginner with xcode/objective C ]]
>>>>
>>>> Has anybody written a simple objective C client that opens a port
>>>> waits for a message then executes a callback. This should be 
>>>> non-blocking
>>>> and be integrated with the "standard" dispatcher top-loop.
>>>>
>>>> I'm confused by the profusion of classes that almost do this.
>>>>
>>>> Has anybody implemented this, or have pointers to a good place to 
>>>> start.
>>>>
>>>> Cheers
>>>>
>>>> /Joe
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>> -- 
>>> ------------------------------------------------------------------------ 
>>>
>>> Anthony Molinaro<anthonym@REDACTED>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
Best regards,
Dmitry Klionsky




More information about the erlang-questions mailing list