ClientGlobal.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Copyright (C) 2008 Happy Fish / YuQing
  3. *
  4. * FastDFS Java Client may be copied only under the terms of the GNU Lesser
  5. * General Public License (LGPL).
  6. * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
  7. **/
  8. package org.csource.fastdfs;
  9. import org.csource.common.IniFileReader;
  10. import org.csource.common.MyException;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.net.InetSocketAddress;
  14. import java.net.Socket;
  15. /**
  16. * Global variables
  17. * @author Happy Fish / YuQing
  18. * @version Version 1.11
  19. */
  20. public class ClientGlobal
  21. {
  22. public static int g_connect_timeout; //millisecond
  23. public static int g_network_timeout; //millisecond
  24. public static String g_charset;
  25. public static int g_tracker_http_port;
  26. public static boolean g_anti_steal_token; //if anti-steal token
  27. public static String g_secret_key; //generage token secret key
  28. public static TrackerGroup g_tracker_group;
  29. public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
  30. public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
  31. private ClientGlobal()
  32. {
  33. }
  34. /**
  35. * load global variables
  36. * @param conf_filename config filename
  37. */
  38. public static void init(String conf_filename) throws FileNotFoundException, IOException, MyException
  39. {
  40. IniFileReader iniReader;
  41. String[] szTrackerServers;
  42. String[] parts;
  43. iniReader = new IniFileReader(conf_filename);
  44. g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
  45. if (g_connect_timeout < 0)
  46. {
  47. g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  48. }
  49. g_connect_timeout *= 1000; //millisecond
  50. g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
  51. if (g_network_timeout < 0)
  52. {
  53. g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
  54. }
  55. g_network_timeout *= 1000; //millisecond
  56. g_charset = iniReader.getStrValue("charset");
  57. if (g_charset == null || g_charset.length() == 0)
  58. {
  59. g_charset = "ISO8859-1";
  60. }
  61. szTrackerServers = iniReader.getValues("tracker_server");
  62. if (szTrackerServers == null)
  63. {
  64. throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
  65. }
  66. InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
  67. for (int i=0; i<szTrackerServers.length; i++)
  68. {
  69. parts = szTrackerServers[i].split("\\:", 2);
  70. if (parts.length != 2)
  71. {
  72. throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
  73. }
  74. tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
  75. }
  76. g_tracker_group = new TrackerGroup(tracker_servers);
  77. g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
  78. g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
  79. if (g_anti_steal_token)
  80. {
  81. g_secret_key = iniReader.getStrValue("http.secret_key");
  82. }
  83. }
  84. /**
  85. * construct Socket object
  86. * @param ip_addr ip address or hostname
  87. * @param port port number
  88. * @return connected Socket object
  89. */
  90. public static Socket getSocket(String ip_addr, int port) throws IOException
  91. {
  92. Socket sock = new Socket();
  93. sock.setSoTimeout(ClientGlobal.g_network_timeout);
  94. sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
  95. return sock;
  96. }
  97. /**
  98. * construct Socket object
  99. * @param addr InetSocketAddress object, including ip address and port
  100. * @return connected Socket object
  101. */
  102. public static Socket getSocket(InetSocketAddress addr) throws IOException
  103. {
  104. Socket sock = new Socket();
  105. sock.setSoTimeout(ClientGlobal.g_network_timeout);
  106. sock.connect(addr, ClientGlobal.g_connect_timeout);
  107. return sock;
  108. }
  109. public static int getG_connect_timeout()
  110. {
  111. return g_connect_timeout;
  112. }
  113. public static void setG_connect_timeout(int connect_timeout)
  114. {
  115. ClientGlobal.g_connect_timeout = connect_timeout;
  116. }
  117. public static int getG_network_timeout()
  118. {
  119. return g_network_timeout;
  120. }
  121. public static void setG_network_timeout(int network_timeout)
  122. {
  123. ClientGlobal.g_network_timeout = network_timeout;
  124. }
  125. public static String getG_charset()
  126. {
  127. return g_charset;
  128. }
  129. public static void setG_charset(String charset)
  130. {
  131. ClientGlobal.g_charset = charset;
  132. }
  133. public static int getG_tracker_http_port()
  134. {
  135. return g_tracker_http_port;
  136. }
  137. public static void setG_tracker_http_port(int tracker_http_port)
  138. {
  139. ClientGlobal.g_tracker_http_port = tracker_http_port;
  140. }
  141. public static boolean getG_anti_steal_token()
  142. {
  143. return g_anti_steal_token;
  144. }
  145. public static boolean isG_anti_steal_token()
  146. {
  147. return g_anti_steal_token;
  148. }
  149. public static void setG_anti_steal_token(boolean anti_steal_token)
  150. {
  151. ClientGlobal.g_anti_steal_token = anti_steal_token;
  152. }
  153. public static String getG_secret_key()
  154. {
  155. return g_secret_key;
  156. }
  157. public static void setG_secret_key(String secret_key)
  158. {
  159. ClientGlobal.g_secret_key = secret_key;
  160. }
  161. public static TrackerGroup getG_tracker_group()
  162. {
  163. return g_tracker_group;
  164. }
  165. public static void setG_tracker_group(TrackerGroup tracker_group)
  166. {
  167. ClientGlobal.g_tracker_group = tracker_group;
  168. }
  169. }