/// <summary> /// load global variables /// </summary> /// <param name="conf_filename">config filename</param> public static void init(string conf_filename) { IniFileReader iniReader; string[] szTrackerServers; string[] parts; iniReader = new IniFileReader(conf_filename); g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT); if (g_connect_timeout < 0) { g_connect_timeout = DEFAULT_CONNECT_TIMEOUT; } g_connect_timeout *= 1000; //millisecond g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT); if (g_network_timeout < 0) { g_network_timeout = DEFAULT_NETWORK_TIMEOUT; } g_network_timeout *= 1000; //millisecond var charset = iniReader.getStrValue("charset"); if (charset == null || charset.Length == 0) { charset = "ISO8859-1"; } g_charset = Encoding.GetEncoding(charset); szTrackerServers = iniReader.getValues("tracker_server"); if (szTrackerServers == null) { throw new MyException("item \"tracker_server\" in " + conf_filename + " not found"); } InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.Length]; for (int i = 0; i < szTrackerServers.Length; i++) { parts = szTrackerServers[i].Split("\\:", 2); if (parts.Length != 2) { throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port"); } tracker_servers[i] = new InetSocketAddress(parts[0].Trim(), int.Parse(parts[1].Trim())); } g_tracker_group = new TrackerGroup(tracker_servers); g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80); g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false); if (g_anti_steal_token) { g_secret_key = iniReader.getStrValue("http.secret_key"); } g_connection_pool_enabled = iniReader.getBoolValue("connection_pool.enabled", DEFAULT_CONNECTION_POOL_ENABLED); g_connection_pool_max_count_per_entry = iniReader.getIntValue("connection_pool.max_count_per_entry", DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY); g_connection_pool_max_idle_time = iniReader.getIntValue("connection_pool.max_idle_time", DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME); if (g_connection_pool_max_idle_time < 0) { g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME; } g_connection_pool_max_idle_time *= 1000; g_connection_pool_max_wait_time_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS); if (g_connection_pool_max_wait_time_in_ms < 0) { g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; } }
public static void initByTrackers(InetSocketAddress[] trackerAddresses) { g_tracker_group = new TrackerGroup(trackerAddresses); }
public static void setG_tracker_group(TrackerGroup tracker_group) { g_tracker_group = tracker_group; }
/// <summary> /// load global config /// </summary> /// <param name="conf_filename">config filename</param> public static void init(FdfsConfig config) { string[] szTrackerServers; string[] parts; g_connect_timeout = config.ConnectTimeout; if (g_connect_timeout < 0) { g_connect_timeout = DEFAULT_CONNECT_TIMEOUT; } g_connect_timeout *= 1000; //millisecond g_network_timeout = config.NetworkTimeout; if (g_network_timeout < 0) { g_network_timeout = DEFAULT_NETWORK_TIMEOUT; } g_network_timeout *= 1000; //millisecond var charset = config.Charset; if (charset == null) { charset = Encoding.GetEncoding("ISO8859-1"); } g_charset = charset; szTrackerServers = config.TrackerServers; if (szTrackerServers == null) { throw new MyException("item \"tracker_server\" not found"); } InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.Length]; for (int i = 0; i < szTrackerServers.Length; i++) { parts = szTrackerServers[i].Split(":", 2); if (parts.Length != 2) { throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port"); } tracker_servers[i] = new InetSocketAddress(parts[0].Trim(), int.Parse(parts[1].Trim())); } g_tracker_group = new TrackerGroup(tracker_servers); g_tracker_http_port = config.TrackerHttpPort; g_anti_steal_token = config.AntiStealToken; if (g_anti_steal_token) { g_secret_key = config.SecretKey; } g_connection_pool_enabled = config.ConnectionPoolEnabled; g_connection_pool_max_count_per_entry = config.ConnectionPoolMaxCountPerEntry; g_connection_pool_max_idle_time = config.ConnectionPoolMaxIdleTime; if (g_connection_pool_max_idle_time < 0) { g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME; } g_connection_pool_max_idle_time *= 1000; g_connection_pool_max_wait_time_in_ms = config.ConnectionPoolMaxWaitTime; if (g_connection_pool_max_wait_time_in_ms < 0) { g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; } }
public static void main(string[] args) { try { if (args.Length < 1) { Console.WriteLine("Usage: 2 parameters, one is config filename, " + "the other is the local filename to upload"); return; } string conf_filename = args[0]; string local_filename; string ext_name; if (args.Length > 1) { local_filename = args[1]; ext_name = null; } else if (Environment.OSVersion.Platform == PlatformID.Win32NT) { local_filename = "c:/windows/system32/notepad.exe"; ext_name = "exe"; } else { local_filename = "/etc/hosts"; ext_name = ""; } ClientGlobal.init(conf_filename); Console.WriteLine("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); Console.WriteLine("charset=" + ClientGlobal.g_charset); TrackerGroup tg = new TrackerGroup(new InetSocketAddress[] { new InetSocketAddress("47.95.221.159", 22122) }); TrackerClient tc = new TrackerClient(tg); TrackerServer ts = tc.getTrackerServer(); if (ts == null) { Console.WriteLine("getTrackerServer return null"); return; } StorageServer ss = tc.getStoreStorage(ts); if (ss == null) { Console.WriteLine("getStoreStorage return null"); } StorageClient1 sc1 = new StorageClient1(ts, ss); NameValuePair[] meta_list = null; //new NameValuePair[0]; string fileid = sc1.upload_file1(local_filename, ext_name, meta_list); Console.WriteLine("Upload local file " + local_filename + " ok, fileid: " + fileid); } catch (Exception ex) { Log.Error(ex.Message + ex.StackTrace); } }
/// <summary> /// delete a storage server from the FastDFS cluster /// </summary> /// <param name="trackerGroup">the tracker server group</param> /// <param name="groupName">the group name of storage server</param> /// <param name="storageIpAddr">the storage server ip address</param> /// <returns> true for success, false for fail</returns> public bool deleteStorage(TrackerGroup trackerGroup, string groupName, string storageIpAddr) { int serverIndex; int notFoundCount; TrackerServer trackerServer; notFoundCount = 0; for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.Length; serverIndex++) { try { trackerServer = trackerGroup.getTrackerServer(serverIndex); } catch { this.errno = ProtoCommon.ECONNREFUSED; return(false); } StructStorageStat[] storageStats = listStorages(trackerServer, groupName, storageIpAddr); if (storageStats == null) { if (this.errno == ProtoCommon.ERR_NO_ENOENT) { notFoundCount++; } else { return(false); } } else if (storageStats.Length == 0) { notFoundCount++; } else if (storageStats[0].getStatus() == ProtoCommon.FDFS_STORAGE_STATUS_ONLINE || storageStats[0].getStatus() == ProtoCommon.FDFS_STORAGE_STATUS_ACTIVE) { this.errno = ProtoCommon.ERR_NO_EBUSY; return(false); } } if (notFoundCount == trackerGroup.tracker_servers.Length) { this.errno = ProtoCommon.ERR_NO_ENOENT; return(false); } notFoundCount = 0; for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.Length; serverIndex++) { try { trackerServer = trackerGroup.getTrackerServer(serverIndex); } catch { Console.WriteLine("connect to server " + trackerGroup.tracker_servers[serverIndex].Address + ":" + trackerGroup.tracker_servers[serverIndex].Port + " fail"); this.errno = ProtoCommon.ECONNREFUSED; return(false); } if (!this.deleteStorage(trackerServer, groupName, storageIpAddr)) { if (this.errno != 0) { if (this.errno == ProtoCommon.ERR_NO_ENOENT) { notFoundCount++; } else if (this.errno != ProtoCommon.ERR_NO_EALREADY) { return(false); } } } } if (notFoundCount == trackerGroup.tracker_servers.Length) { this.errno = ProtoCommon.ERR_NO_ENOENT; return(false); } if (this.errno == ProtoCommon.ERR_NO_ENOENT) { this.errno = 0; } return(this.errno == 0); }
/// <summary> /// constructor with specified tracker group /// </summary> /// <param name="tracker_group">the tracker group object</param> public TrackerClient(TrackerGroup tracker_group) { this.tracker_group = tracker_group; }
/// <summary> /// constructor with global tracker group /// </summary> public TrackerClient() { this.tracker_group = ClientGlobal.g_tracker_group; }