- public Future<RpcResult<Void>> makeToast(MakeToastInput input) {
- // TODO Auto-generated method stub
- log.trace("makeToast - Received input for toast");
- logToastInput(input);
- if (currentTask != null) {
- return inProgressError();
+ public Future<RpcResult<Void>> makeToast(final MakeToastInput input) {
+ LOG.info("makeToast: " + input);
+
+ synchronized (taskLock) {
+ if (currentTask != null) {
+ // return an error since we are already toasting some toast.
+ LOG.info( "Toaster is already making toast" );
+
+ RpcResult<Void> result = Rpcs.<Void> getRpcResult(false, null, Arrays.asList(
+ RpcErrors.getRpcError( "", "in-use", null, ErrorSeverity.WARNING,
+ "Toaster is busy", ErrorType.APPLICATION, null ) ) );
+ return Futures.immediateFuture(result);
+ }
+ else if( outOfBread() ) {
+ RpcResult<Void> result = Rpcs.<Void> getRpcResult(false, null, Arrays.asList(
+ RpcErrors.getRpcError( "out-of-stock", "resource-denied", null, null,
+ "Toaster is out of bread",
+ ErrorType.APPLICATION, null ) ) );
+ return Futures.immediateFuture(result);
+ }
+ else {
+ // Notice that we are moving the actual call to another thread,
+ // allowing this thread to return immediately.
+ // The MD-SAL design encourages asynchronus programming. If the
+ // caller needs to block until the call is
+ // complete then they can leverage the blocking methods on the
+ // Future interface.
+ currentTask = executor.submit(new MakeToastTask(input));
+ }