00001 /* 00002 * Copyright (C) 2011 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 00005 * use this file except in compliance with the License. You may obtain a copy of 00006 * the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 00013 * License for the specific language governing permissions and limitations under 00014 * the License. 00015 */ 00016 00017 package org.ros.internal.node.topic; 00018 00019 import com.google.common.base.Preconditions; 00020 00021 import org.apache.commons.logging.Log; 00022 import org.apache.commons.logging.LogFactory; 00023 import org.ros.internal.node.BaseClientHandshake; 00024 import org.ros.internal.transport.ConnectionHeader; 00025 import org.ros.internal.transport.ConnectionHeaderFields; 00026 00032 public class SubscriberHandshake extends BaseClientHandshake { 00033 00034 private static final boolean DEBUG = false; 00035 private static final Log log = LogFactory.getLog(SubscriberHandshake.class); 00036 00037 public SubscriberHandshake(ConnectionHeader outgoingConnectionHeader) { 00038 super(outgoingConnectionHeader); 00039 Preconditions.checkNotNull(outgoingConnectionHeader.getField(ConnectionHeaderFields.TYPE)); 00040 Preconditions.checkNotNull(outgoingConnectionHeader 00041 .getField(ConnectionHeaderFields.MD5_CHECKSUM)); 00042 } 00043 00044 @Override 00045 public boolean handshake(ConnectionHeader incommingConnectionHeader) { 00046 if (DEBUG) { 00047 log.info("Outgoing subscriber connection header: " + outgoingConnectionHeader); 00048 log.info("Incoming publisher connection header: " + incommingConnectionHeader); 00049 } 00050 setErrorMessage(incommingConnectionHeader.getField(ConnectionHeaderFields.ERROR)); 00051 String incomingType = incommingConnectionHeader.getField(ConnectionHeaderFields.TYPE); 00052 if (incomingType == null) { 00053 setErrorMessage("Incoming type cannot be null."); 00054 } else if (!incomingType.equals(outgoingConnectionHeader.getField(ConnectionHeaderFields.TYPE))) { 00055 setErrorMessage("Message types don't match."); 00056 } 00057 String incomingMd5Checksum = 00058 incommingConnectionHeader.getField(ConnectionHeaderFields.MD5_CHECKSUM); 00059 if (incomingMd5Checksum == null) { 00060 setErrorMessage("Incoming MD5 checksum cannot be null."); 00061 } else if (!incomingMd5Checksum.equals(outgoingConnectionHeader 00062 .getField(ConnectionHeaderFields.MD5_CHECKSUM))) { 00063 setErrorMessage("Checksums don't match."); 00064 } 00065 return getErrorMessage() == null; 00066 } 00067 }