Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to create a new item for a rosjava service list

I'm using the git repositories pulled with wstool as described at:

               http://wiki.ros.org/rosjava/Tutorials/indigo/Source%20Installation

I added a new msg, rosjava/src/rosjava_test_msgs/msg/PhoneContact.msg

string last_name
string first_name  
string phone_number

I also added a new srv, SendContactsSmsMsg.srv

PhoneContact      contact0     
PhoneContact      contact1     
PhoneContact      contact2     
PhoneContact      contact3     
PhoneContact      contact4     
PhoneContact[]    contact_list
string            message   
---   
bool              status

I added a new test client called SendContactsSmsMsgClient.java to:

   ~/rosjava/src/rosjava_core/rosjava_tutorial_services/src/main/java/org/ros/rosjava_tutorial_services

I've Cut-N-Pasted a copy below and would like to discuss it.

I'd like to remove the contact1...contac4 above. I added them because I'm having difficulty creating a PhoneContact to add to the contact_list above.

The genjava git repo seems to be generating only interface files which can't create an instance. For example:

Line 116 below rosjava_test_msgs.PhoneContact contact = new PhoneContact();

doesn't work because the public definition is only an interface. I tried implementing my on Phone Contact (Lines 41...79) below but I'm getting problems with the Raw Message class not being public.

Seems there should be a simple solution to this problem. My HACK is to get instances from the request message, modify them, and add them to the list (See lines 123...140).

I'm just sending a list of phone contacts with a message and sending the msg via SMS messaging interfaces available in Android. This test program just send the RPC and decodes the RPC. I'd image that creating a RPC with a list/array has been done a long time ago and it's trivial to add a instance of my phone_contact msg.

