📄 filemessageprocessor.java
字号:
/*
* @(#)FileMessageProcessor.java
*
* Copyright (c) 2001-2002, JangHo Hwang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the JangHo Hwang nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: FileMessageProcessor.java,v 1.3 2002/06/05 09:34:58 xrath Exp $
*/
package rath.msnm.ftp;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import rath.msnm.SwitchboardSession;
import rath.msnm.msg.IncomingMessage;
import rath.msnm.msg.MimeMessage;
import rath.msnm.msg.FileTransferMessage;
/**
* 颇老 价荐脚俊 荤侩登绰 阿 飘罚黎记甸阑 包府窍绊 牢刘俊 包访等 Cookie唱
* 角力肺 颇老阑 傈价窍绰 thread甫 剁快绰殿狼 开且阑 葛滴 贸府窍绰 努贰胶捞促.
* <p>
* 酒贰绰 捞 努贰胶啊 且 荐 乐绰 老阑 唱凯茄促.
* <ul>
*
* </ul>
*
* @author Jang-Ho Hwang, rath@linuxkorea.co.kr
* @version $Id: FileMessageProcessor.java,v 1.3 2002/06/05 09:34:58 xrath Exp $
*/
public class FileMessageProcessor
{
private static boolean _auto = true;
private SwitchboardSession session = null;
private Hashtable toSendFileMap = new Hashtable();
private Hashtable toReceiveFileMap = new Hashtable();
/**
+* session俊辑 老绢唱绰 葛电 File俊 傈价俊 包访等 皋矫瘤甫
* 贸府窍绊 包府窍绰 按眉甫 积己茄促.
*/
public FileMessageProcessor( SwitchboardSession session )
{
this.session = session;
}
/**
* 颇老 价荐脚俊 包访等 皋矫瘤甸阑 贸府窍绰 葛电 皋家靛甸俊 措茄
* Entry point啊 登绰 皋家靛捞促.
*
* @param msg 惑措规栏肺何磐 罐篮 皋矫瘤
* @param mime 角力 颇老傈价俊 包访等 郴侩捞 淬变 Mime 皋矫瘤肺 FileTransferMessage捞促.
* 窜, FileTransferMessage狼 牢胶畔胶绰 酒聪骨肺 cast绰 且 荐 绝促.
*/
public void processMessage( IncomingMessage msg, MimeMessage mime )
{
// mime.getProperties().list( System.out );
String command = mime.getProperty("Invitation-Command");
if( command==null || !mime.hasProperty("Invitation-Cookie") )
return;
boolean isLastInTransaction = mime.hasProperty("AuthCookie");
try
{
if( command.equals("INVITE") )
{
processInvite(mime);
}
else
if( command.equals("ACCEPT") )
{
if( !isLastInTransaction )
processAccept(mime);
else
processDownload(mime);
}
else
if( command.equals("CANCEL") )
{
processCancel(mime);
}
}
catch( IOException e )
{
e.printStackTrace();
}
catch( Throwable e )
{
e.printStackTrace();
System.err.println( "MIME message violation: FileTransfer(" +e+ ")" );
}
}
/**
* 惑措规栏肺何磐 档馒茄 颇老傈价 夸没阑 贸府茄促.
* 捞 皋家靛俊辑绰 MIME 皋矫瘤俊辑 鞘夸茄 沥焊父阑 榜扼 MsnFileEvent甫 惯价窍绰 老阑 茄促.
* 父距, ${@link #setAutoReceive setAutoReceive} 皋家靛肺 磊悼荐脚 可记阑 true肺 秦初疽促搁
* 捞亥飘甫 惯价窍瘤 臼绊 梆官肺 荐脚倾遏 皋矫瘤甫 价脚茄促.
* <p>
* 磊悼 荐脚葛靛啊 汲沥登绢乐阑 版快, 荐脚登绰 颇老篮 扁夯利栏肺 current directory俊
* 历厘瞪 巴捞促. 父距 捞巴阑 荐沥窍绊 酵促搁, System property狼
* <b>msnm.file.download.dir</b>俊 罐绊磊窍绰 叼泛配府甫 汲沥窍搁 等促.
*/
protected void processInvite( MimeMessage mime ) throws IOException
{
String cookie = mime.getProperty("Invitation-Cookie");
int cookieInt = Integer.parseInt(cookie);
String filename = mime.getProperty("Application-File");
int filesize = Integer.parseInt(mime.getProperty("Application-FileSize"));
if( _auto )
{
session.acceptFileReceive(cookieInt,
new File(System.getProperty("msnm.file.download.dir","."), filename));
return;
}
session.msn.fireFilePostedEvent( session, cookieInt, filename, filesize );
}
/**
* 颇老 磊悼荐脚 葛靛甫 汲沥茄促.
*/
public static void setAutoReceive( boolean auto )
{
_auto = auto;
}
/**
* 颇老 磊悼荐脚 葛靛牢瘤 犬牢茄促. default蔼篮 true捞促.
*/
public static boolean isAutoReceive()
{
return _auto;
}
/**
* 颇老 价脚 夸没阑 沁促绰 荤角阑 捞 session俊 殿废茄促.
* 捞 皋家靛绰 橇肺弊贰赣啊 流立 龋免且 鞘夸绰 绝促.
* public栏肺 急攫等 捞蜡绰 窜瘤, rath.msnm.SwitchboardSession 努贰胶狼
* sendFileRequest皋家靛俊辑 registerSend甫 龋免秦具瞪 鞘夸啊 乐扁 锭巩捞促.
* <p>
* 殿废俊 措茄 秦力绰 荐脚 倾遏/芭例 皋矫瘤啊 坷搁 磊悼栏肺 等促.
* 父距 档馒窍瘤 臼绰促搁, 康盔洒 Map俊辑 秦力登瘤 臼绰促.
*
* @param cookie 价脚且 颇老飘罚黎记俊 措茄 捻虐蔼
*/
public void registerSend( ToSendFile file )
{
toSendFileMap.put( file.getCookie(), file );
}
/**
* 颇老 荐脚阑 倾遏沁促绰 荤角阑 捞 session俊 殿废茄促.
* 捞 皋家靛绰 橇肺弊贰赣啊 流立 龋免且 鞘夸绰 绝促.
* <p>
* 殿废俊 措茄 秦力绰 弥辆 价脚 倾啊皋矫瘤啊 坷搁 磊悼栏肺 等促.
* 父距 档馒窍瘤 臼绰促搁, 康盔洒 Map俊辑 秦力登瘤 臼绰促.
*/
public void registerReceive( int cookie, File file )
{
toReceiveFileMap.put( String.valueOf(cookie), file );
}
/**
* 价脚夸没阑 荐脚磊啊 '倾遏'窍看促绰 皋矫瘤甫 贸府茄促.
* <p>
* 捞亥飘甫 惯价矫挪 饶, File阑 傈价窍扁 困茄 FileServer thread甫 积己窍绊
* 矫累茄促. 弊 饶 registerSend肺 殿废等 郴何 Map俊辑 力芭等促.
*/
public void processAccept( MimeMessage mime ) throws FileNotFoundException, IOException
{
String cookie = mime.getProperty("Invitation-Cookie");
ToSendFile toSend = (ToSendFile)toSendFileMap.remove(cookie);
if( toSend!=null )
{
int cookieInt = Integer.parseInt(cookie);
FileTransferMessage msg = FileTransferMessage.createTransferMessage(cookieInt);
VolatileTransferServer vts = VolatileTransferServer.getInstance( session.msn, toSend, msg );
vts.cookie = cookie;
session.sendMessage( msg );
session.msn.fireFileSendAcceptedEvent( session, cookieInt );
vts.start();
}
}
/**
* 价脚夸没阑 荐脚磊啊 '芭例'窍看促绰 皋矫瘤甫 贸府茄促.
* <p>
* 捞亥飘甫 惯价矫挪 饶, registerSend肺 殿废等 郴何 Map俊辑 力芭等促.
*/
public void processCancel( MimeMessage mime )
{
String cookie = mime.getProperty("Invitation-Cookie");
ToSendFile toSend = (ToSendFile)toSendFileMap.remove(cookie);
if( toSend!=null )
{
int cookieInt = Integer.parseInt(cookie);
session.msn.fireFileSendRejectedEvent( session, cookieInt, mime.getProperty("Cancel-Code") );
}
}
/**
* toReceiveMap俊 夸没等 cookie啊 乐绰瘤 炼荤窍咯 公搬己阑 瘤难具父 茄促.
*
* 价脚螟俊辑 '捞力 罐酒啊扼~' 窍绰 皋矫瘤俊 措茄 贸府甫 秦林绢具 茄促.
* Mime 皋矫瘤狼 沥焊俊 蝶扼 AuthCookie, host, port 沥焊甫 啊瘤绊 价脚磊狼
* 辑滚俊 立加窍咯 颇老阑 促款肺靛 窍绊, BYE~
*/
public void processDownload( MimeMessage mime )
{
String cookie = mime.getProperty("Invitation-Cookie");
File toReceive = (File)toReceiveFileMap.remove(cookie);
if( toReceive!=null )
{
/* 罐阑 颇老捞 捞固 粮犁秦档, 丹绢镜鳖富鳖俊 措茄 巴篮 公矫茄促.
捞巴篮 Application率俊辑 秦搬秦具父 茄促. */
VolatileDownloader vd = VolatileDownloader.getInstance( session.msn,
toReceive, mime );
vd.cookie = cookie;
vd.start();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -