ALMA Computing Group

Client.java

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * ALMA - Atacama Large Millimeter Array
00003  * Copyright (c) ESO - European Southern Observatory, 2011
00004  * (in the framework of the ALMA collaboration).
00005  * All rights reserved.
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  *******************************************************************************/
00021 package alma.demo.dyncomp;
00022 
00023 import java.util.logging.Logger;
00024 import alma.acs.component.client.ComponentClient;
00025 import alma.ACS.ACSComponent;
00026 import alma.ACS.ACSComponentHelper;
00027 import alma.JavaContainerError.wrappers.AcsJContainerServicesEx;
00028 
00029 import si.ijs.maci.ComponentSpec;
00030 import si.ijs.maci.ComponentInfo;
00031 
00037 public class Client extends ComponentClient {
00038 
00039         private final int MAXCOMPONENTS=32;
00040         // The two lists with the acivated components and their URLs
00041         // When a component is freed the items are not reordered (so a hole will exist in the
00042         // position of the freed element)
00043         // Free slots contain null
00044         ACSComponent m_components[];
00045         String m_cURLs[];
00046 
00049         public Client(Logger logger, String managerLoc, String clientName) 
00050                 throws Exception
00051         {
00052                 super(logger,managerLoc,clientName);
00053                 m_components =  new ACSComponent[MAXCOMPONENTS];
00054                 m_cURLs= new String[MAXCOMPONENTS];
00055                 for (int t=0; t<MAXCOMPONENTS; t++) {
00056                         m_components[t]=null;
00057                         m_cURLs[t]=null;
00058                 }
00059         }
00060 
00065         public boolean hasFreeSlot() {
00066                 boolean ret=false;
00067                 for (int t=0; t<MAXCOMPONENTS; t++) 
00068                         if (m_components[t]==null) ret=true;
00069                 return ret;
00070         }
00071 
00077         public String getDynamicComponent(ComponentSpec cs, boolean markAsDefault) 
00078                 throws AcsJContainerServicesEx
00079         {
00080                 // Search for the first free slot in the array
00081                 int freeSlot=0;
00082                 while (freeSlot<MAXCOMPONENTS && m_components[freeSlot]!=null) freeSlot++;
00083                 if (freeSlot<MAXCOMPONENTS) {
00084                         m_components[freeSlot]=ACSComponentHelper.narrow(getContainerServices().getDynamicComponent(cs,markAsDefault));
00085                         if (m_components[freeSlot]!=null) {
00086                                 m_cURLs[freeSlot]=m_components[freeSlot].name();
00087                                 return  m_cURLs[freeSlot];
00088                         } else {
00089                                 m_cURLs[freeSlot]=null;
00090                                 return null;
00091                         }
00092                 }
00093                 return null;
00094         }
00095 
00101         public boolean releaseComponent(String url) {
00102                 // Search if the given url exists
00103                 int t;
00104                 for (t=0; t<MAXCOMPONENTS; t++) {
00105                         if (m_cURLs[t]!=null)
00106                                 if (url.compareTo(m_cURLs[t])==0) break;
00107                 }
00108                 if (t<MAXCOMPONENTS && url.compareTo(m_cURLs[t])==0) {
00109                         getContainerServices().releaseComponent(url);
00110                         m_components[t]=null;
00111                         m_cURLs[t]=null;
00112                         return true;
00113                 } else return false;
00114         }
00115 
00118         public void cleanExit() {
00119                 String cURLs[];
00120                 for (int t=0; t<MAXCOMPONENTS; t++) {
00121                         if (m_cURLs[t]!=null && m_components[t]!=null) 
00122                                 getContainerServices().releaseComponent(m_cURLs[t]);
00123                                 m_components[t]=null;
00124                                 m_cURLs[t]=null;
00125                 }
00126         }
00127 }
00128