MasterDescription.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2013, Yujin Robot.
00005  *
00006  * All rights reserved.
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  *  * Neither the name of Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote products derived
00019  *    from this software without specific prior written permission.
00020  *   
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package com.github.rosjava.android_remocons.common_tools.master;
00036 
00037 import org.jboss.netty.buffer.ChannelBuffer;
00038 import org.jboss.netty.buffer.ChannelBuffers;
00039 
00040 import java.util.Date;
00041 import java.util.regex.Matcher;
00042 import java.util.regex.Pattern;
00043 
00044 import rocon_std_msgs.Icon;
00045 
00051 public class MasterDescription implements java.io.Serializable {
00052     // Unique identifier for keys passed between android apps.
00053     public static final String UNIQUE_KEY = "com.github.rosjava.android_remocons.master.MasterDescription";
00054     private static final long serialVersionUID = 1L;
00055 
00056     public static final String CONNECTING = "connecting...";
00057     public static final String OK = "ok";
00058     public static final String ERROR = "exception";
00059     public static final String WIFI = "invalid wifi";
00060     public static final String UNAVAILABLE = "unavailable";  // when the master app manager is busy serving another remote controller
00061     public static final String CONTROL = "not started";
00062     public static final String NAME_UNKNOWN = "Unknown";
00063     public static final String TYPE_UNKNOWN = "Unknown";
00064 
00065     private MasterId masterId;
00066     private String masterName;
00067     private String masterType; // TODO this contains robot type, but in concerts don't make a lot of sense; move to RobotDescription?
00068     private String appsNameSpace;
00069 
00072     private String masterIconFormat;
00073     private byte[] masterIconData;
00074     private int masterIconDataOffset;
00075     private int masterIconDataLength;
00076 
00077     private String connectionStatus;
00078     private Date timeLastSeen;
00079 
00080     // TODO(kwc): add in canonicalization of masterName
00081     public MasterDescription() {
00082     }
00083 
00084     public MasterDescription(MasterId masterId, String masterName, String masterType,
00085                              Icon masterIcon, String appsNameSpace, Date timeLastSeen) {
00086         setMasterName(masterName);
00087         setMasterId(masterId);
00088         this.masterName = masterName;
00089         this.masterType = masterType;
00090         this.appsNameSpace = appsNameSpace;
00091         if (masterIcon != null) {
00092             this.masterIconFormat = masterIcon.getFormat();
00093             this.masterIconData = masterIcon.getData().array();
00094             this.masterIconDataOffset = masterIcon.getData().arrayOffset();
00095             this.masterIconDataLength = masterIcon.getData().readableBytes();
00096         }
00097         this.timeLastSeen = timeLastSeen;
00098     }
00099 
00100     public void copyFrom(MasterDescription other) {
00101         masterId = other.masterId;
00102         masterName = other.masterName;
00103         masterType = other.masterType;
00104         appsNameSpace = other.appsNameSpace;
00105         masterIconFormat = other.masterIconFormat;
00106         masterIconData = other.masterIconData;
00107         masterIconDataOffset = other.masterIconDataOffset;
00108         masterIconDataLength = other.masterIconDataLength;
00109         connectionStatus = other.connectionStatus;
00110         timeLastSeen = other.timeLastSeen;
00111     }
00112 
00113     public MasterId getMasterId() {
00114         return masterId;
00115     }
00116 
00117     public String getAppsNameSpace() {
00118         return appsNameSpace;
00119     }
00120 
00126     public String getMasterUri() {
00127         return masterId.getMasterUri();
00128     }
00129 
00130     public void setMasterId(MasterId masterId) {
00131         // TODO: ensure the master id is sane.
00132 //              if(false) {
00133 //                      throw new InvalidMasterDescriptionException("Empty Master URI");
00134 //              }
00135         // TODO: validate
00136         this.masterId = masterId;
00137     }
00138 
00139     public String getMasterName() {
00140         return masterName;
00141     }
00142 
00153     public String getMasterFriendlyName() {
00154         String friendlyName = masterName;
00155         // The uuid is a 16 byte hash in hex format = 32 characters
00156         if (masterName.length() > 32) {
00157             String possibleUuidPart = masterName.substring(masterName.length() - 32);
00158             Pattern p = Pattern.compile("[^a-f0-9]");
00159             Matcher m = p.matcher(possibleUuidPart);
00160             if (!m.find()) {
00161                 friendlyName = masterName.substring(0, masterName.length() - 32);
00162             }
00163         }
00164         friendlyName = friendlyName.replace('_', ' ');
00165         final StringBuilder result = new StringBuilder(friendlyName.length());
00166         String[] words = friendlyName.split("\\s");
00167         for (int i = 0, l = words.length; i < l; ++i) {
00168             if (i > 0) result.append(" ");
00169             result.append(Character.toUpperCase(words[i].charAt(0)))
00170                     .append(words[i].substring(1));
00171         }
00172         return result.toString();
00173     }
00174 
00175     public void setMasterName(String masterName) {
00176         // TODO: GraphName validation was removed. What replaced it?
00177         // if (!GraphName.validate(masterName)) {
00178         // throw new InvalidMasterDescriptionException("Bad master name: " +
00179         // masterName);
00180         // }
00181         this.masterName = masterName;
00182     }
00183 
00184     public String getMasterType() {
00185         return masterType;
00186     }
00187 
00188     public void setMasterType(String masterType) {
00189         this.masterType = masterType;
00190     }
00191 
00192     public String getMasterIconFormat() {
00193         return masterIconFormat;
00194     }
00195 
00196     public ChannelBuffer getMasterIconData() {
00197         if (masterIconData == null) {
00198             return null;
00199         } else {
00200             ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer(masterIconData, masterIconDataOffset, masterIconDataLength);
00201             return channelBuffer;
00202         }
00203     }
00204 
00205     public void setMasterIconFormat(String iconFormat) {
00206         this.masterIconFormat = iconFormat;
00207     }
00208 
00209     public void setMasterIconData(ChannelBuffer iconData) {
00210         this.masterIconData = iconData.array();
00211     }
00212 
00213     public void setMasterIcon(Icon masterIcon) {
00214         this.masterIconFormat = masterIcon.getFormat();
00215         this.masterIconData = masterIcon.getData().array();
00216     }
00217 
00218     public String getConnectionStatus() {
00219         return connectionStatus;
00220     }
00221 
00222     public void setConnectionStatus(String connectionStatus) {
00223         this.connectionStatus = connectionStatus;
00224     }
00225 
00226     public Date getTimeLastSeen() {
00227         return timeLastSeen;
00228     }
00229 
00230     public void setTimeLastSeen(Date timeLastSeen) {
00231         this.timeLastSeen = timeLastSeen;
00232     }
00233 
00234     public boolean isUnknown() {
00235         return this.masterName.equals(NAME_UNKNOWN);
00236     }
00237 
00238 //    public static MasterDescription createUnknown(MasterId masterId) {
00239 //        return new MasterDescription(masterId, NAME_UNKNOWN, TYPE_UNKNOWN, null, NAME_UNKNOWN, new Date());
00240 //    }
00241 
00242     @Override
00243     public boolean equals(Object o) {
00244         // Return true if the objects are identical.
00245         // (This is just an optimization, not required for correctness.)
00246         if (this == o) {
00247             return true;
00248         }
00249         // Return false if the other object has the wrong type.
00250         // This type may be an interface depending on the interface's
00251         // specification.
00252         if (!(o instanceof MasterDescription)) {
00253             return false;
00254         }
00255         // Cast to the appropriate type.
00256         // This will succeed because of the instanceof, and lets us access
00257         // private fields.
00258         MasterDescription lhs = (MasterDescription) o;
00259         // Check each field. Primitive fields, reference fields, and nullable
00260         // reference
00261         // fields are all treated differently.
00262         return (masterId == null ? lhs.masterId == null : masterId.equals(lhs.masterId));
00263     }
00264 
00265     // I need to override equals() so I'm also overriding hashCode() to match.
00266     @Override
00267     public int hashCode() {
00268         // Start with a non-zero constant.
00269         int result = 17;
00270         // Include a hash for each field checked by equals().
00271         result = 31 * result + (masterId == null ? 0 : masterId.hashCode());
00272         return result;
00273     }
00274 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24