anchor.diff: +%%
  +%% This is a 'ranch' managed worker. When a socket client
  +%% connects to the server, the socket is passed here and
  +%% we manage the overall communication.
  +%%
  +%% When the client first connects, we challenge for a valid
  +%% message containing the robot's name. If we get a valid-
  +%% looking message, we put a 'registration|robot_name' message
  +%% into the 'registration' tube of beanstalk for processing,
  +%% then we drop into loop that watches the 'robot_name_commands'
  +%% tube and forward them onto the connected socket client.
  +%%
   -module(robot_command_protocol).
   -behavior(ranch_protocol).
   
  
positive.code: 
  loop(command_wait, Socket, Transport, Q, Log, TubeName) ->
    { watching, _ } = beanstalk:watch(Q, io_lib:format("~s_commands", [ TubeName ])),
    case beanstalk:reserve(Q) of
      { reserved, JobId, BMessage } ->
        send_message(Socket, Transport, binary:bin_to_list(BMessage)),
        { deleted } = beanstalk:delete(Q, JobId),
        loop(command_wait, Socket, Transport, Q, Log, TubeName);
      Error ->
        syslog:log(Log, err, "Error reserving a job: ~p",  [ Error ])
    end.
  
  
negative.code:   send_messages(Socket, Transport, Messages).
  
  send_message(Socket, Transport, Message) ->
    case Transport:send(Socket, binary:encode_unsigned(length(Message))) of
      ok ->
        Transport:send(Socket, Message);
      Error ->
        Error
    end.
  