What's the trick?

     1  /*
     2   * Copyright (C) 2011 Google Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License. You may obtain a copy of
     6   * the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations under
    14   * the License.
    15   */
    16  
    17  package org.ros.rosjava_tutorial_services;
    18  
    19  import org.ros.exception.RemoteException;
    20  import org.ros.exception.RosRuntimeException;
    21  import org.ros.exception.ServiceNotFoundException;
    22  import org.ros.namespace.GraphName;
    23  import org.ros.node.AbstractNodeMain;
    24  import org.ros.node.ConnectedNode;
    25  import org.ros.node.NodeMain;
    26  import org.ros.node.service.ServiceClient;
    27  import org.ros.node.service.ServiceResponseListener;
    28  
    29  import org.jboss.netty.buffer.ChannelBuffer;
    30  import org.ros.exception.RosMessageRuntimeException;
    31  import org.ros.internal.message.context.MessageContext;
    32  import org.ros.internal.message.field.Field;
    33  import org.ros.internal.message.field.MessageFieldType;
    34  import org.ros.internal.message.field.MessageFields;
    35  import org.ros.message.Duration;
    36  import org.ros.message.MessageIdentifier;
    37  import org.ros.message.Time;
    38  import org.ros.internal.message.RawMessage;
    39  import org.ros.internal.message.*;
    40  
    41  class MyPhoneContact
    42      implements rosjava_test_msgs.PhoneContact {
    43      String LastName;
    44      String FirstName;
    45      String PhoneNumber;
    46  
    47      @Override
    48      public String getPhoneNumber() {
    49      return PhoneNumber;
    50      }
    51      @Override
    52      public String getFirstName() {
    53      return FirstName;
    54      }
    55  
    56      @Override
    57      public String getLastName() {
    58      return LastName;
    59  
    60      }
    61  
    62      @Override
    63      public void setPhoneNumber(java.lang.String value) {
    64      PhoneNumber = value;
    65      }
    66      @Override
    67      public void setFirstName(java.lang.String value) {
    68      FirstName = value;
    69      }
    70      @Override
    71      public void setLastName(java.lang.String value) {
    72      FirstName = value;
    73      }
    74      @Override
    75      public RawMessage toRawMessage() {
    76      return (RawMessage) this;       /* From src/rosjava_bootstrap/message_generation/src/main/java/org/ros/internal/message/MessageImpl.java */
    77      }
    78  
    79  }
    80  
    81  
    82  /**
    83   * A simple {@link ServiceClient} {@link NodeMain}.
    84   *
    85   * @author damonkohler@google.com (Damon Kohler)
    86   */
    87  public class SendContactsSmsMsgClient extends AbstractNodeMain {
    88  
    89    @Override
    90    public GraphName getDefaultNodeName() {
    91      return GraphName.of("rosjava_tutorial_services/client");
    92    }
    93  
    94    @Override
    95    public void onStart(final ConnectedNode connectedNode) {
    96      ServiceClient<rosjava_test_msgs.SendContactsSmsMsgRequest, rosjava_test_msgs.SendContactsSmsMsgResponse> serviceClient;
    97      try {
    98        serviceClient = connectedNode.newServiceClient("send_contacts_sms_msg", rosjava_test_msgs.SendContactsSmsMsg._TYPE);
    99      } catch (ServiceNotFoundException e) {
   100        throw new RosRuntimeException(e);
   101      }
   102      final rosjava_test_msgs.SendContactsSmsMsgRequest request = serviceClient.newMessage();
   103      java.util.List<rosjava_test_msgs.PhoneContact> contact_list = request.getContactList();
   104  
   105      // How to create a contact with a new decl?
   106      // rosjava_test_msgs.Contact contact;
   107      // MyPhoneContact contact = new MyPhoneContact();
   108  
   109      // rosjava_test_msgs.Contact contact = new rosjava_test_msgs.Contact();
   110  
   111      // java.util.List<rosjava_test_msgs.Contact>contacts = request.getContacts();
   112      // Sending a null phone number crashes android apk.
   113      for (int i = 0; i < 3; i++) {
   114      //Contact is abstract; cannot be instantiated
   115      // rosjava_test_msgs.Contact contact = contacts.get(1); IndexOutOfBoundsException: Index: 1, Size: 0
   116      rosjava_test_msgs.PhoneContact contact = new MyPhoneContact();
   117  
   118          connectedNode.getLog().info("onStart: i:" + i + "[info]");
   119          /// org.ros.internal.message.Message.Contact = new org.ros.internal.message.Message.Contact();
   120      // System.out.println("Have a contact:" + contact);
   121  
   122      switch (i) {                            /* Ting Numbers */
   123          case 0: // contact = request.getContact0();
   124                      contact.setPhoneNumber("+1 (669) 241-8418");    /* Yellow Phone */
   125                      contact.setFirstName("Piet");
   126                      break;
   127  
   128          case 1: // contact = request.getContact1();
   129                      contact.setPhoneNumber("+1 (408) 646-8557");    /* White Phone */
   130                      contact.setFirstName("Pete");
   131              break;
   132  
   133          case 2: // contact = request.getContact2();
   134                      contact.setPhoneNumber("+1 (408) 544-0065");    /* Intel Phone */
   135                      contact.setFirstName("Peter");
   136              break;
   137  
   138          default:
   139              // contact = request.getContact3();
   140                      contact.setPhoneNumber("<<void>>");
   141      }
   142          contact.setLastName("Delaney");
   143  
   144          System.out.println("onStart: Adding contact i:" + i + ": FirstName:'" + contact.getFirstName() + "', PhoneNumber: " + contact.getPhoneNumber() );
   145  
   146      contact_list.add(i, contact);       /* NOTE: Links in current structure. We need unique memory allocations. */
   147      }
   148      // request.setContactList(contacts);
   149  
   150      request.setMessage("Hi Piet Delaney");
   151  
   152      serviceClient.call(request, new ServiceResponseListener<rosjava_test_msgs.SendContactsSmsMsgResponse>() {
   153        @Override
   154        public void onSuccess(rosjava_test_msgs.SendContactsSmsMsgResponse response) {
   155      // java.util.List<rosjava_test_msgs.Contact>contacts = request.getContacts();
   156      // rosjava_test_msgs.Contact contacts = contact[0];
   157          java.util.List<rosjava_test_msgs.PhoneContact> contact_list = request.getContactList();
   158          rosjava_test_msgs.PhoneContact   contact;
   159  
   160          connectedNode.getLog().info(
   161          String.format("Sent Message:'%s' to ROSS SMS Info RPC Server which returnd status: '%s'.",
   162                            request.getMessage(), response.getStatus() )
   163      );
   164  
   165          for (int i = 0; i < 4; i++)  {
   166          contact = contact_list.get(i);      /* Getting contacts from List */
   167  
   168              connectedNode.getLog().info(
   169                  String.format("PhoneNumber:'%s', Name: '%s' '%s', Message: '%s' ",
   170                           contact.getPhoneNumber(), contact.getFirstName(), contact.getLastName(), request.getMessage() )
   171              );
   172           }
   173        }
   174  
   175        @Override
   176        public void onFailure(RemoteException e) {
   177          throw new RosRuntimeException(e);
   178        }
   179      });
   180    }
   181  }