How to set up IPC in Swift using UNIX domain sockets - swift

I need to set up some relatively simple IPC for Mac and it needs to be done using UNIX domain sockets. I'm brand new to Swift so while I know this is possible I'm struggling even create my client and server. I have found a few useful resources for using UNIX domain sockets in other languages (Like this one: IPC using Unix Domain Sockets) but anything based in swift that I've found is more than I can follow at the moment in the brand new language. If someone could explain it like I'm 5 or at least point me in the right direction I'd appreciate it.

If you are more familiar with other languages - do it using other languages, and then incorporate into Swift.
Personally, I beleive that each language should be used to the things it doing best. Swift is not really doing best any of low-level stuff, but it could use libraries written in other languages, that are actually good in such things.
You can write the library on C or C++, working with sockets as you might find convenient, then make the Objective-C/C++ wrapper around it, then use the bridging-header to import Objective-C/C++ wrapper and use it from Swift code.
It may be a bit overhead in code size, but it would look nicer and be debuggable better, than having all this ugly Unsafe*** stuff in Swift.
For 5-y.o section:
0) Create 2 projects: Objective-C/C++ static library, Swift console app
Write some nice API in .h file into lib, like "create, read, write" functions, implement them using resources you found in C or C++.
Write some other .h file into lib with Objective-C/C++ API.
You can really keep names and arguments, just change types to be NS* (std::string or char* -> NSString* and etc), and make this looks like class. Implement passing to C/C++ part in .m/.mm relevant file.
Build the project 1, you should get an .a (static) library.
Add a Bridging Header (it is a term, you could google it) to your Swift app, import there a Objective-C/C++ header. Add the .a library to Build Phases (Link Binary with Libraries).
Use Objective-C/C++ class you created like it is Swift class for unix socket IPC.

Related

Custom Programming Language ~ How to interact with the Operating System

I am trying to create my own programming language but I am already thinking ahead a little.
Of course when I can compile a little program I won't have a Standart Library at that time,
and you'd have to create one yourself. Now take for example I'd like to add some functionality to print a string to the screen, I am pretty sure I'd have to do a few System Calls to the operating system to get this displayed.
So to the point: What would be the best way to interact with the Operating System?
Possibilities I came up with myself:
- Generate Object Files and link those to the (for example) C Standart Library
- Writing the files with embedded assembly language containing System Calls
I have a feeling there are better possibilities!
I hope you can help me,
Christian
EDIT: It's a compiled language I am creating!
You have basically two options, like you say yourself. You can link your standard library with the standard C library, so that the I/O functions in your standard library can use C functions. Alternatively you can make system calls to the operating system directly.
The second approach seems like it's going to be more work: The system calls will be different on each operating system, so you'll have to put a lot of work into porting your system. The system calls may not be well documented, causing many frustrations.
You could start by linking your standard library to the C standard library and worry about other aspects of your language for the moment. Later you can look into replacing the C functions you use with syscalls.
Stdin and stdout are pretty much the minimum - if you are using a Unix environment you can then gain access to they keyboard, command line and also pipe text in and out to files.
If you are writing a new language - then StdError may also be worth considering too!

How to Convert C++ code to Objective C Code in iphone?

I have some pure C++ codes are have I want to change the code as objective C code format. How can do this there is an any tools are there or else how can i do this.
You can compile C++ code to work on the iphone directly. Keep your core as pure C++, and write the bits that need to talk to objective-C libraries as objective-C++ ( .mm rather than .m IIRC ).
It's not possible automatically to translate C++ to Objective-C in the sense that you are asking. Picking just one of the large semantic differences at random, C++ allows multiple inheritance and Objective-C doesn't. You therefore can't map the inheritance graph of C++ onto the inheritance graph of Objective-C.
What you'd do if you were converting manually would be to switch to protocols where what you're meaning to establish is a published set of permissible communications and object composition where you wanted an object to combine some other logic. You'd probably keep the main line of inheritance in a lot of cases. However you're not going to find a tool capable of automating that process.
You can refer following question :here
and you can compile c++ code directly by changing vc.m to vc.mm

Building a simple bridge between objc and lua?

