/**
         * entry point
         *
         * @param args comand arguments
         *             <ul><li>args[0]: config filename</li></ul>
         *             <ul><li>args[1]: local filename to upload</li></ul>
         */
        public static void main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Error: Must have 2 parameters, one is config filename, "
                                  + "the other is the local filename to upload");
                return;
            }

            Console.WriteLine("dotnetcore.version=" + typeof(object).GetTypeInfo().Assembly.GetName().Version.ToString());

            string conf_filename  = args[0];
            string local_filename = args[1];

            try
            {
                ClientGlobal.init(conf_filename);
                Console.WriteLine("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
                Console.WriteLine("charset=" + ClientGlobal.g_charset);

                long          startTime;
                string        group_name;
                string        remote_filename;
                ServerInfo[]  servers;
                TrackerClient tracker       = new TrackerClient();
                TrackerServer trackerServer = tracker.getTrackerServer();

                StorageServer storageServer = null;

                /*
                 * storageServer = tracker.getStoreStorage(trackerServer);
                 * if (storageServer == null)
                 * {
                 *  Console.WriteLine("getStoreStorage fail, error code: " + tracker.getErrorCode());
                 *  return;
                 * }
                 */

                StorageClient   client = new StorageClient(trackerServer, storageServer);
                byte[]          file_buff;
                NameValuePair[] meta_list;
                string[]        results;
                string          master_filename;
                string          prefix_name;
                string          file_ext_name;
                string          generated_slave_filename;
                int             errno;

                meta_list    = new NameValuePair[4];
                meta_list[0] = new NameValuePair("width", "800");
                meta_list[1] = new NameValuePair("heigth", "600");
                meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF");
                meta_list[3] = new NameValuePair("author", "Mike");

                file_buff = ClientGlobal.g_charset.GetBytes("this is a test");
                Console.WriteLine("file Length: " + file_buff.Length);

                group_name = null;
                StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
                if (storageServers == null)
                {
                    Log.Error("get store storage servers fail, error code: " + tracker.getErrorCode());
                }
                else
                {
                    Log.Error("store storage servers count: " + storageServers.Length);
                    for (int k = 0; k < storageServers.Length; k++)
                    {
                        Log.Error((k + 1) + ". " + storageServers[k].getInetSocketAddress().Address + ":" + storageServers[k].getInetSocketAddress().Port);
                    }
                    Log.Error("");
                }

                startTime = DateTime.Now.Ticks;
                results   = client.upload_file(file_buff, "txt", meta_list);
                Console.WriteLine("upload_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");

                /*
                 * group_name = "";
                 * results = client.upload_file(group_name, file_buff, "txt", meta_list);
                 */
                if (results == null)
                {
                    Log.Error("upload file fail, error code: " + client.getErrorCode());
                    return;
                }
                else
                {
                    group_name      = results[0];
                    remote_filename = results[1];
                    Log.Error("group_name: " + group_name + ", remote_filename: " + remote_filename);
                    Log.Error(client.get_file_info(group_name, remote_filename).ToString());

                    servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
                    if (servers == null)
                    {
                        Log.Error("get storage servers fail, error code: " + tracker.getErrorCode());
                    }
                    else
                    {
                        Log.Error("storage servers count: " + servers.Length);
                        for (int k = 0; k < servers.Length; k++)
                        {
                            Log.Error((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
                        }
                        Log.Error("");
                    }

                    meta_list    = new NameValuePair[4];
                    meta_list[0] = new NameValuePair("width", "1024");
                    meta_list[1] = new NameValuePair("heigth", "768");
                    meta_list[2] = new NameValuePair("bgcolor", "#000000");
                    meta_list[3] = new NameValuePair("title", "Untitle");

                    startTime = DateTime.Now.Ticks;
                    errno     = client.set_metadata(group_name, remote_filename, meta_list, (byte)ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE);
                    Console.WriteLine("set_metadata time used: " + (DateTime.Now.Ticks - startTime) + " ms");
                    if (errno == 0)
                    {
                        Log.Error("set_metadata success");
                    }
                    else
                    {
                        Log.Error("set_metadata fail, error no: " + errno);
                    }

                    meta_list = client.get_metadata(group_name, remote_filename);
                    if (meta_list != null)
                    {
                        for (int i = 0; i < meta_list.Length; i++)
                        {
                            Console.WriteLine(meta_list[i].getName() + " " + meta_list[i].getValue());
                        }
                    }

                    //Thread.sleep(30000);

                    startTime = DateTime.Now.Ticks;
                    file_buff = client.download_file(group_name, remote_filename);
                    Console.WriteLine("download_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");

                    if (file_buff != null)
                    {
                        Console.WriteLine("file Length:" + file_buff.Length);
                        Console.WriteLine(ClientGlobal.g_charset.GetString(file_buff));
                    }

                    file_buff       = ClientGlobal.g_charset.GetBytes("this is a slave buff");
                    master_filename = remote_filename;
                    prefix_name     = "-part1";
                    file_ext_name   = "txt";
                    startTime       = DateTime.Now.Ticks;
                    results         = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list);
                    Console.WriteLine("upload_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");
                    if (results != null)
                    {
                        Log.Error("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);

                        generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
                        if (generated_slave_filename != results[1])
                        {
                            Log.Error("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
                        }

                        Log.Error(client.get_file_info(results[0], results[1]).ToString());
                    }

                    startTime = DateTime.Now.Ticks;
                    errno     = client.delete_file(group_name, remote_filename);
                    Console.WriteLine("delete_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");
                    if (errno == 0)
                    {
                        Log.Error("Delete file success");
                    }
                    else
                    {
                        Log.Error("Delete file fail, error no: " + errno);
                    }
                }

                results = client.upload_file(local_filename, null, meta_list);
                if (results != null)
                {
                    string            file_id;
                    int               ts;
                    string            token;
                    string            file_url;
                    InetSocketAddress inetSockAddr;

                    group_name      = results[0];
                    remote_filename = results[1];
                    file_id         = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename;

                    inetSockAddr = trackerServer.getInetSocketAddress();
                    file_url     = "http://" + inetSockAddr.Address;
                    if (ClientGlobal.g_tracker_http_port != 80)
                    {
                        file_url += ":" + ClientGlobal.g_tracker_http_port;
                    }
                    file_url += "/" + file_id;
                    if (ClientGlobal.g_anti_steal_token)
                    {
                        ts        = (int)(DateTime.Now.Ticks / 1000);
                        token     = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key);
                        file_url += "?token=" + token + "&ts=" + ts;
                    }

                    Log.Error("group_name: " + group_name + ", remote_filename: " + remote_filename);
                    Log.Error(client.get_file_info(group_name, remote_filename).ToString());
                    Log.Error("file url: " + file_url);

                    errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.Replace("/", "_"));
                    if (errno == 0)
                    {
                        Log.Error("Download file success");
                    }
                    else
                    {
                        Log.Error("Download file fail, error no: " + errno);
                    }

                    errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.Replace("/", "-")));
                    if (errno == 0)
                    {
                        Log.Error("Download file success");
                    }
                    else
                    {
                        Log.Error("Download file fail, error no: " + errno);
                    }

                    master_filename = remote_filename;
                    prefix_name     = "-part2";
                    file_ext_name   = null;
                    startTime       = DateTime.Now.Ticks;
                    results         = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list);
                    Console.WriteLine("upload_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");
                    if (results != null)
                    {
                        Log.Error("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);

                        generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
                        if (generated_slave_filename != results[1])
                        {
                            Log.Error("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
                        }

                        Log.Error(client.get_file_info(results[0], results[1]).ToString());
                    }
                }

                System.IO.FileInfo f;
                f = new System.IO.FileInfo(local_filename);
                int nPos = local_filename.LastIndexOf('.');
                if (nPos > 0 && local_filename.Length - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
                {
                    file_ext_name = local_filename.Substring(nPos + 1);
                }
                else
                {
                    file_ext_name = null;
                }

                results = client.upload_file(null, f.Length,
                                             new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
                if (results != null)
                {
                    group_name      = results[0];
                    remote_filename = results[1];

                    Console.WriteLine("group name: " + group_name + ", remote filename: " + remote_filename);
                    Console.WriteLine(client.get_file_info(group_name, remote_filename));

                    master_filename = remote_filename;
                    prefix_name     = "-part3";
                    startTime       = DateTime.Now.Ticks;
                    results         = client.upload_file(group_name, master_filename, prefix_name, f.Length, new UploadLocalFileSender(local_filename), file_ext_name, meta_list);
                    Console.WriteLine("upload_file time used: " + (DateTime.Now.Ticks - startTime) + " ms");
                    if (results != null)
                    {
                        Log.Error("slave file group_name: " + results[0] + ", remote_filename: " + results[1]);

                        generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name);
                        if (generated_slave_filename != results[1])
                        {
                            Log.Error("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
                        }

                        Log.Error(client.get_file_info(results[0], results[1]).ToString());
                    }
                }
                else
                {
                    Log.Error("Upload file fail, error no: " + errno);
                }

                storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
                if (storageServer == null)
                {
                    Console.WriteLine("getFetchStorage fail, errno code: " + tracker.getErrorCode());
                    return;
                }
                /* for test only */
                Console.WriteLine("active test to storage server: " + storageServer.getConnection().activeTest());

                /* for test only */
                Console.WriteLine("active test to tracker server: " + trackerServer.getConnection().activeTest());
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message + ex.StackTrace);
            }
        }
예제 #2
0
        /**
         * entry point
         *
         * @param args comand arguments
         *             <ul><li>args[0]: config filename</li></ul>
         */
        public static void main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Error: Must have 1 parameter: config filename");
                return;
            }

            try
            {
                ClientGlobal.init(args[0]);
                Console.WriteLine("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
                Console.WriteLine("charset=" + ClientGlobal.g_charset);

                TrackerClient tracker = new TrackerClient();

                /*
                 * Console.WriteLine("delete storage return: " + tracker.deleteStorage("group1", "192.168.0.192"));
                 * Console.WriteLine("delete storage errno: " + tracker.getErrorCode());
                 */

                TrackerServer trackerServer = tracker.getTrackerServer();
                if (trackerServer == null)
                {
                    return;
                }

                int count;
                StructGroupStat[] groupStats = tracker.listGroups(trackerServer);
                if (groupStats == null)
                {
                    Console.WriteLine("");
                    Console.WriteLine("ERROR! list groups error, error no: " + tracker.getErrorCode());
                    Console.WriteLine("");
                    return;
                }

                Console.WriteLine("group count: " + groupStats.Length);

                count = 0;
                foreach (StructGroupStat groupStat in groupStats)
                {
                    count++;
                    Console.WriteLine("Group " + count + ":");
                    Console.WriteLine("group name = " + groupStat.getGroupName());
                    Console.WriteLine("disk total space = " + groupStat.getTotalMB() + "MB");
                    Console.WriteLine("disk free space = " + groupStat.getFreeMB() + " MB");
                    Console.WriteLine("trunk free space = " + groupStat.getTrunkFreeMB() + " MB");
                    Console.WriteLine("storage server count = " + groupStat.getStorageCount());
                    Console.WriteLine("active server count = " + groupStat.getActiveCount());
                    Console.WriteLine("storage server port = " + groupStat.getStoragePort());
                    Console.WriteLine("storage HTTP port = " + groupStat.getStorageHttpPort());
                    Console.WriteLine("store path count = " + groupStat.getStorePathCount());
                    Console.WriteLine("subdir count per path = " + groupStat.getSubdirCountPerPath());
                    Console.WriteLine("current write server index = " + groupStat.getCurrentWriteServer());
                    Console.WriteLine("current trunk file id = " + groupStat.getCurrentTrunkFileId());

                    StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName());
                    if (storageStats == null)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("ERROR! list storage error, error no: " + tracker.getErrorCode());
                        Console.WriteLine("");
                        break;
                    }

                    var format       = "yyyy-MM-dd HH:mm:ss";
                    int stroageCount = 0;
                    foreach (var storageStat in storageStats)
                    {
                        stroageCount++;
                        Console.WriteLine("\tStorage " + stroageCount + ":");
                        Console.WriteLine("\t\tstorage id = " + storageStat.getId());
                        Console.WriteLine("\t\tip_addr = " + storageStat.getIpAddr() + "  " + ProtoCommon.getStorageStatusCaption(storageStat.getStatus()));
                        Console.WriteLine("\t\thttp domain = " + storageStat.getDomainName());
                        Console.WriteLine("\t\tversion = " + storageStat.getVersion());
                        Console.WriteLine("\t\tjoin time = " + storageStat.getJoinTime().ToString(format));
                        Console.WriteLine("\t\tup time = " + (storageStat.getUpTime().Ticks == 0 ? "" : storageStat.getUpTime().ToString(format)));
                        Console.WriteLine("\t\ttotal storage = " + storageStat.getTotalMB() + "MB");
                        Console.WriteLine("\t\tfree storage = " + storageStat.getFreeMB() + "MB");
                        Console.WriteLine("\t\tupload priority = " + storageStat.getUploadPriority());
                        Console.WriteLine("\t\tstore_path_count = " + storageStat.getStorePathCount());
                        Console.WriteLine("\t\tsubdir_count_per_path = " + storageStat.getSubdirCountPerPath());
                        Console.WriteLine("\t\tstorage_port = " + storageStat.getStoragePort());
                        Console.WriteLine("\t\tstorage_http_port = " + storageStat.getStorageHttpPort());
                        Console.WriteLine("\t\tcurrent_write_path = " + storageStat.getCurrentWritePath());
                        Console.WriteLine("\t\tsource ip_addr = " + storageStat.getSrcIpAddr());
                        Console.WriteLine("\t\tif_trunk_server = " + storageStat.isTrunkServer());
                        Console.WriteLine("\t\tconntion.alloc_count  = " + storageStat.getConnectionAllocCount());
                        Console.WriteLine("\t\tconntion.current_count  = " + storageStat.getConnectionCurrentCount());
                        Console.WriteLine("\t\tconntion.max_count  = " + storageStat.getConnectionMaxCount());
                        Console.WriteLine("\t\ttotal_upload_count = " + storageStat.getTotalUploadCount());
                        Console.WriteLine("\t\tsuccess_upload_count = " + storageStat.getSuccessUploadCount());
                        Console.WriteLine("\t\ttotal_append_count = " + storageStat.getTotalAppendCount());
                        Console.WriteLine("\t\tsuccess_append_count = " + storageStat.getSuccessAppendCount());
                        Console.WriteLine("\t\ttotal_modify_count = " + storageStat.getTotalModifyCount());
                        Console.WriteLine("\t\tsuccess_modify_count = " + storageStat.getSuccessModifyCount());
                        Console.WriteLine("\t\ttotal_truncate_count = " + storageStat.getTotalTruncateCount());
                        Console.WriteLine("\t\tsuccess_truncate_count = " + storageStat.getSuccessTruncateCount());
                        Console.WriteLine("\t\ttotal_set_meta_count = " + storageStat.getTotalSetMetaCount());
                        Console.WriteLine("\t\tsuccess_set_meta_count = " + storageStat.getSuccessSetMetaCount());
                        Console.WriteLine("\t\ttotal_delete_count = " + storageStat.getTotalDeleteCount());
                        Console.WriteLine("\t\tsuccess_delete_count = " + storageStat.getSuccessDeleteCount());
                        Console.WriteLine("\t\ttotal_download_count = " + storageStat.getTotalDownloadCount());
                        Console.WriteLine("\t\tsuccess_download_count = " + storageStat.getSuccessDownloadCount());
                        Console.WriteLine("\t\ttotal_get_meta_count = " + storageStat.getTotalGetMetaCount());
                        Console.WriteLine("\t\tsuccess_get_meta_count = " + storageStat.getSuccessGetMetaCount());
                        Console.WriteLine("\t\ttotal_create_link_count = " + storageStat.getTotalCreateLinkCount());
                        Console.WriteLine("\t\tsuccess_create_link_count = " + storageStat.getSuccessCreateLinkCount());
                        Console.WriteLine("\t\ttotal_delete_link_count = " + storageStat.getTotalDeleteLinkCount());
                        Console.WriteLine("\t\tsuccess_delete_link_count = " + storageStat.getSuccessDeleteLinkCount());
                        Console.WriteLine("\t\ttotal_upload_bytes = " + storageStat.getTotalUploadBytes());
                        Console.WriteLine("\t\tsuccess_upload_bytes = " + storageStat.getSuccessUploadBytes());
                        Console.WriteLine("\t\ttotal_append_bytes = " + storageStat.getTotalAppendBytes());
                        Console.WriteLine("\t\tsuccess_append_bytes = " + storageStat.getSuccessAppendBytes());
                        Console.WriteLine("\t\ttotal_modify_bytes = " + storageStat.getTotalModifyBytes());
                        Console.WriteLine("\t\tsuccess_modify_bytes = " + storageStat.getSuccessModifyBytes());
                        Console.WriteLine("\t\ttotal_download_bytes = " + storageStat.getTotalDownloadloadBytes());
                        Console.WriteLine("\t\tsuccess_download_bytes = " + storageStat.getSuccessDownloadloadBytes());
                        Console.WriteLine("\t\ttotal_sync_in_bytes = " + storageStat.getTotalSyncInBytes());
                        Console.WriteLine("\t\tsuccess_sync_in_bytes = " + storageStat.getSuccessSyncInBytes());
                        Console.WriteLine("\t\ttotal_sync_out_bytes = " + storageStat.getTotalSyncOutBytes());
                        Console.WriteLine("\t\tsuccess_sync_out_bytes = " + storageStat.getSuccessSyncOutBytes());
                        Console.WriteLine("\t\ttotal_file_open_count = " + storageStat.getTotalFileOpenCount());
                        Console.WriteLine("\t\tsuccess_file_open_count = " + storageStat.getSuccessFileOpenCount());
                        Console.WriteLine("\t\ttotal_file_read_count = " + storageStat.getTotalFileReadCount());
                        Console.WriteLine("\t\tsuccess_file_read_count = " + storageStat.getSuccessFileReadCount());
                        Console.WriteLine("\t\ttotal_file_write_count = " + storageStat.getTotalFileWriteCount());
                        Console.WriteLine("\t\tsuccess_file_write_count = " + storageStat.getSuccessFileWriteCount());
                        Console.WriteLine("\t\tlast_heart_beat_time = " + storageStat.getLastHeartBeatTime().ToString(format));
                        Console.WriteLine("\t\tlast_source_update = " + storageStat.getLastSourceUpdate().ToString(format));
                        Console.WriteLine("\t\tlast_sync_update = " + storageStat.getLastSyncUpdate().ToString(format));
                        Console.WriteLine("\t\tlast_synced_timestamp = " + storageStat.getLastSyncedTimestamp().ToString(format) + getSyncedDelayString(storageStats, storageStat));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message + ex.StackTrace);
            }
        }