Application de mesure reseau pour iOS

De Ensiwiki
Révision de 9 juin 2011 à 12:42 par Guignous (discussion | contributions) (Integration of command lines)

Aller à : navigation, rechercher


Application of networking measurment under iOS
Project Project of speciality 2A
Sub-project Networking
Students Antoine SPITAELS (TEL)

Simon GUIGNOUARD (TEL)

Siwar KRIAA (TEL)

Promo 2012
Tutor Franck ROUSSEAU Franck.Rousseau@imag.fr


Introduction

The aim of this project is to develop an iPhone application for network performance measurement in real time enabling wireless mobility. IPMT, a tool developed in C by the team Drakkar has served as a basis. IPMT is a tool initially implemented on Unix plateforms that is composed of two programs for sending traffic respectively in TCP or in UDP and two programs for receiving data. We have to adapt the IPMT code in order to be compatible with an iOS plateform, and to build an user interface to remplace the command line interface.

From a Posix plateform to iOS

Flags insertion

The application is initially implemented in C-language and run under several OS (Solaris, FreeBSD, MacOS X and Windows/Cygwin). The first step consists on adapting this application for iOS. The language for developing applications for iPhone is Objective-C,an overlayer of C. We don't virtually have to rewrite the entire application but rather check the compatibility of all C libraries and all IPMT functions with the iOS plateform.

Using flags such as #ifdef __FreeBSD_, #ifdef __APPLE_ enables you to force the compiler to compile or not certain piece of code depending on the OS.

First of all, you will need to include the following header "TargetConditionals.h" for Apple plateforms as shown below:

#ifdef __APPLE__ 
#include "TargetConditionals.h" 
#endif

This librairy allow you to use the Apple's Flag to target a specific device (TARGET_IPHONE_SIMULATOR,TARGET_OS_IPHONE,TARGET_MAC_OS...)


For exemple:

#if !TARGET_IPHONE_SIMULATOR && !TARGET_OS_IPHONE    
   exit(1); 
#else 
   return 0; 
 #endif

It this example, the application will quit when this function is executed on Unix OS: exit(0) and the command line will be given back to the user but under iOS this code implies only the end of the function.

Management of executables

If your application is implemented on a Posix plateform then it might include many executables which are linked within a Makefile. For iPhone, only one executable is allowed. Then, to adapt your application to iOS two solutions are possible:

  • Keep several executables and use NSTask
  • Replace each main by a function while coding for iPhone. Only one main should be left.

We choose the simplest solution: using a function instead of an executable:

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
int
function(int argc,char **argv)
#else
int
main(int argc,char **argv)
#endif

Modifications under iOS

  • In order to allow relaunch of IPMT tool in iOS, we have to make some changes to the IPMT code. The first consists on resetting every variable and function used in the IPMT program. To adapt a command line tool to a graphical application we have to reset the arguments analyzer.

<< int getopt(int, char * const [], const char *) >> "getopt" is a function that analyses the arguments of the command line. The first argument "int" indicates the number of elements of the table "char*[] " containing arguments transmitted to the main(). An option is preceded by "-". "optind" is a variable which indicates the next element to be analyzed in the char*[]. The system set the value of this variable to 1. Under iOS, to be able to analyze the same table of strings many times ie each time we launch a request, we have to reset "getopt" by setting the value of "optind" to 1.

   #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
   optind=1;
   #endif  
   while ((ch = getopt(argc, argv, "abdegHhi:jM:m:r:o:p:t:vk46q")) != -1)

You can get further information on this issue on this page [1]

  • Creating threads

To enable user interaction we have to launch the IPMT function in a new thread which is liberated when we quit the application. You can get further information on thread in iOS on this page [2]

Graphical interface

Integration of command lines

Under a Posix plateform the application is launched thanks to command lines directly from a terminal. This method is not possible under iPhone because there is natively no shell or another way to access to the network layer, and especially, typing command lines with a tiny virtual keyboard is not very convenient. Therefore, the first thing to do is to provide a user-friendly graphical interface.

For beginners in Objective-C, the Interface Builder tool integrated in Xcode is quite helpful to quickly set a simple and functional GUI. Classic objects of user interface (UITextfield, UIToolbar, UIButton etc.) can be graphically manipulated. Then, objects have just to be linked to their declaration.

In our application, we retrieve arguments entered by the user thanks to different objects (UITextfield, UISegmentedControl, UISlider) that we store in a table. The following example shows how to add an option to the table of arguments.

arg=calloc(nb_param,sizeof(char*)); //Allocation of the table
       for (i=2;i<nb_param-1;i=i+2)    
           arg[i]=calloc(50,sizeof(char)); 
       arg[nb_param-1]=calloc(50,sizeof(char)); 
       i=1;
       if (![portdst.text isEqual:@""])     //if the textfield portdst is not empty
       {
           arg[i]="-p";                                //we store "-p"
           sprintf(arg[i+1],"%s",[portdst.text UTF8String]); //we store the value entered by user in the textfield portdst
           i=i+2;
       }

Finally, our table will contain the command line that the user would have entered in the terminal. (eg: tcpmt -p 13000 -d 10 10.0.2.10) Then, we just have to call the function with the number of parameters and the table as arguments. In our case, functions are coded in C, so for example we call: tcpmt(nb_param,arg);

Manipulating iPhone keyboard

When it comes to insert a text in a "UITextView" the keyboard is always on the screen which hamper the user to insert other fields below. This pushed us to add a button to the keyboard which makes the keyboard disappear.

Graphic plotting

CorePlot is a plotting framework that enables creating graphs in an iPhone application. There are many versions of CorePlot. In our case, we have chosen the recent one available on Google Code [3]. To install this framework on Xcode 4, you have to follow these instructions:

  1. Copy the CorePlotHeaders directory to your Xcode project (this file figures in iOS Binaries file).
  2. Copy the Frameworks file to your Xcode project (this file figures in Source Code).
  3. Open your apps Target Build Settings, and for Other Linker Flags include this: -ObjC -all_load
  4. Add the QuartzCore framework to the project.

This is the static method for installing the framewok. Thus, you are not supposed to re-install CorePlot each time you change the device in order to test the application. All the previous configuration will figure on you GIT deposit. Finally, you should import the following header "CorePlot-CocoaTouch.h" in the appropriate files to be able to call classes of CorePlot.