= Building and Running Client Scripts = Here we will build and run a client script. To see a complete example refer to [wiki:WARPnet/Example the example script]. == Building the Client Script == === Synchronizing Structs === There are two files that the user control over as the build their custom scripts to control the boards. The first is `warpnet_client_experiment_structs.py`. This file defines the data structures that are sent back and forth between the FPGA design and the client script. The FPGA design and this file must be in sync for data to be accurately transferred between the two. Let us look at an example to understand how this file is built. The following is the ControlStruct that is used to set basic node parameters. First we see the C struct that is stored in the FPGA. {{{ #!c #define STRUCTID_CONTROL 0x13 #define STRUCTID_CONTROL_ACK 0x14 typedef struct { char structID; char nodeID; char txPower; char channel; char modOrderHeader; char modOrderPayload; short reserved; int pktGen_period; int pktGen_length; int pktGen_numPackets; } warpnetControl; typedef struct { char structID; char nodeID; short cmdID; } warpnetAck; }}} The next is the ControlStruct as seen in `warpnet_client_experiment_structs.py`. {{{ #!python STRUCTID_CONTROL = 0x13 STRUCTID_CONTROL_ACK = 0x14 class ControlStruct(ClientStruct): txPower = -1 channel = -1 modOrderHeader = -1 modOrderPayload = -1 reserved = 0 packetGeneratorPeriod = 0 packetGeneratorLength = 0 packetGeneratorNumPackets = 0 def __init__(self, logger=None): ClientStruct.__init__(self, logger) self.structID = STRUCTID_CONTROL self.expectedReturnStructID = STRUCTID_CONTROL_ACK self.txPower = 63 self.channel = 4 self.modOrderHeader = 0 self.modOrderPayload = 2 self.packetGeneratorPeriod = 10 self.packetGeneratorLength = 1300 self.packetGeneratorNumPackets = 1000 def prepToSend(self, nodeID): self.updateDone = False return struct.pack('!6BH3I', self.structID, nodeID, self.txPower, self.channel, self.modOrderHeader, \ self.modOrderPayload, self.reserved, self.packetGeneratorPeriod, self.packetGeneratorLength, self.packetGeneratorNumPackets) def updateFromNode(self, rawData, pcapts): dataTuple = struct.unpack('!BBH', rawData[0:4]) }}} The `prepToSend` function creates the raw data that is seen by `warpnet.c` in the FPGA. It creates a byte packed data of 6 bytes (6B), 1 short (H) and 3 ints (3I). This is exactly what the node is expecting. A appropriate response from the node is a `warpnetACK` which `updateFromNode` parses in the python file. For a struct that is requesting data the raw data sent would be a `warpnetRequest` struct and expect a response of the appropriate type. === Logging Data === As statistics are received by the client, there is good chance that the data would want to be written to a file. There is a built in method to do this. In the top-level script create a `DataLogger` object like the following. {{{ dataLog = DataLogger('filename.txt', fileRefreshTime) }}} When instantiating a struct pass the `dataLog` object to the it as follows: {{{ statStruct = StatisticsStruct(dataLog) }}} In the StatisticsStruct any received data in `updateFromNode` can be written to the this log file using {{{ self.logData('Data0=%d, Data1=%d" %(dataTuple[0], dataTuple[1])) }}} == Running the Client Script == 1. Once the client script has been written, navigate to the folder where it is located. 1. Run the following: {{{ python experiment.py }}}