Exemplo n.º 1
0
        /**
         * query storage servers to upload file
         *
         * @param trackerServer the tracker server
         * @param groupName     the group name to upload file to, can be empty
         * @return storage servers, return null if fail
         */
        public StorageServer[] getStoreStorages(TrackerServer trackerServer, String groupName)
        {
            byte[] header;
            String ip_addr;
            int    port;
            byte   cmd;
            int    out_len;
            bool   bNewConnection;
            Socket trackerSocket;

            if (trackerServer == null)
            {
                trackerServer = getConnection();
                if (trackerServer == null)
                {
                    return(null);
                }

                bNewConnection = true;
            }
            else
            {
                bNewConnection = false;
            }

            trackerSocket = trackerServer.getSocket();
            var outputStream = new NetworkStream(trackerSocket);

            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);
                outputStream.Write(header, 0, header.Length);
                var encoder = Encoding.GetEncoding(ClientGlobal.g_charset);
                if (groupName != null && groupName.Length > 0)
                {
                    byte[] bGroupName;
                    byte[] bs;
                    int    group_len;

                    bs         = encoder.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;
                    }

                    bGroupName.Fill <byte>(0);
                    Array.Copy(bs, 0, bGroupName, 0, group_len);
                    outputStream.Write(bGroupName, 0, bGroupName.Length);
                }


                ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(outputStream,
                                                                              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);
                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 = Encoding.ASCII.GetString(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).Trim(
                        '\0');
                    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)
            {
                if (!bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }

                throw ex;
            }
            finally
            {
                outputStream.Close();
                if (bNewConnection)
                {
                    try
                    {
                        trackerServer.close();
                    }
                    catch (IOException ex1)
                    {
                    }
                }
            }
        }
Exemplo n.º 2
0
 /**
  * constructor
  *
  * @param trackerServer the tracker server, can be null
  * @param storageServer the storage server, can be null
  */
 public StorageClient1(TrackerServer trackerServer, StorageServer storageServer)
     : base(trackerServer, storageServer)
 {
 }