Distributed Objects, Threading, Objective-C

I have a working server / client application using distributed objects in objective-c. What I'm struggling with now makes the application multithreaded. So that users can simultaneously access the server.

Here is the main function for the server. Here I create an nsconnection object.

As I understand it, the way I should approach this is when the user tries to access the server, a new thread should be assigned for this particular call. Should [conn runInNewThread] take care of this?

Any thoughts are appreciated ...

Here is the code for the server.

int main (void) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Transactions *trans = [Transactions new]; NSConnection *conn = [NSConnection defaultConnection]; [conn setRootObject: trans]; [conn runInNewThread]; if (![conn registerName:@"holycow"]) { NSLog (@"Failed registering holycow."); exit (1); } NSLog (@"waiting for connections..."); [[NSRunLoop currentRunLoop] run]; [pool release]; return 0; } 
+4
source share
2 answers

In order to respond to client messages, the response server object must be set as the root object of an instance of the NSConnection class, and this NSConnection must be registered on the network by name.

Thus, in the case of a distributed object , an object with one server can handle several clients. or you can create more server objects and split your clients.

0
source

@ Parag Bafna was right in his answer when I conducted the test. However, I used a special kind of architecture on the server that could help with this. Take, for example, a command that takes a long time to run on the server. This can hang the server a bit and make it much slower for connections that need to be processed. Here is my solution.

  • Ask the client to call the asynchronous class method using the oneway property. Let me call it runProcess .

  • Make runProcess execute a popen() in the task and save the PID as a global variable.

  • Then use performSelectorInBackground to run the synchronous class method called readProcess in the background.

  • In readProcess I use while(fgets(buff, sizeof(buff), ghPID)!=NULL) to read the previously set popen() output (note the global variable ghPID ) and add the last line to the global variable of the last lines read. This method works as a background task, and the client is already disconnected from runProcess .

  • Now connect the client to a synchronous class method called getProcessData . Then it should take the global variable of the last lines and return it back. Since this does not take much time, the client quickly disconnects from this class method.

  • The client can then poll this data until it finds out about it. To help with this, you can create a synchronous method called isProcessRunning , which can check the global boolean variable on the server gbRunning called gbRunning and return true / false. Of course, you will have to flip this variable on the daemon server true / false in various class methods when the server is busy with the popen() task.

This way, your server daemon can respond to concurrent requests faster.

An additional tip would be to use the kill file or another mechanism (SIGHUP shared memory?), So if you are in a while loop and want to stop this process, you can simply delete the kill file somewhere in / tmp and the process will use pclose to kill it and then delete the kill file. I also do this before the start of the process, if I want only one specific process to run at a time with this server daemon.

0
source

Source: https://habr.com/ru/post/1392402/


All Articles