RobotDescription.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above
00013  *    copyright notice, this list of conditions and the following
00014  *    disclaimer in the documentation and/or other materials provided
00015  *    with the distribution.
00016  *  * Neither the name of Willow Garage, Inc. nor the names of its
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *   
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00034 package com.github.rosjava.android_apps.application_management;
00035 
00036 import org.jboss.netty.buffer.ChannelBuffer;
00037 import org.jboss.netty.buffer.ChannelBuffers;
00038 
00039 import java.util.Date;
00040 import java.util.regex.Pattern;
00041 import java.util.regex.Matcher;
00042 import rocon_app_manager_msgs.Icon;
00043 
00044 public class RobotDescription implements java.io.Serializable {
00045     public static final String CONNECTING = "connecting...";
00046     public static final String OK = "ok";
00047     public static final String ERROR = "exception";
00048     public static final String WIFI = "invalid wifi";
00049     public static final String UNAVAILABLE = "unavailable";  // when the robot app manager is busy serving another remote controller
00050     public static final String CONTROL = "not started";
00051     private static final String NAME_UNKNOWN = "Unknown";
00052     private static final String TYPE_UNKNOWN = "Unknown";
00053     private static final long serialVersionUID = 1L;
00054     private RobotId robotId;
00055     private String robotName;
00056     private String robotType;
00057     private String gatewayName; // unique id used as the signature of the remote app manager (used to check that the remote controller is us).
00058     // Icon stored piecemeal because msg arrays (stored as jboss ChannelBuffers) can't
00059     // be dumped and reloaded by the snakeyaml library.
00060     private String robotIconFormat;
00061     private byte[] robotIconData;
00062     private int robotIconDataOffset;
00063     private int robotIconDataLength;
00064 
00065     private String connectionStatus;
00066     private Date timeLastSeen;
00067     // Unique identifier for keys passed between android apps.
00068     public static final String UNIQUE_KEY = "com.github.rosjava.android_apps.application_management.RobotDescription";
00069 
00070     // TODO(kwc): add in canonicalization of robotName
00071     public RobotDescription() {
00072     }
00073 
00074     public RobotDescription(RobotId robotId, String robotName, String robotType, Icon robotIcon, String gatewayName, Date timeLastSeen) {
00075             setRobotName(robotName);
00076             setRobotId(robotId);
00077             this.robotName = robotName;
00078             this.robotType = robotType;
00079             this.gatewayName = gatewayName;
00080             if ( robotIcon != null ) {
00081                 this.robotIconFormat = robotIcon.getFormat();
00082                 this.robotIconData = robotIcon.getData().array();
00083                 this.robotIconDataOffset = robotIcon.getData().arrayOffset();
00084                 this.robotIconDataLength = robotIcon.getData().readableBytes();
00085             }
00086             this.timeLastSeen = timeLastSeen;
00087     }
00088     public void copyFrom(RobotDescription other) {
00089             robotId = other.robotId;
00090             robotName = other.robotName;
00091             robotType = other.robotType;
00092             gatewayName = other.gatewayName;
00093             robotIconFormat = other.robotIconFormat;
00094             robotIconData = other.robotIconData;
00095             robotIconDataOffset = other.robotIconDataOffset;
00096             robotIconDataLength = other.robotIconDataLength;
00097             connectionStatus = other.connectionStatus;
00098             timeLastSeen = other.timeLastSeen;
00099     }
00100     public RobotId getRobotId() {
00101             return robotId;
00102     }
00103 
00104     public String getGatewayName() { return gatewayName; }
00105 
00110     public String getMasterUri() {
00111         return robotId.getMasterUri();
00112     }
00113 
00114     public void setRobotId(RobotId robotId) {
00115             // TODO: ensure the robot id is sane.
00116 //              if(false) {
00117 //                      throw new InvalidRobotDescriptionException("Empty Master URI");
00118 //              }
00119             // TODO: validate
00120             this.robotId = robotId;
00121     }
00122     public String getRobotName() {
00123             return robotName;
00124     }
00136     public String getRobotFriendlyName() {
00137         String friendlyName = robotName;
00138         // The uuid is a 16 byte hash in hex format = 32 characters
00139         if (robotName.length() > 32) {
00140             String possibleUuidPart = robotName.substring(robotName.length() - 32);
00141             Pattern p = Pattern.compile("[^a-f0-9]");
00142             Matcher m = p.matcher(possibleUuidPart);
00143             if (!m.find()) {
00144                 friendlyName = robotName.substring(0, robotName.length() - 32);
00145             }
00146         }
00147         friendlyName = friendlyName.replace('_',' ');
00148         final StringBuilder result = new StringBuilder(friendlyName.length());
00149         String[] words = friendlyName.split("\\s");
00150         for(int i=0, l=words.length; i<l; ++i) {
00151           if(i > 0) result.append(" ");
00152           result.append(Character.toUpperCase(words[i].charAt(0)))
00153                 .append(words[i].substring(1));
00154         }
00155         return result.toString();
00156     }
00157 
00158     public void setRobotName(String robotName) {
00159             // TODO: GraphName validation was removed. What replaced it?
00160             // if (!GraphName.validate(robotName)) {
00161             // throw new InvalidRobotDescriptionException("Bad robot name: " +
00162             // robotName);
00163             // }
00164             this.robotName = robotName;
00165     }
00166     public String getRobotType() {
00167             return robotType;
00168     }
00169     public void setRobotType(String robotType) {
00170             this.robotType = robotType;
00171     }
00172 
00173     public String getRobotIconFormat() {
00174         return robotIconFormat;
00175     }
00176 
00177     public ChannelBuffer getRobotIconData() {
00178         if ( robotIconData == null ) {
00179             return null;
00180         } else {
00181             ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer(robotIconData, robotIconDataOffset, robotIconDataLength);
00182             return channelBuffer;
00183         }
00184     }
00185 
00186     public void setRobotIcon(Icon robotIcon) {
00187         this.robotIconFormat = robotIcon.getFormat();
00188         this.robotIconData = robotIcon.getData().array();
00189     }
00190 
00191     public String getConnectionStatus() {
00192             return connectionStatus;
00193     }
00194     public void setConnectionStatus(String connectionStatus) {
00195             this.connectionStatus = connectionStatus;
00196     }
00197     public Date getTimeLastSeen() {
00198             return timeLastSeen;
00199     }
00200     public void setTimeLastSeen(Date timeLastSeen) {
00201             this.timeLastSeen = timeLastSeen;
00202     }
00203     public boolean isUnknown() {
00204             return this.robotName.equals(NAME_UNKNOWN);
00205     }
00206     public static RobotDescription createUnknown(RobotId robotId)  {
00207             return new RobotDescription(robotId, NAME_UNKNOWN, TYPE_UNKNOWN, null, NAME_UNKNOWN, new Date());
00208     }
00209     @Override
00210     public boolean equals(Object o) {
00211             // Return true if the objects are identical.
00212             // (This is just an optimization, not required for correctness.)
00213             if(this == o) {
00214                     return true;
00215             }
00216             // Return false if the other object has the wrong type.
00217             // This type may be an interface depending on the interface's
00218             // specification.
00219             if(!(o instanceof RobotDescription)) {
00220                     return false;
00221             }
00222             // Cast to the appropriate type.
00223             // This will succeed because of the instanceof, and lets us access
00224             // private fields.
00225             RobotDescription lhs = (RobotDescription) o;
00226             // Check each field. Primitive fields, reference fields, and nullable
00227             // reference
00228             // fields are all treated differently.
00229             return (robotId == null ? lhs.robotId == null : robotId.equals(lhs.robotId));
00230     }
00231     // I need to override equals() so I'm also overriding hashCode() to match.
00232     @Override
00233     public int hashCode() {
00234             // Start with a non-zero constant.
00235             int result = 17;
00236             // Include a hash for each field checked by equals().
00237             result = 31 * result + (robotId == null ? 0 : robotId.hashCode());
00238             return result;
00239     }
00240 }


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Fri Aug 28 2015 10:04:40