Difference between revisions of "Studio:How to synchronize scripts"

From STRIDE Wiki
Jump to: navigation, search
Line 6: Line 6:
  
 
SCL File:
 
SCL File:
<nowiki>#define MAX_STRING_SIZE 50
+
#define MAX_STRING_SIZE 50
int get_time(char* szTime);
+
int get_time(char* szTime);
int shutdown(void);
+
int shutdown(void);
#ifdef _SCL
+
#ifdef _SCL
#pragma scl_function(get_time)
+
#pragma scl_function(get_time)
#pragma scl_ptr(get_time.szTime,  OUT,  PRIVATE)
+
#pragma scl_ptr(get_time.szTime,  OUT,  PRIVATE)
#pragma scl_string(get_time.szTime, MAX_STRING_SIZE)
+
#pragma scl_string(get_time.szTime, MAX_STRING_SIZE)
#pragma scl_function(shutdown)
+
#pragma scl_function(shutdown)
#endif /* _SCL */   
+
#endif /* _SCL */   
  
 
The server scripts waits for requests, gets the time and returns requests:
 
The server scripts waits for requests, gets the time and returns requests:

Revision as of 18:12, 28 February 2007

If a test scenario requires multiple scripts synchronization between the scripts is required. For example if testing a component requires to simulate a depend component it is sometimes easier to split it in multiple scripts. One script could be driving the test which launches a separate script non-blocking which simulates the depended component. The following example shows how communication between two scripts can be achieved using a simple client-server example. The client queries the server for date and time. The server returns a date and time string. The interface between the client and server is defined in a SCL file. For the example the SCL files has two interfaces, one to request information (in the example the date and time string) and one to shutdown (end) the server script. SCL File: #define MAX_STRING_SIZE 50 int get_time(char* szTime); int shutdown(void); #ifdef _SCL #pragma scl_function(get_time) #pragma scl_ptr(get_time.szTime, OUT, PRIVATE) #pragma scl_string(get_time.szTime, MAX_STRING_SIZE) #pragma scl_function(shutdown) #endif /* _SCL */ The server scripts waits for requests, gets the time and returns requests: ## Used interfaces my $req = $main::ascript->Functions->Item("get_time")->Owner; my $shutdown = $main::ascript->Functions->Item("shutdown")->Owner; ## teake ownership of required interfaces $req->Register(); $shutdown->Register(); ## initialize exit variable my $end = 0; ## Wait for requests from clients until exit while ($end == 0) { ## Wait for an event my $event = $main::ascript->WaitForEvent(); ## if the event is a get time request, get the time ## and return if ($event->Name eq $req->Name) { $req->OutPointers->{szTime} = gmctime(); $req->{ReturnValue} = 1; $req->Return(); } ## Shutdown event. Set the variable to exit the loop if ($event->Name eq $shutdown->Name) { $shutdown->{ReturnValue} = 1; $shutdown->Return(); $end = 1; } } ## Unregister ownership $req->Unregister(); $shutdown->Unregister(); The client script queries the server. However it also assures that the server is running. To show how scripts can be launched and terminated the client scripts first checks if the server is running. If not it starts the server and waits until the server is ready to accept requests. This is done by waiting until the server has registered the interfaces for script communication. At the end the client script sends a shutdown request to the server to end the server script. ## Check if server is running if (!($main::ascript->Functions->Item("shutdown")->Owner->IsRegistered)) { ## Server not available ## As an option we can start the server here ## This might not be required in all cases but is a good sample ## on how to start another script non-blocking $main::studio->Workspace->Files->Item("server.pl")->RunNonBlocking(); ## wait for the server to be up ## An easy way is to wait until the server has taken ownership ## of the supported functions while (!($main::ascript->Functions->Item("shutdown")->Owner->IsRegistered)) { $main::ascript->Sleep(100); } } ## Get a shortcut to the function my $req = $main::ascript->Functions->Item("get_time")->User; ## Call te function (blocking request to the server) $req->Call(); ## Check for the return value and display date and time if ($req->ReturnValue) { $main::ascript->MessageBox($req->OutPointers->szTime, "Current Time"); } else { $main::ascript->MessageBox("Server returned Error", "Error"); } ## Shutdown the server (if required) $main::ascript->Functions->Item("shutdown")->User->Call();