47. Design a RPC Framework

Design a RPC Framework
Design an in-memory Mini RPC Framework that maintains a registry of methods per service and allows calling a registered method using serviceId, methodName, and string inputs.

Note: This is a simulation. callMethod does not execute real code; it returns an output string computed using the rules below.

RPC (Remote Procedure Call) is a way for one program to call a function/method in another program over a network as if it were a local call.
The RPC framework handles things like serialization (marshalling), networking, and response/error handling behind the scenes.

Method Signatures

void registerMethod(int serviceId, String methodName, List<String> inputParameterTypes)

Registers (or overwrites) the method identified by (serviceId, methodName) with its parameter types.
  • serviceId is between 1 and 1000.
  • methodName contains only a-z.
  • Each item in inputParameterTypes is exactly one of: "String", "Long", "Double".
  • inputParameterTypes.size() will always be between 1 and 20.
  • If registerMethod is called with same serviceId and methodName then inputParameterTypes are overwritten.

boolean unregisterMethod(int serviceId, String methodName)

Returns true if the method existed and was removed. Otherwise returns false. After removal, a method cannot be called unless registered again.

String callMethod(int serviceId, String methodName, List<String> inputValues)

It calls a method and gets back output in string format.
  • numeric input values (Long/Double) are expected to be between 1 and 100.
  • callMethod validations (in this exact order):
    1. If method not registered: return "method does not exist"
    2. If inputValues.size() != registered parameter count: return "input size mismatch"
    3. If any inputValues[i] (0-based index) cannot be parsed into its expected type: return "invalid input parameter at index " + i
  • callMethod output construction:
    For each index i (0-based), append a piece and a trailing hyphen. After preparing complete output, remove the final trailing hyphen:
    • If type is "String": append (i+1) + "-" + inputValues[i] + "-"
    • If type is "Long": parse to long v, append ((i+1) * serviceId * v) + "-"
    • If type is "Double": parse to double d, compute x = (i+1) * serviceId * 2 * d, append ((int)x) + "-"
    • When converting a computed Double to Int, use truncation (e.g. 4.67 -> 4).

Examples


registerMethod(3, "sum", ["String","Long","Double"])
callMethod(3, "sum", ["ab", "10", "4.2"])
  

Output building:
i=0 String: "1-ab-"
i=1 Long: (2*3*10)=60 => "60-"
i=2 Double: (3*3*2*4.2)=75.6, (int)=75 => "75-"
returns: 1-ab-60-75



callMethod(3, "missing", ["x"])
  

returns: method does not exist



callMethod(3, "sum", ["ab", "10"])
  

returns: input size mismatch



callMethod(3, "sum", ["ab", "notALong", "4.2"])
  

returns: invalid input parameter at index 1



unregisterMethod(3, "sum")     // returns true
callMethod(3, "sum", ["ab", "10", "4.2"])
  

returns: method does not exist



registerMethod(3, "sum", ["Long"])   // overwrites previous signature
callMethod(3, "sum", ["7"])
  

i=0 Long: (1*3*7)=21
returns: 21





Please use Laptop/Desktop or any other large screen to add/edit code.