installationservices/switestfw/test/sntpclient/sntpclient.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <bacline.h>
       
    21 
       
    22 #include "commandlineargs.h"
       
    23 #include "sntpclientengine.h"
       
    24 #include "util.h"
       
    25 
       
    26 LOCAL_C void RunSNTPClientL();
       
    27 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs);
       
    28 
       
    29 
       
    30 GLDEF_C TInt E32Main() // main function called by E32
       
    31     {
       
    32 	__UHEAP_MARK;
       
    33 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
       
    34 	TRAPD(err, RunSNTPClientL()); 
       
    35 	delete cleanup; // destroy clean-up stack
       
    36 	__UHEAP_MARKEND;
       
    37 	return err; // and return
       
    38     }
       
    39     
       
    40 
       
    41     
       
    42 LOCAL_C void RunSNTPClientL()
       
    43 	{
       
    44 	TRAP_IGNORE(Util::SetAppropriateTimezoneL());
       
    45 	
       
    46 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
    47 	CleanupStack::PushL(scheduler);
       
    48 	CActiveScheduler::Install(scheduler);
       
    49 	
       
    50 	// Parse the command line
       
    51 	
       
    52 	TCommandLineArgs args;
       
    53 	ParseCommandLineL(args);
       
    54 	
       
    55 	// Create the client object, and allow it to run to completion
       
    56 	
       
    57 	CSNTPClient* engine = CSNTPClient::NewLC(args);
       
    58 	engine->Start();
       
    59 	CActiveScheduler::Start();
       
    60 	
       
    61 	args.iServers.ResetAndDestroy();
       
    62 	
       
    63 	CActiveScheduler::Install(NULL);
       
    64 	
       
    65 	if (engine->State() == EStateFailed)
       
    66 		{
       
    67 		User::Leave(KErrGeneral);
       
    68 		}
       
    69 	else if (engine->State() == EStateAborted)
       
    70 		{
       
    71 		User::Leave(KErrTimedOut);
       
    72 		}
       
    73 		
       
    74 	CleanupStack::PopAndDestroy(2, scheduler);
       
    75 	
       
    76 	}
       
    77 	
       
    78 LOCAL_C void ParseCommandLineL(TCommandLineArgs& aArgs)
       
    79 	{
       
    80 	
       
    81 	/* Get and parse the command line arguments */
       
    82 	
       
    83 	TBool parsingOffset(EFalse);
       
    84 	TBool enoughArguments(EFalse);
       
    85 	
       
    86 	aArgs.iOffset = 0;
       
    87 	aArgs.iApplyDaylightSavings = EFalse;
       
    88 	
       
    89 	CCommandLineArguments* args = CCommandLineArguments::NewLC();
       
    90 	TInt argCount = args->Count();
       
    91 	
       
    92 	/* Ignore the first argument, this is the executable name */
       
    93 	
       
    94 	for (TInt i = 1; i < argCount; ++i)
       
    95 		{
       
    96 		
       
    97 		TPtrC arg = args->Arg(i);
       
    98 		
       
    99 		
       
   100 		if (parsingOffset)
       
   101 			{
       
   102 			
       
   103 			if (arg.Length() != 3)
       
   104 				{
       
   105 				User::Leave(KErrArgument);
       
   106 				}
       
   107 			
       
   108 			/*
       
   109 			 TO DO:
       
   110 			 	- Nicer, more liberal parsing
       
   111 			 */
       
   112 			
       
   113 			TInt temp(((arg[1] - '0') * 10) + (arg[2] - '0'));
       
   114 			
       
   115 			if (arg[0] == '-')
       
   116 				{
       
   117 				aArgs.iOffset = -temp;
       
   118 				}
       
   119 			else
       
   120 				{
       
   121 				aArgs.iOffset = temp;
       
   122 				}
       
   123 			
       
   124 			parsingOffset = EFalse;
       
   125 			
       
   126 			}
       
   127 		else if (arg.CompareF(KCommandLineDaylightSavings) == 0)
       
   128 			{
       
   129 			aArgs.iApplyDaylightSavings = ETrue;
       
   130 			}
       
   131 		else if (arg.CompareF(KCommandLineOffset) == 0)
       
   132 			{
       
   133 			parsingOffset = ETrue;
       
   134 			}
       
   135 		else
       
   136 			{
       
   137 			// This argument must be the IP address/hostname
       
   138 			// sanity check the name
       
   139 			
       
   140 			if (arg.Length() > 256)
       
   141 				{
       
   142 				User::Leave(KErrArgument);
       
   143 				}
       
   144 			
       
   145 			HBufC* server = arg.AllocLC();
       
   146 			User::LeaveIfError(aArgs.iServers.Append(server));
       
   147 			CleanupStack::Pop(server);
       
   148 			
       
   149 			enoughArguments = ETrue;
       
   150 			}
       
   151 		
       
   152 		}
       
   153 		
       
   154 	// Sanity check the arguments
       
   155 	if ((!enoughArguments) || aArgs.iOffset < -12 || aArgs.iOffset > 13)
       
   156 		{
       
   157 		User::Leave(KErrArgument);
       
   158 		}
       
   159 		
       
   160 	CleanupStack::PopAndDestroy(args);
       
   161 	
       
   162 	}