I have integrated Lua with my ObjC code (iphone game). The setup was pretty easy, but now, I have a little problem with the bridging. I have googled for results, etc... and it seems there isn't anything that could work without modifications. I mean, I have checked luaobjc bridge (it seems pretty old and dicontinued), I heard about LuaCocoa but it seems not to work on iphone, and wax is too thick.
My needs are pretty spare, I just need to be able to call objc methods from lua and don't mind having to do extra work to make it work (I don't need a totally authomatic bridging system).
So, I have decided to build a little bridge myself based on this page http://anti-alias.me/?p=36. It has key information about how to accomplish what I need, but the tutorial is not completed and I have some doubts about how to deal with method overloading when called from lua, etc...
Do anybody know if there exist any working bridge between objc and lua on the iphone or if it could be so hard to complete the bridge the above site offers?
Any information will be welcomed.
Don't reinvent the wheel!
First, you are correct that luaobjc and some other variants are outdated. A good overview can be found on the LuaCocoa page. LuaCocoa is fine but apparently doesn't support iPhone development, so the only other choice is Wax. Both LuaCocoa and Wax are runtime bridges, which means that you can (in theory) access every Objective-C class and method in Lua at the expense of runtime performance.
For games and from my experience the runtime performance overhead is so significant that it doesn't warrant the use of any runtime binding library. From a perspective of why one would use a scripting language, both libraries defy the purpose of favoring a scripting language over a lower-level language: they don't provide a DSL solution - which means you're still going to write what is essentially Objective-C code but with a slightly different syntax, no runtime debugging support, and no code editing support in Xcode. In other words: runtime Lua binding is a questionable solution at best, and has lots of cons going against it. Runtime Lua bindings are particularly unsuited for fast-paced action games aiming at a constantly high framerate.
What you want is a static binding. Static bindings at a minimum require you to declare what kind of methods will be available in Lua code. Some binding libraries scan your header files, others require you to provide a special declaration file similar to a header file. Most binding libraries can use both approaches. The benefit is optimal runtime performance, and being able to actually design what classes, methods and variables Lua scripts have access to.
There are but 3 candidates to bind Lua code to an iPhone app. To be fair, there are a lot more but most have one or more crucial flaws or are simply not stable or for special purposes only, or simply don't work for iPhone apps. The candidates are:
tolua and tolua++
luabind
SWIG
Big disadvantage shared by all Lua static binding libraries: none of them can bind directly to Objective-C code. All require to have an additional C or C++ layer available that ultimately interfaces with your Objective-C code. This has to do with how Objective-C works as a language and how small a role it has played (so far) when it comes to embedding Lua in Objective-C apps.
I recently evaluated all three binding libraries and came to enjoy SWIG. It is very well documented but has a bit of a learning curve. But I believe that learning curve is warranted because SWIG can be used to combine nearly any programming and scripting language, it can be advantageous to know how to use SWIG for future projects. Plus, once you understand their definition file implementation it turns out to be very easy (especially when compared to luabind) and considerably more flexible than tolua.
OK, bit late to the party but in case others come late also to this post here's another approach to add to the choices available: hand-code your LUA APIs.
I did a lecture on this topic where I live coded some basic LUA bindings in an hour. Its not hard. From the lecture I made a set of video tutorials that shows how to get started.
The approach of using a bindings generation tool like SWIG is a good one if you already have exactly the APIs that you need to call written in Objective-C and it makes sense to bring all those same API's over into LUA.
The pros of the hand-coding approach:
your project just compiles with one standard Xcode target
your project is all C & Obj-C
the LUA is just data shipped along with your images
no fiddling with "do I check in generated code" to Git
you create LUA functions for just the things you want
you can easily have hosted scripts that live inside an object
the API is under your control and is well known
dont expose engine APIs to level building team/tools
The last point is just that if you have detail functions that only make sense at the engine level and you don't want to see those when coding the game play you'll need to tell SWIG not to bind those.
Steffens answer is perfect and this approach is just another option, that may suit some folks better depending on the project.

The Objective-C++ language: where can I find more information about it?

