GrxServerManager.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 
11 package com.generalrobotix.ui.util;
12 
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.Vector;
16 
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.ui.preferences.ScopedPreferenceStore;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 
28 
29 @SuppressWarnings("serial")
30 public class GrxServerManager extends GrxBaseItem{
31  private static volatile Vector<ProcessInfo> vecServerInfo = new Vector<ProcessInfo>();
32  private static volatile ProcessInfo nameServerInfo = new ProcessInfo();
35  private static SynchronizedAccessor<String> nameServerLogDir = new SynchronizedAccessor<String>("");
36  private static final int MAXMUM_PORT_NUMBER = 65535;
37  private static final String LINE_SEPARATOR = new String( System.getProperty("line.separator") );
38 
39  public static int NAME_SERVER_PORT_ ;
40  public static String NAME_SERVER_HOST_ ;
41  public static String NAME_SERVER_LOG_DIR_ ;
42  public String serverInfoDefaultDir_ = "";
43  public int serverInfoDefaultWaitCount_ = 0;
44 
45  //次回起動のための値を保存
46  public synchronized void SaveServerInfo() {
47  setServerInfoToPreferenceStore();
48  setNameServerInfoToPreferenceStore();
49  }
50 
54  public GrxServerManager(String name, GrxPluginManager manager) {
55  super(name, manager);
56  }
57 
62  public Vector<ProcessInfo> getServerInfo(){
63  return vecServerInfo;
64  }
65 
71  return nameServerInfo;
72  }
73 
74 
75  public String getNewHost(){
76  return newHost_.get();
77  }
78 
79  public int getNewPort(){
80  return newPort_.get();
81  }
82 
83  public String getNameserverLogDir(){
84  return nameServerLogDir.get();
85  }
86 
87  public String setNewHostPort(String host, String port,StringBuffer refHost, StringBuffer refPort){
88  String retHost = checkHostFormat(host);
89  String retPort = checkPortFormat(port);
90 
91  String ret = retHost + retPort;
92  if( ret.isEmpty() ){
93  newPort_.set(Integer.valueOf(port));
94  newHost_.set(host);
95  } else {
96  if(!retHost.isEmpty()){
97  refHost.append( getNewHost() );
98  }
99  if(!retPort.isEmpty()){
100  refPort.append( Integer.toString(getNewPort()) );
101  }
102  }
103  return ret;
104  }
105 
114  public boolean toggleProcess(ProcessInfo pInfo){
115  GrxProcessManager pm = (GrxProcessManager) manager_.getItem("processManager");
116  AProcess process = pm.get(pInfo.id);
117  StringBuffer nsHost = new StringBuffer("");
118  StringBuffer nsPort = new StringBuffer("");
119  Activator.refNSHostPort(nsHost, nsPort);
120  String nsOpt = "-ORBInitRef NameService=corbaloc:iiop:" + nsHost +
121  ":" + nsPort + "/NameService";
122 
123  if (process!=null){
124  if (process.isRunning()) {
125  // 停止処理
126  process.stop();
127  return false;
128  }else
129  pm.unregister(pInfo.id);
130  }
131  // 新規登録と開始処理
132  String s = pInfo.com.get(pInfo.editComIndex);
133  if(!(new File(s)).isAbsolute()){
134  pInfo.com.clear();
135  String ss = comToAbsolutePath(s);
136  if(ss!=null)
137  pInfo.com.add(ss);
138  pInfo.com.add(s);
139  }
140  if (pInfo.useORB) {
141  for(int i=0; i<pInfo.com.size(); i++)
142  pInfo.com.set(i, pInfo.com.get(i) + " " + nsOpt);
143  }
144  updatedParam(pInfo);
145  pm.register(pInfo);
146  if (!pm.get(pInfo.id).start(null)) {
147  // start 失敗
148  pm.unregister(pInfo.id);
149  return false;
150  }
151 
152  return true;
153  }
154 
161  private boolean updatedParam(ProcessInfo pInfo) {
162  boolean ret = false;
163  for (ProcessInfo pi : vecServerInfo) {
164 
165  if (pi.id.equals(pInfo.id)) {
166  if (pInfo.autoStart != pi.autoStart) {
167  pi.autoStart = pInfo.autoStart;
168  ret = true;
169  }
170  if (pInfo.useORB != pi.useORB) {
171  pi.useORB = pInfo.useORB;
172  ret = true;
173  }
174  if (!pInfo.com.equals(pi.com)) {
175  pi.com.clear();
176  for (String i : pInfo.com) {
177  pi.com.add(new String(i));
178  }
179  ret = true;
180  }
181  if (!pInfo.args.equals(pi.args)) {
182  pi.args = pInfo.args;
183  ret = true;
184  }
185  break;
186  }
187  }
188 
189  return ret;
190  }
191 
192  private String checkHostFormat(String host){
193  String ret = "";
194  if(host.length() > 255){
195  return MessageBundle.get("GrxServerManager.message.hostFormat.num") + LINE_SEPARATOR;
196  }
197 
198  int limit = host.length() - host.replace(".", "").length();
199 
200  String [] splitStr = host.split("\\.", limit == 0 ? limit : limit + 1);
201 
202  for(String i : splitStr){
203  if(i.length() < 1 || i.length() > 63){
204  return MessageBundle.get("GrxServerManager.message.hostFormat.label") + LINE_SEPARATOR;
205  }
206  if( i.matches("^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]$") ||
207  i.matches("^[a-zA-Z0-9]$") ){
208  continue;
209  } else {
210  return MessageBundle.get("GrxServerManager.message.hostFormat.rfc") + LINE_SEPARATOR;
211  }
212  }
213  return ret;
214  }
215 
216  private String checkPortFormat(String port){
217  String ret = "";
218  try{
219  int portInt = Integer.valueOf(port);
220  if( portInt > MAXMUM_PORT_NUMBER || portInt < 0)
221  ret = MessageBundle.get("GrxServerManager.message.portFormat1") +
222  Integer.toString(MAXMUM_PORT_NUMBER) + MessageBundle.get("GrxServerManager.message.portFormat2") + LINE_SEPARATOR;
223  }catch (NumberFormatException ex){
224  ret = MessageBundle.get("GrxServerManager.message.portFormat1") +
225  Integer.toString(MAXMUM_PORT_NUMBER) + MessageBundle.get("GrxServerManager.message.portFormat2") + LINE_SEPARATOR;
226  }
227  return ret;
228  }
229 
230  public void initialize(){
231  getNameServerInfoFromPerferenceStore();
232  vecServerInfo.clear();
233  getServerInfoFromPerferenceStore();
234  newPort_.set(NAME_SERVER_PORT_);
235  newHost_.set(NAME_SERVER_HOST_);
236  nameServerLogDir.set(NAME_SERVER_LOG_DIR_);
237  }
238 
239  public void restart(IProgressMonitor monitor){
240  NAME_SERVER_PORT_ = newPort_.get();
241  NAME_SERVER_HOST_ = newHost_.get();
242  nameServerInfo.args = generateNameServerArgs();
243  GrxProcessManager pm = (GrxProcessManager) manager_.getItem("processManager");
244  pm.restart(monitor);
245  }
246 
247  private String generateNameServerArgs(){
248  String ret;
249  ret = "-start " + Integer.toString(NAME_SERVER_PORT_) + " -logdir " + NAME_SERVER_LOG_DIR_ +
250  " -ORBendPointPublish giop:tcp:"+ NAME_SERVER_HOST_ + ":";
251  return ret;
252  }
254  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
255  nameServerInfo.id = PreferenceConstants.NAMESERVER;
256  String _dir = store.getString(
258  NAME_SERVER_LOG_DIR_ = GrxXmlUtil.expandEnvVal(_dir).trim();
259  if(!NAME_SERVER_LOG_DIR_.isEmpty()){
260  String localStr = NAME_SERVER_LOG_DIR_.replaceFirst("^\"", "");
261  File logDir = new File(localStr.replaceFirst("\"$", ""));
262  if(!logDir.exists()){
263  logDir.mkdirs();
264  }
265  }
266  NAME_SERVER_PORT_ = store.getInt(
268  if(NAME_SERVER_PORT_==0)
269  NAME_SERVER_PORT_ = store.getDefaultInt(
271  NAME_SERVER_HOST_ = store.getString(
273  if(NAME_SERVER_HOST_.equals(""))
274  NAME_SERVER_HOST_ = store.getDefaultString(
276 
277  nameServerInfo.args = generateNameServerArgs();
278 
279  String s = store.getString(
281  // 優先度 SERVER_DIRの下, Pathの通っているところ, AbsolutePath //
282  String ss=comToAbsolutePath(s);
283  if(ss!=null)
284  nameServerInfo.com.add(ss);
285  nameServerInfo.com.add(s);
286  s = store.getString(
288  if(!s.equals(""))
289  nameServerInfo.com.add(s);
290 
291  nameServerInfo.autoStart = true;
292  nameServerInfo.waitCount = store.getInt(
294  nameServerInfo.dir = store.getString(
296  nameServerInfo.isCorbaServer = false;
297  nameServerInfo.hasShutdown = store.getBoolean(
299  nameServerInfo.doKillall = false;
300  nameServerInfo.autoStop = true;
301  }
302 
303  private String[] parse(String string){
304  String[] ret = string.split("\""+PreferenceConstants.SEPARATOR+"\"",-1);
305  if(ret.length!=0){
306  if(ret[0].charAt(0)=='\"')
307  ret[0] = ret[0].substring(1,ret[0].length());
308  String s=ret[ret.length-1];
309  if(s.charAt(s.length()-1)=='\"')
310  ret[ret.length-1] = s.substring(0,s.length()-1);
311  }
312  return ret;
313  }
314 
316  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
317  String idList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID);
318  String[] id = idList.split(PreferenceConstants.SEPARATOR,-1);
319  String dirList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.DIR);
320  String[] dir = parse(dirList);
321  String waitCountList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.WAITCOUNT);
322  String[] waitCount = waitCountList.split(PreferenceConstants.SEPARATOR,-1);
323  String comList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM);
324  String[] com = parse(comList);
325  String argsList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS);
326  String[] args = parse(argsList);
327  String autoStartList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART);
328  String[] autoStart = autoStartList.split(PreferenceConstants.SEPARATOR,-1);
329  String useORBList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB);
330  String[] useORB = useORBList.split(PreferenceConstants.SEPARATOR,-1);
331  String hasShutDownList = store.getString(PreferenceConstants.PROCESS+"."+PreferenceConstants.HASSHUTDOWN);
332  String[] hasShutDown = hasShutDownList.split(PreferenceConstants.SEPARATOR,-1);
333  for(int i=1; i<id.length; i++){
334  ProcessInfo processInfo = new ProcessInfo();
335  processInfo.id = id[i];
336  String s=null;
337  if(i<com.length && !com[i].equals(""))
338  s = com[i].trim();
339  else
340  s = com[0].trim();
341  // 優先度 AbsolutePath, SERVER_DIRの下, Pathの通っているところ //
342  if((new File(s)).isAbsolute())
343  processInfo.com.add(s);
344  else{
345  String ss=comToAbsolutePath(s);
346  if(ss!=null){
347  processInfo.com.add(ss);
348  processInfo.editComIndex = 1;
349  }
350  processInfo.com.add(s);
351  }
352  if(i<args.length && !args[i].equals(""))
353  processInfo.args = args[i].trim();
354  else
355  processInfo.args = args[0].trim();
356  if(i<autoStart.length && !autoStart[i].equals(""))
357  processInfo.autoStart = autoStart[i].equals("true")? true : false;
358  else
359  processInfo.autoStart = autoStart[0].equals("true")? true : false;
360  if(i<useORB.length && !useORB[i].equals(""))
361  processInfo.useORB = useORB[i].equals("true")? true : false;
362  else
363  processInfo.useORB = useORB[0].equals("true")? true : false;
364  if(i<hasShutDown.length && !hasShutDown[i].equals(""))
365  processInfo.hasShutdown = hasShutDown[i].equals("true")? true : false;
366  else
367  processInfo.hasShutdown = hasShutDown[0].equals("true")? true : false;
368  if(i<waitCount.length && !waitCount[i].equals(""))
369  processInfo.waitCount = Integer.parseInt(waitCount[i]);
370  else
371  processInfo.waitCount = Integer.parseInt(waitCount[0]);
372  if(i<dir.length && !dir[i].equals(""))
373  processInfo.dir = dir[i].trim();
374  else
375  processInfo.dir = dir[0].trim();
376  processInfo.isCorbaServer = true;
377  processInfo.doKillall = false;
378  processInfo.autoStop = true;
379  vecServerInfo.add(processInfo);
380  }
381  serverInfoDefaultDir_ = dir[0].trim();
382  serverInfoDefaultWaitCount_ = Integer.parseInt(waitCount[0]);
383 
384  if(nameServerInfo.waitCount==0)
385  nameServerInfo.waitCount = serverInfoDefaultWaitCount_;
386  if(nameServerInfo.dir.equals(""))
387  nameServerInfo.dir = serverInfoDefaultDir_;
388  }
389 
390  private String comToAbsolutePath(String com){
391  if((new File(com)).isAbsolute())
392  return null;
393  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
394  String serverDir = store.getString("SERVER_DIR");
395  if(!serverDir.equals("")){
396  File file = new File(serverDir);
397  String[] list = file.list();
398  for(int i=0; i<list.length; i++){
399  int endIndex = list[i].lastIndexOf(".");
400  if(endIndex<0)
401  endIndex = list[i].length();
402  String s=list[i].substring(0, endIndex);
403  if(s.equals(com)){
404  return serverDir+"/"+com;
405  }
406  }
407  }
408  return null;
409  }
410 
411  public static void setServerInfoToPreferenceStore(){
412  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
413  String id = PreferenceConstants.ALLSERVER;
414  String com = "\"\"";
415  String args = "\"\"";
416  String autoStart= "false";
417  String useORB= "false";
418  for(int i=0; i<vecServerInfo.size(); i++){
419  ProcessInfo processInfo =vecServerInfo.elementAt(i);
420  id += PreferenceConstants.SEPARATOR + processInfo.id;
421  com += PreferenceConstants.SEPARATOR + "\""+processInfo.com.get(processInfo.editComIndex)+"\"";
422  args += PreferenceConstants.SEPARATOR + "\""+processInfo.args+"\"";
423  autoStart += PreferenceConstants.SEPARATOR + (processInfo.autoStart? "true" : "false");
424  useORB += PreferenceConstants.SEPARATOR + (processInfo.useORB? "true" : "false");
425  }
426  store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID, id);
427  store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM, com);
428  store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS, args);
429  store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART, autoStart);
430  store.setValue(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB, useORB);
431  }
432 
434  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
436  newPort_.get());
438  newHost_.get());
439  }
440 
441  public void restoreDefault(){
442  IPreferenceStore store =Activator.getDefault().getPreferenceStore();
445  store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID);
446  store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM);
447  store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS);
449  store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB);
450  initialize();
451  }
452 }
static final String get(String key)
#define null
our own NULL pointer
Definition: IceTypes.h:57
GrxServerManager(String name, GrxPluginManager manager)
png_infop png_charpp name
Definition: png.h:2382
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
boolean toggleProcess(ProcessInfo pInfo)
サーバプログラムの起動トグル
png_bytep png_bytep png_size_t length
Definition: png.h:1541
png_uint_32 i
Definition: png.h:2735
ProcessInfo getNameServerInfo()
NameServerのProcessInfo取得
boolean updatedParam(ProcessInfo pInfo)
vecServerInfoの更新 vecServerInfoに同名のidが存在すればpInfoを元に更新する
static String expandEnvVal(String str)
String setNewHostPort(String host, String port, StringBuffer refHost, StringBuffer refPort)
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
Vector< ProcessInfo > getServerInfo()
vecServerInfoの取得
org


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03