ServiceFactory.java
Go to the documentation of this file.
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.service;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import org.ros.exception.DuplicateServiceException;
00022 import org.ros.internal.message.service.ServiceDescription;
00023 import org.ros.internal.node.server.SlaveServer;
00024 import org.ros.message.MessageDeserializer;
00025 import org.ros.message.MessageFactory;
00026 import org.ros.message.MessageSerializer;
00027 import org.ros.namespace.GraphName;
00028 import org.ros.node.service.ServiceClient;
00029 import org.ros.node.service.ServiceResponseBuilder;
00030 import org.ros.node.service.ServiceServer;
00031 
00032 import java.util.concurrent.ScheduledExecutorService;
00033 
00039 public class ServiceFactory {
00040 
00041   private final GraphName nodeName;
00042   private final SlaveServer slaveServer;
00043   private final ServiceManager serviceManager;
00044   private final ScheduledExecutorService executorService;
00045   private final Object mutex;
00046 
00047   public ServiceFactory(GraphName nodeName, SlaveServer slaveServer, ServiceManager serviceManager,
00048       ScheduledExecutorService executorService) {
00049     this.nodeName = nodeName;
00050     this.slaveServer = slaveServer;
00051     this.serviceManager = serviceManager;
00052     this.executorService = executorService;
00053     mutex = new Object();
00054   }
00055 
00072   public <T, S> DefaultServiceServer<T, S> newServer(ServiceDeclaration serviceDeclaration,
00073       ServiceResponseBuilder<T, S> responseBuilder, MessageDeserializer<T> deserializer,
00074       MessageSerializer<S> serializer, MessageFactory messageFactory) {
00075     DefaultServiceServer<T, S> serviceServer;
00076     GraphName name = serviceDeclaration.getName();
00077 
00078     synchronized (mutex) {
00079       if (serviceManager.hasServer(name)) {
00080         throw new DuplicateServiceException(String.format("ServiceServer %s already exists.", name));
00081       } else {
00082         serviceServer =
00083             new DefaultServiceServer<T, S>(serviceDeclaration, responseBuilder,
00084                 slaveServer.getTcpRosAdvertiseAddress(), deserializer, serializer, messageFactory,
00085                 executorService);
00086         serviceManager.addServer(serviceServer);
00087       }
00088     }
00089     return serviceServer;
00090   }
00091 
00098   @SuppressWarnings("unchecked")
00099   public <T, S> DefaultServiceServer<T, S> getServer(GraphName name) {
00100     if (serviceManager.hasServer(name)) {
00101       return (DefaultServiceServer<T, S>) serviceManager.getServer(name);
00102     }
00103     return null;
00104   }
00105 
00122   @SuppressWarnings("unchecked")
00123   public <T, S> DefaultServiceClient<T, S> newClient(ServiceDeclaration serviceDeclaration,
00124       MessageSerializer<T> serializer, MessageDeserializer<S> deserializer,
00125       MessageFactory messageFactory) {
00126     Preconditions.checkNotNull(serviceDeclaration.getUri());
00127     DefaultServiceClient<T, S> serviceClient;
00128     GraphName name = serviceDeclaration.getName();
00129     boolean createdNewClient = false;
00130 
00131     synchronized (mutex) {
00132       if (serviceManager.hasClient(name)) {
00133         serviceClient = (DefaultServiceClient<T, S>) serviceManager.getClient(name);
00134       } else {
00135         serviceClient =
00136             DefaultServiceClient.newDefault(nodeName, serviceDeclaration, serializer, deserializer,
00137                 messageFactory, executorService);
00138         serviceManager.addClient(serviceClient);
00139         createdNewClient = true;
00140       }
00141     }
00142 
00143     if (createdNewClient) {
00144       serviceClient.connect(serviceDeclaration.getUri());
00145     }
00146     return serviceClient;
00147   }
00148 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49