I've been learning C++ for some months now and find it an excellent language albeit its perks.
I was wondering what exactly is the so-called Objective-C++ and if it's worth learning it as a main development language to target Apple environments (ie. Mac OS X, iPhone OS). Searching around the web I only found a couple of good articles: mainly Wikipedia's entry and one from Mac Dev Center.
Wikipedia says:
Objective-C++ is a front-end to the GNU Compiler Collection which can compile source files which use a combination of C++ and Objective-C syntax. Objective-C++ adds to C++ the extensions Objective-C adds to C.
The Mac Dev Center article adds:
Apple’s Objective-C compiler allows you to freely mix C++ and Objective-C code in the same source file. This Objective-C/C++ language hybrid is called Objective-C++. With it you can make use of existing C++ libraries from your Objective-C applications.
The Apple article calls Objective-C++ a language. Is this right?
I would like to know the following:
Is it possible to learn and use Objective-C++ without knowing C/Objective-C?
Are there any good books, sites, forums, etc where one can get more information and/or help?
Are there any big projects done in Objective-C++ as far as you know of?
Yes you should learn Objective-C++ when you want to develop competitive comnerical strength (aka so good that people are willing to pay for them) Apple applications.
It is indeed nothing else the Objective-C and C++ combined in the same file.
For Objective-C use any of the hunderts of the popular apple development blogs/mailing lists/newsgroups. Especially the one directly provided by Apple.
For C++ this is the same, dozens of blogs/mailing lists/starter tutorials are there. But while C and its Objective-C add-on is pretty simple and easy to learn if you already understand programming. C++ is a total different and complex beast. Get a few good books and learn it (after 10 years you will be able to fully understand the language :-) but you can writing C++ programs with just a fraction of this knowledge.
So and now the magic question why you should use C++ when you could get away with Objective-C. The answer is pretty simple. Beside the obvious mentioned wrapping of existing C++ libraries, Objective-C is slow, first of all - method call. THe usual adivse is to do something serious in your methods to avoid this runtime penality that shows up in this language.
But especially for Containers you should really consider to use C++ templates. A vector is much much faster then an NSArray. If your dataset is large you will feel the difference. Also i find C++ containers easier to use because they avoid the typecasts you have to do with Objective-C.
With slow ARM CPU's on iPad and iPhone this is not premature optimization.
Unfortunately you can't mix C++ and Cocoa classes and therefore C++ should only be used for algorithmic data. For the GUI you have to use Cocoa and Objective-C classes.
Getting the right balance between C++ and Objective-C is some part of the skills you need to develop as a Apple programmer.
Objective-C++ is really just mixing Objective-C with C++. Since it allows syntax from both you could argue that it is a new language.
I primarily use ObjC++ (.mm source files) when I have to interface Objective-C code with some C++ library. It is convenient to be able to call C++ in that case. Personally I do not know a lot of people who actually really mix C++ and Objective-C.
OBJECTIVE-C++
Objective-C is a small set of extensions to ANSI C. Objective-C++ is the same set of extensions applied to C++. Apple’s Objective-C compiler is also an Objective-C++ compiler.
One of the advantages of Objective-C is that, as a super-set of ANSI C, it can be easily mixed with the millions of lines of existing C code in the world. Objective-C++ can be mixed with the millions of lines of C++ code that already exist. C++ features, such as name mangling, are fully supported by Objective-C++ so that direct linkage between Objective-C++ code and existing C++ code is possible.
Objective-C source code files are identified by the .m extension. Apple’s compiler treats files with the .M or .mm extensions as Objective-C++ source code. Additionally, the –x compiler option can be used to instruct Apple’s compiler to treat any input file as Objective-C++ source code.
Apple’s online documentation describes the features and limitations of Objective-C++ at http://developer.apple.com/techpubs/, and in the release notes that come with Apple’s developer tools. In general, Objective-C classes and C++ classes can be intermixed so that an Objective-C method can call a C++ member function and visa versa or a C++ class can include a pointer to an Objective-C object as a member variable. Objective-C classes cannot inherit from C++ classes or the other way around. The two class hierarchies must remain distinct. The semantics regarding instance creation and deletion are dramatically different between C++ and Objective-C. As a result, mixing them can be tricky, but the benefit of reusing existing C++ code in new Objective-C projects outweighs the complications that it introduces.
Some more practical points about Objective-C++
Gcc is at once a compiler for C, Objective-C, and C++. You can intermix C++ and Objective-C code to some degree. To instruct the compiler that a file contains C++ code as well as Objective-C, use the file extension .mm or .M instead of .m.
Following are some ways in which C++ and Objective-C code can be used together:
Objective-C objects can have fields that point to C++ objects, and vice versa.
Objective-C code can call methods on C++ objects, and vice versa.
Objective-C objects can have C++ objects (as opposed to pointers) as fields, but only if the C++ class has no virtual methods.
However, Objective-C and C++ are not completely compatible. Here are some things you can't do:
Objective-C classes can't inherit from C++ classes, and vice versa.
You can't declare Objective-C classes in C++ namespaces or templates, or vice versa.
You can't use C++ keywords for Objective-C variable names.
You can't call Objective-C methods with C++ syntax, or vice versa.
Finally, there are some restrictions that are imposed to avoid ambiguity:
You can't use the name id as a C++ template name. If you could, the declaration id< TypeName > var could be either a C++ template declaration or an Objective-C declaration using a protocol.
If you are passing a globally-scoped C++ variable to an Objective-C method, you need a space between the first and second colons.
You might find these link helpful :-
https://www.raywenderlich.com/62989/introduction-c-ios-developers-part-1
https://www.raywenderlich.com/62990/introduction-c-ios-developers-part-2
https://www.sitepoint.com/using-c-and-c-in-an-ios-app-with-objective-c/
https://github.com/sitepoint-editors/HelloCpp
1) i doubt it, because everyone of them is a superset of the C language.
i don't consider objective-c++ a totally new language. it is just a mix of both that gives you some additional possibilities - i.e. reuse existing c++ code, or use faster c++ in more time critical code sections, or use c++ code where no objective-c / cocoa interface is exposed (mainly hardware related lowlevel stuff like serial ports / ioctls, opengl, ...)
2) i think resources regarding this topics a
3) pass ... i mean what do you consider big. i just know from a bunch of projects (maybe not all of them commercial in the sense that you can buy the software in the store) that use this mix for the reasons listed in 1.
Plenty of large projects use some Objective-C++. Camino, Chromium, and Firefox, for example, are all substantial open-source projects that include Objective-C++.

