cosfiletransfer_filetransfersession_impl.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,040 行 · 第 1/3 页
ERL
1,040 行
%%----------------------------------------------------------------------%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %% $Id$%% %%----------------------------------------------------------------------%% File : CosFileTransfer_FileTransferSession_impl.erl%% Description : %%%% Created : 12 Sept 2000%%-----------------------------------------------------------------------module('CosFileTransfer_FileTransferSession_impl').%%----------------------------------------------------------------------%% Include files%%-----------------------------------------------------------------------include_lib("orber/include/corba.hrl").-include_lib("orber/src/orber_iiop.hrl").-include("cosFileTransferApp.hrl").%%----------------------------------------------------------------------%% External exports%%-----------------------------------------------------------------------export([init/1, terminate/2, code_change/3, handle_info/2]).%% Interface functions-export(['_get_protocols_supported'/2, set_directory/3, create_file/3, create_directory/3, get_file/3, delete/3, transfer/4, append/4, insert/5, logout/2]).%%----------------------------------------------------------------------%% Internal exports%%-----------------------------------------------------------------------export([oe_orber_create_directory_current/2, oe_orber_get_content/4, oe_orber_count_children/3]).-export([invoke_call/3]).%%----------------------------------------------------------------------%% Records%%-----------------------------------------------------------------------record(state, {protocols, server, type, current, module, connection, mytype, connection_timeout}).%%----------------------------------------------------------------------%% Macros%%-----------------------------------------------------------------------define(create_InitState(P, S, T, C, M, Co, Ty, CT), #state{protocols=P, server=S, type=T, current=C, module=M, connection=Co, mytype=Ty, connection_timeout=CT}).-define(get_Protocols(S), S#state.protocols).-define(get_Server(S), S#state.server).-define(get_CurrentDir(S), S#state.current).-define(get_Module(S), S#state.module).-define(get_Connection(S), S#state.connection).-define(get_MyType(S), S#state.mytype).-define(get_ConnectionTimeout(S), S#state.connection_timeout).-define(set_CurrentDir(S, C), S#state{current=C}).-define(is_FTP(S), S#state.type=='FTP').-define(is_FTAM(S), S#state.type=='FTAM').-define(is_NATIVE(S), S#state.type=='NATIVE').-define(is_ORBER_NATIVE(S), S#state.module==cosFileTransferNATIVE_file).%%======================================================================%% External functions%%======================================================================%%----------------------------------------------------------------------%% Function : init/1%% Returns : {ok, State} |%% {ok, State, Timeout} |%% ignore |%% {stop, Reason}%% Description: Initiates the server%%----------------------------------------------------------------------init(['FTP', Host, Port, User, Password, _Account, Protocol, Timeout]) -> {ok, Pid} = ftp:open(Host, Port, []), ok = ftp:user(Pid, User, Password), {ok, PWD} = ftp:pwd(Pid), {Connection, ProtocolSupport} = setup_local(Protocol), {ok, ?create_InitState(ProtocolSupport, Pid, 'FTP', PWD, ftp, Connection, Protocol, Timeout)};init([{'NATIVE', Mod}, Host, Port, User, Password, _Account, Protocol, Timeout]) -> {ok, Pid} = Mod:open(Host, Port), ok = Mod:user(Pid, User, Password), {ok, PWD} = Mod:pwd(Pid), {Connection, ProtocolSupport} = setup_local(Protocol), {ok, ?create_InitState(ProtocolSupport, Pid, 'NATIVE', PWD, Mod, Connection, Protocol, Timeout)}. %%----------------------------------------------------------------------%% Function : terminate/2%% Returns : any (ignored by gen_server)%% Description: Shutdown the server%%----------------------------------------------------------------------terminate(_Reason, State) -> case ?get_MyType(State) of ssl -> catch ssl:close(?get_Connection(State)); _ -> catch gen_tcp:close(?get_Connection(State)) end, ok.%%----------------------------------------------------------------------%% Function : code_change/3%% Returns : {ok, NewState}%% Description: Convert process state when code is changed%%----------------------------------------------------------------------code_change(_OldVsn, State, _Extra) -> {ok, State}.%%---------------------------------------------------------------------%%% function : handle_info/2%% Arguments: %% Returns : %% Effect : %%----------------------------------------------------------------------handle_info(Info, State) -> case Info of {'EXIT', _Pid, Reason} -> {stop, Reason, State}; _Other -> {noreply, State} end. %%======================================================================%% CosFileTransfer::FileTransferSession%%======================================================================%%---------------------------------------------------------------------%%% Function : _get_protocols_supported%% Arguments : %% Returns : A list of CosFileTransfer::ProtocolSupport, i.e.,%% struct ProtocolSupport { %% Istring protocol_name; %% ProtocolAddressList addresses; %% eq a list of strings.%% }; %% Description: %%----------------------------------------------------------------------'_get_protocols_supported'(_OE_This, State) -> {reply, ?get_Protocols(State), State}.%%----------------------------------------------------------------------%% Function : set_directory%% Arguments : Directory - CosFileTransfer::Directory%% Returns : %% Description: %%----------------------------------------------------------------------set_directory(_OE_This, State, Directory) when ?is_FTP(State); ?is_NATIVE(State) -> Mod = ?get_Module(State), Path = filename:join('CosFileTransfer_Directory': '_get_complete_file_name'(Directory)), case catch Mod:cd(?get_Server(State), Path) of ok -> {reply, ok, ?set_CurrentDir(State, Path)}; {error, epath} -> corba:raise(#'CosFileTransfer_FileNotFoundException' {reason="Directory not found."}); {error, elogin} -> corba:raise(#'CosFileTransfer_SessionException' {reason="User not loggen in."}); {error, econn} -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason="Premature connection ending."}); _ -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason = "Unexpected error."}) end.%%----------------------------------------------------------------------%% Function : create_file%% Arguments : FileNameList%% Returns : File%% Description: This operation creates a File Object representing a %% file which may or may not exist. Typically used as%% argument when invoking transfer/3. See also get_file/2.%%----------------------------------------------------------------------create_file(OE_This, State, FileNameList) -> {reply, cosFileTransferApp:create_file(OE_This, FileNameList), State}.%%----------------------------------------------------------------------%% Function : create_directory%% Arguments : FileNameList - full path name.%% Returns : Directory%% Description: %%----------------------------------------------------------------------create_directory(OE_This, State, FileNameList) when ?is_FTP(State); ?is_NATIVE(State) -> Mod = ?get_Module(State), case Mod:mkdir(?get_Server(State), filename:join(FileNameList)) of ok -> {reply, cosFileTransferApp:create_dir(OE_This, FileNameList), State}; {error, epath} -> corba:raise(#'CosFileTransfer_FileNotFoundException' {reason="Directory not found."}); {error, elogin} -> corba:raise(#'CosFileTransfer_SessionException' {reason="User not loggen in."}); {error, econn} -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason="Premature connection ending."}); _ -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason="Unknown error."}) end.%%----------------------------------------------------------------------%% Function : get_file%% Arguments : FileNameList%% Returns : FileWrapper%% Description: This operation should be independent of the working Directory,%% i.e., a full path name must be supplied. The file or %% directory the returned object is supposed to represent%% MUST(!!!!) exist.%%----------------------------------------------------------------------get_file(OE_This, State, FileNameList) when ?is_FTP(State); ?is_NATIVE(State) -> case check_type(OE_This, State, filename:join(FileNameList)) of {ndirectory, _Listing} -> {reply, #'CosFileTransfer_FileWrapper'{the_file = cosFileTransferApp: create_dir(OE_This, FileNameList), file_type = ndirectory}, State}; nfile -> {reply, #'CosFileTransfer_FileWrapper'{the_file = cosFileTransferApp: create_file(OE_This, FileNameList), file_type = nfile}, State}; Other -> %% If we want to return {stop, ....} Other end.%%----------------------------------------------------------------------%% Function : delete%% Arguments : File%% Returns : -%% Description: %%----------------------------------------------------------------------delete(_OE_This, State, File) when ?is_FTP(State); ?is_NATIVE(State) -> Mod = ?get_Module(State), Result = case 'CosPropertyService_PropertySet': get_property_value(File, "is_directory") of #any{value=false} -> Mod:delete(?get_Server(State), filename:join('CosFileTransfer_File': '_get_complete_file_name'(File))); #any{value=true} -> Mod:rmdir(?get_Server(State), filename:join('CosFileTransfer_File': '_get_complete_file_name'(File))); Other -> Other end, case Result of ok -> {reply, ok, State}; {error, epath} -> corba:raise(#'CosFileTransfer_FileNotFoundException' {reason="File or Directory not found."}); {error, elogin} -> corba:raise(#'CosFileTransfer_SessionException' {reason="User not loggen in."}); {error, econn} -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason="Premature connection ending."}); _ -> corba:raise(#'CosFileTransfer_RequestFailureException' {reason="Unknown error."}) end.%%----------------------------------------------------------------------%% Function : transfer%% Arguments : SrcFile eq DestFile eq CosFileTransfer::File%% Returns : -%% Description: DestFile must be a newly created File object, using create_file()%% on the Target FileTransferSession, prior to calling transfer().%%----------------------------------------------------------------------transfer(OE_This, State, SrcFile, DestFile) when ?is_ORBER_NATIVE(State) -> case which_FTS_type(OE_This, SrcFile, DestFile) of {source, TargetFTS} -> %% The source FTS is supposed to be the active one, set up a connection. Protocols = 'CosFileTransfer_FileTransferSession': '_get_protocols_supported'(TargetFTS), SrcName = 'CosFileTransfer_File':'_get_complete_file_name'(SrcFile), Pid = spawn(?MODULE, invoke_call, [self(), transfer, [TargetFTS, SrcFile, DestFile]]), send_file(Protocols, ?get_MyType(State), ?get_ConnectionTimeout(State), filename:join(SrcName)), check_reply(Pid), {reply, ok, State}; {target, _SourceFTS} -> DestName = 'CosFileTransfer_File':'_get_complete_file_name'(DestFile), receive_file(?get_MyType(State), ?get_Connection(State), ?get_ConnectionTimeout(State), filename:join(DestName), write), {reply, ok, State} end;transfer(OE_This, State, SrcFile, DestFile) when ?is_FTP(State); ?is_NATIVE(State) -> case which_FTS_type(OE_This, SrcFile, DestFile) of {source, TargetFTS} -> source_FTS_operation(State, SrcFile, DestFile, transfer, 0, TargetFTS); {target, _SourceFTS} -> target_FTS_operation(State, SrcFile, DestFile, send, 0) end.%%----------------------------------------------------------------------%% Function : append%% Arguments : SrcFile eq DestFile eq CosFileTransfer::File%% Returns : -%% Description:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?