Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Request-response tutorial



This tutorial present the coding and configuration of a very system with two modules communicating through a request-response Service.

Overview

Suppose the we want to specify two modules:
  • One module received a request with an int and replies with the value multiplied by 10
  • Another module allows to set an int value, and sends a request with this value to the other butotn on a click of a button

tutorequest

Architecture

We will define only one request-response Service:
  • One service will send the request sends with int value on the click of a button
  • The other service will reply with another int value, which will be the value received with the request multiplied by 10
And two applications:
  • The first ResponseModule will not have any graphical interface and will:
    • Subscribe to the request
    • Publish the reponse with the value received with the request multiplied by 10
  • The second RequestModule will have a graphical interface and will:
    • Subscribe to the response and show the associated value
    • Show a spinner and a button and sends a request with the spinner value when the user clicks on the button

tutorequestarchi

XML configuration

Services definition

We define only one request service in a services.xml XML file:
  • The request part of the service carries the numeric value
  • The response part of the service carries another numeric value
      <services>
         <requestResponse name="request" id="1" >  
            <request>
               <data name="query" type="int" /> 
            </request>   
            <response>
               <data name="response" type="int" /> 
            </response>                   
         </requestResponse>              
      </services>

Types definition

These two services use a very simple types definition. There is only one type:
  • The int type is an int
We define this type in a types.xml XML file:
      <types>
         <simpleType name="int" baseType="int" />        
      </types>

Applications definition

We define our two applications in an applications.xml XML file:
  • The RequestAppli only contains the RequestModule. This Module:
    • sends the request request
    • subscribes to the request response
  • The ResponseAppli only contains the ResponseModule. This Module:
    • subscribes to the request request
    • reply with the request response. The associated value is the value associated with the request multiplied by 10
      <applications>
         <application name="requestAppli" id="1">
            <modules>
               <module name="RequestModule" id="1" > 
                  <interfaces>
                     <requestSend service="request" timeOut="200ms"/>
                  </interfaces>             
               </module>
            </modules>
         </application> 
         <application name="responseAppli" id="2">
            <modules>
               <module name="ResponseModule" id="1" > 
                  <interfaces>
                     <requestReceived service="request" />
                  </interfaces>             
               </module>
            </modules>
         </application>           
      </applications>
For now we did not bridge our services to any implementation for our two applications. This will be done in the next step.

Code the applications

Code the RequestModule GUI

The GUI has:
  • A spinner for the request query
  • A button for sending the request
  • A label for showing the request response value
The code has no framework specificities:
      public class RequestGUI extends JFrame {
        private JSpinner spinner = null;
        private JButton button = null;
        private JLabel label = null;
        private RequestModule module = null;

        public RequestGUI(RequestModule module) {
          super("Request GUI");
          this.module = module;
      }

        public void updateCount(int count) {
          // will be called when receiving the response
          label.setText(Integer.toString(count));
          label.invalidate();
          label.repaint();
      }

        public void setup() {
          Container pane = this.getContentPane();
          pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
          button = new JButton("Send Request");
          SpinnerNumberModel spModel = new SpinnerNumberModel(0, 0, 100, 1);
          spinner = new JSpinner(spModel);
          label = new JLabel("0");
          label.setHorizontalAlignment(JLabel.CENTER);
          pane.add(spinner);
          pane.add(Box.createHorizontalStrut(50));
          pane.add(button);
          pane.add(Box.createHorizontalStrut(50));
          pane.add(label);
          pane.add(Box.createHorizontalStrut(50));
          button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              // send the request
              module.publish(spModel.getNumber().intValue());
            }
          });
        }
      }      
The result is:
tutorequest

Code the RequestModule

We need to implement:
  • A publish(int value) method to send the request
  • A subscribe(ServiceInstance service) method to receive the response
      public class RequestModule {
        private ServiceInstance requestService = null;
        private RequestGUI gui = null;

        public void init(Module module) {
          requestService = module.getService("request");
          gui = new RequestGUI(this);
          gui.setup();
          gui.pack();
          gui.setVisible(true);
        }

        public void subscribe(ServiceInstance service) {
          int count = requestService.getData("response").getValueAsInt();
          gui.updateCount(count);
      }

        public void publish(int value) {
          requestService.setDataIntValue("query", value);
          requestService.invoke();
        }
      }      

Specify the bridge between the RequestModule and its implementation

      <applications>
         <application name="requestAppli" id="1">
            <deployment>
               <lib url="request.jar" />
            </deployment>
            <modules>
               <module name="RequestModule" id="1" > 
                  <implementation path="my.package.RequestModule" > 
                     <initEntryPoint method="init" /> 
                     <defaultReceiveEntryPoint method="subscribe" /> 
                  </implementation>             
                  <interfaces>
                     <requestSend service="request" timeOut="200ms"/>
                  </interfaces>             
               </module>
            </modules>
         </application>

Code the ResponseModule

We only need to implement:
  • A publish(int value) method to receive the request and send the response
      public class ResponseModule {
        public void init(Module module) {
          requestService = module.getProviderService("request");
        }

        public void subscribe(ServiceInstance service) {
          // get the query value
          int i = service.getData("query").getValueAsInt();
          // compute the response (in this case arbitrary equal to 10 * query value)
          service.setDataIntValue("response", i * 10);
          // invoke the service for the response
          service.invoke();
        }
      }      

Specify the bridge between the ResponseModule and its implementation

         <application name="responseAppli" id="2">
            <deployment>
               <lib url="response.jar" />
            </deployment>         
            <modules>
               <module name="ResponseModule" id="1" > 
                  <implementation path="org.da.samples.protoframework.response.ResponseModule"  > 
                     <initEntryPoint method="init" / > 
                     <defaultReceiveEntryPoint method="subscribe" / > 
                  </implementation > 
                  <interfaces>
                     <requestReceived service="request" />
                  </interfaces>             
               </module>
            </modules>
         </application>

Starting the framework

To start the framework, we must start the framework with our filelist.xml file for our configuration:
      java -jar protoframework.jar config=filelist.xml


tutorequest

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence