4.5. Finished MathService Service


from pyGridWare.generated.services.mathservice.MathService import MathServiceWSRF

class MathService(MathServiceWSRF):

    def wsa_add(self, ps, address, **kw):
        request,response = MathServiceWSRF.wsa_add(self, ps, address)(1)
        ctx = self.GetResourceContext(ps, address)(2)
        ctx.properties.Value += request(3)
        ctx.properties.LastOp = "ADDITION"(4)
        return request,response

    def wsa_subtract(self, ps, address, **kw):
        request,response = MathServiceWSRF.wsa_subtract(self, ps, address)
        ctx = self.GetResourceContext(ps, address)
        ctx.properties.Value -= request
        ctx.properties.LastOp = "SUBTRACT"
        return request,response

    def wsa_getValueRP(self, ps, address, **kw):
        request,response = MathServiceWSRF.wsa_getValueRP(self, ps, address)
        ctx = self.GetResourceContext(ps, address)
        ctx.properties.LastOp = "GETVALUE"
        return request,response.__class__(ctx.properties.Value)

    # 
    # The Factory Method for our Finished MathService
    # 
    def wsa_createResource(self, ps, address, **kw):
        # Get request and response
        request,response = MathServiceWSRF.wsa_createResource(self, ps, address, **kw)

        # Create Resource Context
        from pyGridWare.utility.http import GetURLFromRequest
        ctx = self.NewResourceContextMathPort(\(5)
            GetURLFromRequest(kw['request'])(6)
        )

        # Initialize Value to 0
        counter = ctx.properties
        counter.Value = 0 (7)

        # Create EndPointReference and set in response
        from pyGridWare.addressing.AddressingUtils import AddressingUtils
        epr = AddressingUtils.createEndpointReference(ctx) (8)
        response._EndpointReference = epr (9)
        return request,response

    
(1)
Call the base class to get the response and request python objects.
(2)
Retrieve the resource context.
(3)
Use the resource context's properties attribute to get the Value resource property, add the request to it, and set it to the new value.
(4)
Use the resource context's properties attribute to set the LastOp resource property to "ADDITION".
(5)
NewResourceContextMathPort: takes a URL and create a new resource context representing the math port.
(6)
GetURLFromRequest: Utility function that retrieves the URL directly from the HTTP request kw['request']. Implication? The location of the service is abstracted from it's implementation, so the service can reply/exist at multiple locations.
(7)
Initialize the resource properties of the newly created resource context.
(8)
AddressingUtils.createEndpointReference: Utility function for creating an endpoint reference(EPR) from a resource context. The EPR is a reference to the newly created service instance.
(9)
response._EndpointReference: Set the newly created endpoint reference in the response.