Coding for VTS – Part 3
Posted on Jul, 2018 by Tony Gartrell
A Step by Step guide to using the New HPE Virtual Table Server (VTS)
Introduction to the New HPE Virtual Table Server (VTS) – Part 1Install and Setting up VTS – Part 2
Coding for VTS – Part 3
Advanced Topics in VTS – Part 4
NOTE: You can have multiple instances of VTS running and being used during a test. These code samples are assuming you are using a single instance of VTS. If you are using multiple instances, you’ll want to use the vtc functions and NOT the lrvtc functions! Instances are covered in Advanced Topics of VTS.
Connecting
The first thing you need to do is connect to the server from your script. It is not required, but highly recommended, that you should set some global constants for the arguments for the connection string. I also recommend you add the connect statement in the vuser_init.
char *VtsServer = "testserver"; //Server name or IP, don’t use localhost. int nPort = 8888; //Port used when enabling the instance. int rc = 0; // Return code, to make sure there were no errors. vuser_init() { rc = lrvtc_connect(VtsServer, nPort, VTOPT_KEEP_ALIVE); //Connects to a single instance of VTS and keeps it open until you disconnect lr_log_message("rc=%d\n", rc); //Output the return code to the log return 0; }
Now you’ll want to work with adding and removing data.
Sending Data
There are three functions you can use to send data to the VTS table:
lrvtc_send_message ( char *columnName, char *value ); – This function sends a value to a single column.
lrvtc_send_if_unique ( char *columnName, char *value ); – This function sends a value to a single column, but makes sure the value is unique. Should only be used on indexed columns.
lrvtc_send_row1 ( char *columnNames, char *values, char *delimiter, unsigned char sendflag ); This function sends a “row” of data. You don’t have to fill in the whole row; this is not like a database, you can just fill in the columns you want. The column names have to be listed out with delimiters. The sendflag has three options:
- VTSEND_SAME_ROW – This will help to keep row integrity in place and allow you to have a relationship between several columns
- VTSEND_STACKED – This will send data to the end of each column regardless of the number of rows. This is used if row integrity isn’t needed
- VTSEND_STACKED_UNIQUE – Same as stacked, just makes sure each value is unique to the column
NOTE: You don’t have to use lr_eval_string to send a value. It will evaluate any parameters in the value argument.
Examples:
//This sends the c_po parameter to the v_ponumber column in the table. rc = lrvtc_send_message(“v_ponumber”, “{c_po}”); //This sends the c_ss parameter to v_ssnumber and makes sure it is unique. If it isn’t unique the value isn’t added. rc = lrvtc_send_if_unique (“v_ssnumber”, “{c_ss}”); // This sends three values, c_firstname, c_lastname, and c_address1 to the corresponding column. “;” is used as the delimiter in both the column names and values section. rc = lrvtc_send_row1(“v_firstname;v_lastname;v_address1”, “{c_firstname}; {c_lastname};{c_address1}”, “;”, VTSEND_SAME_ROW);
NOTE – If the column name doesn’t exist, VTS will create the column for you.
Retrieving Data
Similar to adding data there are options to retrieve data. It is very important to note that retrieving data removes the top row of the columns.
lrvtc_retrieve_message ( char *columnName ); – This function retrieves the first row in that column and saves it to a parameter with the same name as the column.
lrvtc_retrieve_message1 ( char *columnNames, char *delimiter ); – The function retrieves the first row of each column listed, separated by the delimiter, and saves it to parameters based on the names of each column.
Examples:
//This retrieves the first row in the v_ponumber table and removes it from the table. The value is then saved to the v_ponumber parameter. rc = lrvtc_retrieve_message(“v_ponumber”); // This retrieves three values, v_firstname, v_lastname, and v_address1 from the corresponding columns and removes them from the table. These are then saved as parameters; v_firstname, v_lastname, and v_address1. rc = lrvtc_retrieve_message1(“v_firstname;v_lastname;v_address1”, “;”);
NOTE – If you have columns that need to maintain row integrity, be sure to not retrieve any data from those columns separately and to always retrieve them with message1.
Advanced functions
For the most part you’ll only need to use the send and retrieve functions. There are many other functions that allow you to query and update data, along with table functions that allow you to create new columns, add indexes, and drop tables. These are for advanced uses and typically not used very often.
Some of them will be covered in future articles.
Disconnecting
Finally, it is recommended that you disconnect from the VTS server when the script finishes. The function should be added to the vuser_end to make sure it is executed when the script stops. Below is an example or the vuser_end:
vuser_end() { rc = lrvtc_disconnect(); //disconnect the active VTC connection lr_log_message("Disconnect result rc=%d\n", rc); //Output the return code to the log. return 0; }
Sample script
int rc = 0; char *VtsServer = "testserver"; int nPort = 8888; Action() { rc = lrvtc_connect(VtsServer,nPort,VTOPT_KEEP_ALIVE); lr_log_message("rc=%d\n", rc); lr_save_string("123456789", "c_test"); //Save 123456789 to the c_test parameter rc = lrvtc_send_if_unique("v_test", "{c_test}"); lr_log_message("Send if unique rc=%d\n", rc); rc = lrvtc_retrieve_message("v_test"); lr_log_message("Retrieve_message rc=%d\n", rc); lr_log_message("Retrieved value is: %s", lr_eval_string("{v_test}")); lrvtc_disconnect(); return 0; }
Next Up:
Advance Topics in VTS – Part 4