Suggestions for Adding Plugin Capability?

Is there a general procedure for programming extensibility capability into your code?
I am wondering what the general procedure is for adding extension-type capability to a system you are writing so that functionality can be extended through some kind of plugin API rather than having to modify the core code of a system.
Do such things tend to be dependent on the language the system was written in, or is there a general method for allowing for this?
I've used event-based APIs for plugins in the past. You can insert hooks for plugins by dispatching events and providing access to the application state.
For example, if you were writing a blogging application, you might want to raise an event just before a new post is saved to the database, and provide the post HTML to the plugin to alter as needed.
This is generally something that you'll have to expose yourself, so yes, it will be dependent on the language your system is written in (though often it's possible to write wrappers for other languages as well).
If, for example, you had a program written in C, for Windows, plugins would be written for your program as DLLs. At runtime, you would manually load these DLLs, and expose some interface to them. For example, the DLLs might expose a gimme_the_interface() function which could accept a structure filled with function pointers. These function pointers would allow the DLL to make calls, register callbacks, etc.
If you were in C++, you would use the DLL system, except you would probably pass an object pointer instead of a struct, and the object would implement an interface which provided functionality (accomplishing the same thing as the struct, but less ugly). For Java, you would load class files on-demand instead of DLLs, but the basic idea would be the same.
In all cases, you'll need to define a standard interface between your code and the plugins, so that you can initialize the plugins, and so the plugins can interact with you.
P.S. If you'd like to see a good example of a C++ plugin system, check out the foobar2000 SDK. I haven't used it in quite a while, but it used to be really well done. I assume it still is.
I'm tempted to point you to the Design Patterns book for this generic question :p
Seriously, I think the answer is no. You can't write extensible code by default, it will be both hard to write/extend and awfully inefficient (Mozilla started with the idea of being very extensible, used XPCOM everywhere, and now they realized it was a mistake and started to remove it where it doesn't make sense).
what makes sense to do is to identify the pieces of your system that can be meaningfully extended and support a proper API for these cases (e.g. language support plug-ins in an editor). You'd use the relevant patterns, but the specific implementation depends on your platform/language choice.
IMO, it also helps to use a dynamic language - makes it possible to tweak the core code at run time (when absolutely necessary). I appreciated that Mozilla's extensibility works that way when writing Firefox extensions.
I think there are two aspects to your question:
The design of the system to be extendable (the design patterns, inversion of control and other architectural aspects) (http://www.martinfowler.com/articles/injection.html). And, at least to me, yes these patterns/techniques are platform/language independent and can be seen as a "general procedure".
Now, their implementation is language and platform dependend (for example in C/C++ you have the dynamic library stuff, etc.)
Several 'frameworks' have been developed to give you a programming environment that provides you pluggability/extensibility but as some other people mention, don't get too crazy making everything pluggable.
In the Java world a good specification to look is OSGi (http://en.wikipedia.org/wiki/OSGi) with several implementations the best one IMHO being Equinox (http://www.eclipse.org/equinox/)
Find out what minimum requrements you want to put on a plugin writer. Then make one or more Interfaces that the writer must implement for your code to know when and where to execute the code.
Make an API the writer can use to access some of the functionality in your code.
You could also make a base class the writer must inherit. This will make wiring up the API easier. Then use some kind of reflection to scan a directory, and load the classes you find that matches your requirements.
Some people also make a scripting language for their system, or implements an interpreter for a subset of an existing language. This is also a possible route to go.
Bottom line is: When you get the code to load, only your imagination should be able to stop you.
Good luck.
If you are using a compiled language such as C or C++, it may be a good idea to look at plugin support via scripting languages. Both Python and Lua are excellent languages that are used to script a large number of applications (Civ4 and blender use Python, Supreme Commander uses Lua, etc).
If you are using C++, check out the boost python library. Otherwise, python ships with headers that can be used in C, and does a fairly good job documenting the C/python API. The documentation seemed less complete for Lua, but I may not have been looking hard enough. Either way, you can offer a fairly solid scripting platform without a terrible amount of work. It still isn't trivial, but it provides you with a very good base to work from.