MyCalculator server source

The complete source for the server application written across the five steps of Example 1.

This module gathers the fragments shown in Step 1: Define input and output records through Step 5: Start the GWS server and process requests. Compare it against what you have written to check that nothing is missing.

To compile and run this program, go to Compiling GWS server applications and Testing the GWS service in stand-alone mode.

Complete server application

IMPORT com

# input record
PUBLIC DEFINE add_in RECORD
  a INTEGER,
  b INTEGER
END RECORD

# output record
PUBLIC DEFINE add_out RECORD
  r INTEGER
END RECORD

MAIN
  DEFINE ret INTEGER
  DEFER INTERRUPT

  # Define and register the service and its operations
  CALL createservice()

  # Start the GWS server
  CALL com.WebServiceEngine.Start()

  WHILE TRUE
    # Process each incoming request (infinite loop)
    LET ret = com.WebServiceEngine.ProcessServices(-1)
    CASE ret
      WHEN 0
        DISPLAY "Request processed."
      WHEN -1
        DISPLAY "Timeout reached."
      WHEN -2
        DISPLAY "Disconnected from application server."
        EXIT PROGRAM
      WHEN -3
        DISPLAY "Client Connection lost."
      WHEN -4
        DISPLAY "Server interrupted with Ctrl-C."
      WHEN -10
        DISPLAY "Internal server error."
        EXIT PROGRAM
      WHEN -15
        DISPLAY "Server was not started."
        EXIT PROGRAM
      OTHERWISE
        DISPLAY "ERROR: ", ret, sqlca.sqlerrm
    END CASE

    # DEFER INTERRUPT sets int_flag on Ctrl-C, so the server stops cleanly
    IF int_flag <> 0 THEN
      LET int_flag = 0
      EXIT WHILE
    END IF
  END WHILE

  DISPLAY "Server stopped"

END MAIN

FUNCTION createservice()
  DEFINE serv com.WebService   # A WebService
  DEFINE op   com.WebOperation # Operation of a WebService

  # Create the service, using the temporary namespace
  LET serv =
    com.WebService.CreateWebService("MyCalculator", "http://tempuri.org/webservices")

  # Create the Add operation in RPC style
  LET op = com.WebOperation.CreateRPCStyle("add", "Add", add_in, add_out)

  # Publish the operation to the service
  CALL serv.publishOperation(op,NULL)

  # Register the service with the GWS server
  CALL com.WebServiceEngine.RegisterService(serv)
END FUNCTION

# User public function implementing the Add operation
PUBLIC FUNCTION add()
  LET add_out.r = add_in.a + add_in.b
END FUNCTION