/// <summary>
        /// receive pack header
        /// </summary>
        /// <param name="in">input stream</param>
        /// <param name="expect_cmd">expect response command</param>
        /// <param name="expect_body_len">expect response package body length</param>
        /// <returns> RecvHeaderInfo: errno and pkg body length</returns>
        public static RecvHeaderInfo recvHeader(Stream stream, byte expect_cmd, long expect_body_len)
        {
            byte[] header;
            int    bytes;
            long   pkg_len;

            header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
            if ((bytes = stream.Read(header, 0, header.Length)) != header.Length)
            {
                throw new IOException("recv package size " + bytes + " != " + header.Length);
            }
            if (header[PROTO_HEADER_CMD_INDEX] != expect_cmd)
            {
                throw new IOException("recv cmd: " + header[PROTO_HEADER_CMD_INDEX] + " is not correct, expect cmd: " + expect_cmd);
            }
            if (header[PROTO_HEADER_STATUS_INDEX] != 0)
            {
                return(new RecvHeaderInfo(header[PROTO_HEADER_STATUS_INDEX], 0));
            }
            pkg_len = ProtoCommon.buff2long(header, 0);
            if (pkg_len < 0)
            {
                throw new IOException("recv body length: " + pkg_len + " < 0!");
            }
            if (expect_body_len >= 0 && pkg_len != expect_body_len)
            {
                throw new IOException("recv body length: " + pkg_len + " is not correct, expect length: " + expect_body_len);
            }
            return(new RecvHeaderInfo(0, pkg_len));
        }
        /// <summary>
        /// pack header by FastDFS transfer protocol
        /// </summary>
        /// <param name="cmd">which command to send</param>
        /// <param name="pkg_len">package body length</param>
        /// <param name="errno">status code, should be (byte)0</param>
        /// <returns> packed byte buffer</returns>
        public static byte[] packHeader(byte cmd, long pkg_len, byte errno)
        {
            byte[] header;
            byte[] hex_len;
            header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
            Arrays.fill(header, (byte)0);

            hex_len = ProtoCommon.long2buff(pkg_len);
            Array.Copy(hex_len, 0, header, 0, hex_len.Length);
            header[PROTO_HEADER_CMD_INDEX]    = cmd;
            header[PROTO_HEADER_STATUS_INDEX] = errno;
            return(header);
        }
        /**
         * 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);
            }
        }
 protected DateTime dateValue(byte[] bs, int offset, FieldInfo filedInfo)
 {
     return(Dates.Get(ProtoCommon.buff2long(bs, offset + filedInfo.offset) * 1000));
 }
 protected int intValue(byte[] bs, int offset, FieldInfo filedInfo)
 {
     return((int)ProtoCommon.buff2long(bs, offset + filedInfo.offset));
 }
 protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo)
 {
     return(ProtoCommon.buff2int(bs, offset + filedInfo.offset));
 }
Esempio n. 7
0
        /// <summary>
        /// query storage server to upload file
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <param name="groupName">the group name to upload file to, can be empty</param>
        /// <returns> storage server object, return null if fail</returns>
        public StorageServer getStoreStorage(TrackerServer trackerServer, string groupName)
        {
            byte[]     header;
            string     ip_addr;
            int        port;
            byte       cmd;
            int        out_len;
            byte       store_path;
            Connection connection;

            if (trackerServer == null)
            {
                trackerServer = getTrackerServer();
            }
            connection = trackerServer.getConnection();
            var outStream = connection.getOutputStream();

            try
            {
                if (groupName == null || groupName.Length == 0)
                {
                    cmd     = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE;
                    out_len = 0;
                }
                else
                {
                    cmd     = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE;
                    out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                header = ProtoCommon.packHeader(cmd, out_len, (byte)0);
                outStream.Write(header, 0, header.Length);
                if (groupName != null && groupName.Length > 0)
                {
                    byte[] bGroupName;
                    byte[] bs;
                    int    group_len;
                    bs         = ClientGlobal.g_charset.GetBytes(groupName);
                    bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
                    if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                    {
                        group_len = bs.Length;
                    }
                    else
                    {
                        group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                    }
                    Arrays.fill(bGroupName, (byte)0);
                    Array.Copy(bs, 0, bGroupName, 0, group_len);
                    outStream.Write(bGroupName, 0, bGroupName.Length);
                }
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP,
                                                                              ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }
                ip_addr = Strings.Get(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE - 1).Trim();
                port    = (int)ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN
                                                     + ProtoCommon.FDFS_IPADDR_SIZE - 1);
                store_path = pkgInfo.body[ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN - 1];
                return(new StorageServer(ip_addr, port, store_path));
            }
            catch (IOException ex)
            {
                try
                {
                    connection.close();
                }
                catch (IOException ex1)
                {
                    throw ex1;
                }
                finally
                {
                    connection = null;
                }
                throw ex;
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.release();
                    }
                    catch
                    {
                    }
                }
            }
        }
        /**
         * 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);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// delete a storage server from the tracker server
        /// </summary>
        /// <param name="trackerServer">the connected tracker server</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>
        private bool deleteStorage(TrackerServer trackerServer,
                                   string groupName, string storageIpAddr)
        {
            byte[]     header;
            byte[]     bGroupName;
            byte[]     bs;
            int        len;
            Connection connection = null;

            try
            {
                connection = trackerServer.getConnection();
                Stream outStream = connection.getOutputStream();
                bs         = ClientGlobal.g_charset.GetBytes(groupName);
                bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
                if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                {
                    len = bs.Length;
                }
                else
                {
                    len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                Arrays.fill(bGroupName, (byte)0);
                Array.Copy(bs, 0, bGroupName, 0, len);
                int    ipAddrLen;
                byte[] bIpAddr = ClientGlobal.g_charset.GetBytes(storageIpAddr);
                if (bIpAddr.Length < ProtoCommon.FDFS_IPADDR_SIZE)
                {
                    ipAddrLen = bIpAddr.Length;
                }
                else
                {
                    ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
                }
                header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte)0);
                byte[] wholePkg = new byte[header.Length + bGroupName.Length + ipAddrLen];
                Array.Copy(header, 0, wholePkg, 0, header.Length);
                Array.Copy(bGroupName, 0, wholePkg, header.Length, bGroupName.Length);
                Array.Copy(bIpAddr, 0, wholePkg, header.Length + bGroupName.Length, ipAddrLen);
                outStream.Write(wholePkg, 0, wholePkg.Length);
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, 0);
                this.errno = pkgInfo.errno;
                return(pkgInfo.errno == 0);
            }
            catch (IOException e)
            {
                try
                {
                    connection.close();
                }
                finally
                {
                    connection = null;
                }
                throw e;
            }
            finally
            {
                if (connection != null)
                {
                    connection.release();
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// query storage server stat info of the group
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <param name="groupName">the group name of storage server</param>
        /// <param name="storageIpAddr">the storage server ip address, can be null or empty</param>
        /// <returns> storage server stat array, return null if fail</returns>
        public StructStorageStat[] listStorages(TrackerServer trackerServer,
                                                string groupName, string storageIpAddr)
        {
            byte[]     header;
            byte[]     bGroupName;
            byte[]     bs;
            int        len;
            Connection connection;

            if (trackerServer == null)
            {
                trackerServer = getTrackerServer();
                if (trackerServer == null)
                {
                    return(null);
                }
            }
            connection = trackerServer.getConnection();
            Stream outStream = connection.getOutputStream();

            try
            {
                bs         = ClientGlobal.g_charset.GetBytes(groupName);
                bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
                if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                {
                    len = bs.Length;
                }
                else
                {
                    len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                Arrays.fill(bGroupName, (byte)0);
                Array.Copy(bs, 0, bGroupName, 0, len);
                int    ipAddrLen;
                byte[] bIpAddr;
                if (storageIpAddr != null && storageIpAddr.Length > 0)
                {
                    bIpAddr = ClientGlobal.g_charset.GetBytes(storageIpAddr);
                    if (bIpAddr.Length < ProtoCommon.FDFS_IPADDR_SIZE)
                    {
                        ipAddrLen = bIpAddr.Length;
                    }
                    else
                    {
                        ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1;
                    }
                }
                else
                {
                    bIpAddr   = null;
                    ipAddrLen = 0;
                }
                header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte)0);
                byte[] wholePkg = new byte[header.Length + bGroupName.Length + ipAddrLen];
                Array.Copy(header, 0, wholePkg, 0, header.Length);
                Array.Copy(bGroupName, 0, wholePkg, header.Length, bGroupName.Length);
                if (ipAddrLen > 0)
                {
                    Array.Copy(bIpAddr, 0, wholePkg, header.Length + bGroupName.Length, ipAddrLen);
                }
                outStream.Write(wholePkg, 0, wholePkg.Length);
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }
                ProtoStructDecoder <StructStorageStat> decoder = new ProtoStructDecoder <StructStorageStat>();
                return(decoder.decode(pkgInfo.body, StructStorageStat.getFieldsTotalSize()));
            }
            catch (IOException ex)
            {
                try
                {
                    connection.close();
                }
                catch (IOException ex1)
                {
                    throw ex1;
                }

                throw ex;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                this.errno = ProtoCommon.ERR_NO_EINVAL;
                return(null);
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.release();
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// list groups
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <returns> group stat array, return null if fail</returns>
        public StructGroupStat[] listGroups(TrackerServer trackerServer)
        {
            byte[] header;
            //string ip_addr;
            //int port;
            //byte cmd;
            //int out_len;
            //byte store_path;
            Connection connection;

            if (trackerServer == null)
            {
                trackerServer = getTrackerServer();
                if (trackerServer == null)
                {
                    return(null);
                }
            }
            connection = trackerServer.getConnection();
            Stream outStream = connection.getOutputStream();

            try
            {
                header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte)0);
                outStream.Write(header, 0, header.Length);
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }
                ProtoStructDecoder <StructGroupStat> decoder = new ProtoStructDecoder <StructGroupStat>();
                return(decoder.decode(pkgInfo.body, StructGroupStat.getFieldsTotalSize()));
            }
            catch (IOException ex)
            {
                try
                {
                    connection.close();
                }
                catch (IOException ex1)
                {
                    throw ex1;
                }
                finally
                {
                    connection = null;
                }
                throw ex;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                this.errno = ProtoCommon.ERR_NO_EINVAL;
                return(null);
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.release();
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// query storage server to download file
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <param name="cmd">command code, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE orProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE</param>
        /// <param name="groupName">the group name of storage server</param>
        /// <param name="filename">filename on storage server</param>
        /// <returns> storage server JavaSocket object, return null if fail</returns>
        protected ServerInfo[] getStorages(TrackerServer trackerServer,
                                           byte cmd, string groupName, string filename)
        {
            byte[]     header;
            byte[]     bFileName;
            byte[]     bGroupName;
            byte[]     bs;
            int        len;
            string     ip_addr;
            int        port;
            Connection connection;

            if (trackerServer == null)
            {
                trackerServer = getTrackerServer();
                if (trackerServer == null)
                {
                    return(null);
                }
            }
            connection = trackerServer.getConnection();
            Stream outStream = connection.getOutputStream();

            try
            {
                bs         = ClientGlobal.g_charset.GetBytes(groupName);
                bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
                bFileName  = ClientGlobal.g_charset.GetBytes(filename);
                if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                {
                    len = bs.Length;
                }
                else
                {
                    len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                Arrays.fill(bGroupName, (byte)0);
                Array.Copy(bs, 0, bGroupName, 0, len);
                header = ProtoCommon.packHeader(cmd, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + bFileName.Length, (byte)0);
                byte[] wholePkg = new byte[header.Length + bGroupName.Length + bFileName.Length];
                Array.Copy(header, 0, wholePkg, 0, header.Length);
                Array.Copy(bGroupName, 0, wholePkg, header.Length, bGroupName.Length);
                Array.Copy(bFileName, 0, wholePkg, header.Length + bGroupName.Length, bFileName.Length);
                outStream.Write(wholePkg, 0, wholePkg.Length);
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }
                if (pkgInfo.body.Length < ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN)
                {
                    throw new IOException("Invalid body length: " + pkgInfo.body.Length);
                }
                if ((pkgInfo.body.Length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) % (ProtoCommon.FDFS_IPADDR_SIZE - 1) != 0)
                {
                    throw new IOException("Invalid body length: " + pkgInfo.body.Length);
                }
                int server_count = 1 + (pkgInfo.body.Length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) / (ProtoCommon.FDFS_IPADDR_SIZE - 1);
                ip_addr = Strings.Get(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE - 1).Trim();
                int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ProtoCommon.FDFS_IPADDR_SIZE - 1;
                port    = (int)ProtoCommon.buff2long(pkgInfo.body, offset);
                offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
                ServerInfo[] servers = new ServerInfo[server_count];
                servers[0] = new ServerInfo(ip_addr, port);
                for (int i = 1; i < server_count; i++)
                {
                    servers[i] = new ServerInfo(Strings.Get(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).Trim(), port);
                    offset    += ProtoCommon.FDFS_IPADDR_SIZE - 1;
                }
                return(servers);
            }
            catch (IOException ex)
            {
                try
                {
                    connection.close();
                }
                catch (IOException ex1)
                {
                    throw ex1;
                }
                finally
                {
                    connection = null;
                }

                throw ex;
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.release();
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 13
0
        /// <summary>
        /// query storage servers to upload file
        /// </summary>
        /// <param name="trackerServer">the tracker server</param>
        /// <param name="groupName">the group name to upload file to, can be empty</param>
        /// <returns> storage servers, return null if fail</returns>
        public StorageServer[] getStoreStorages(TrackerServer trackerServer, string groupName)
        {
            byte[]     header;
            string     ip_addr;
            int        port;
            byte       cmd;
            int        out_len;
            Connection connection;

            if (trackerServer == null)
            {
                trackerServer = getTrackerServer();
                if (trackerServer == null)
                {
                    return(null);
                }
            }
            connection = trackerServer.getConnection();
            Stream outStream = connection.getOutputStream();

            try
            {
                if (groupName == null || groupName.Length == 0)
                {
                    cmd     = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL;
                    out_len = 0;
                }
                else
                {
                    cmd     = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL;
                    out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                }
                header = ProtoCommon.packHeader(cmd, out_len, (byte)0);
                outStream.Write(header, 0, header.Length);
                if (groupName != null && groupName.Length > 0)
                {
                    byte[] bGroupName;
                    byte[] bs;
                    int    group_len;
                    bs         = ClientGlobal.g_charset.GetBytes(groupName);
                    bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
                    if (bs.Length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
                    {
                        group_len = bs.Length;
                    }
                    else
                    {
                        group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                    }
                    Arrays.fill(bGroupName, (byte)0);
                    Array.Copy(bs, 0, bGroupName, 0, group_len);
                    outStream.Write(bGroupName, 0, bGroupName.Length);
                }
                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(connection.getInputStream(),
                                                                              ProtoCommon.TRACKER_PROTO_CMD_RESP, -1);
                this.errno = pkgInfo.errno;
                if (pkgInfo.errno != 0)
                {
                    return(null);
                }
                if (pkgInfo.body.Length < ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN)
                {
                    this.errno = ProtoCommon.ERR_NO_EINVAL;
                    return(null);
                }
                int       ipPortLen    = pkgInfo.body.Length - (ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1);
                const int recordLength = ProtoCommon.FDFS_IPADDR_SIZE - 1 + ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
                if (ipPortLen % recordLength != 0)
                {
                    this.errno = ProtoCommon.ERR_NO_EINVAL;
                    return(null);
                }
                int serverCount = ipPortLen / recordLength;
                if (serverCount > 16)
                {
                    this.errno = ProtoCommon.ERR_NO_ENOSPC;
                    return(null);
                }
                StorageServer[] results    = new StorageServer[serverCount];
                byte            store_path = pkgInfo.body[pkgInfo.body.Length - 1];
                int             offset     = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN;
                for (int i = 0; i < serverCount; i++)
                {
                    ip_addr    = Strings.Get(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).Trim();
                    offset    += ProtoCommon.FDFS_IPADDR_SIZE - 1;
                    port       = (int)ProtoCommon.buff2long(pkgInfo.body, offset);
                    offset    += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE;
                    results[i] = new StorageServer(ip_addr, port, store_path);
                }
                return(results);
            }
            catch (IOException ex)
            {
                try
                {
                    connection.close();
                }
                catch (IOException ex1)
                {
                    throw ex1;
                }
                finally
                {
                    connection = null;
                }
                throw ex;
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.release();
                    }
                    catch
                    {
                    }
                }
            }
        }