/**
* (c) Melexis Telecom and/or Remote Operating Services B.V.
*
* Distributable under LGPL license
* See terms of license at gnu.org
*/
package com.skyzoo.Jutil.tftp.server;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.skyzoo.Jutil.tftp.common.ACK;
import com.skyzoo.Jutil.tftp.common.DATA;
import com.skyzoo.Jutil.tftp.common.ERROR;
import com.skyzoo.Jutil.tftp.common.OACK;
import com.skyzoo.Jutil.tftp.common.TFTPOptions;
import com.skyzoo.Jutil.tftp.common.TFTPPacket;
import com.skyzoo.Jutil.tftp.common.TFTPSocket;
import com.skyzoo.Jutil.tftp.common.TFTPUtils;
/**
* However this class is called ClientHandler it is meant to be the
* class that handles the actual client tftp transfers.
*
* @author marco
*/
public class ClientHandler
{
/**
* Boolean flag that is used internally to indicate we might have stuffed
* the connection to the client with resent packages. This flag is cleared
* when we duplicate the timeout with the next data package sent which
* should help to clean up the channel that did get stuffed
*/
private boolean stuffedLink = false;
/**
* The private field for storage of the timeout setting for the communication
* with the client
*/
private int timeout = 5;
/**
* Creates a new instance of TFTPSocket
*/
public ClientHandler() throws SocketException
{
tftpSock = new TFTPSocket(timeout);
/* would like to replace with dynamic array! */
/* fileContent = new byte[MAX_FILE_SIZE]; */
}
/**
*
* Connects the clienthandler to the client, packages send do not need to contain a
* address. If they do they are overwritten by the ones of the connected socket.
*
* @return
*/
public void connect(InetAddress inetAddress, int clientPort)
{
tftpSock.connect(inetAddress, clientPort);
}
/**
*
* disconnects from the client, any packet send after a disconnect needs to contain a
* destination address and port, because nne is available on the socket.
*
*/
public void disconnect()
{
tftpSock.disconnect();
}
/**
* This method returns the IP address and port number of the
* client this class is talking to.
*/
public String getClient()
{
String client = "";
InetAddress addr = tftpSock.getAddress();
int port = tftpSock.getPort();
if (addr != null)
client += addr.getHostAddress();
if (port != 0)
{
client += ":";
client += port;
}
return client;
}
/**
* This method is an internal helper function that checks if the
* received ack packet matches the expectations. That means
* that the opcode and blocknumbers are expected.
*
* @param expecting The expected acknowledgement
* @param received The received acknowledgement
* @return true if received matched the expectations
*/
private boolean correctAnswer(ACK expecting, ACK received)
{
if (expecting == null)
return false;
if (received == null)
return false;
if (expecting.getOpCode() != received.getOpCode())
return false;
if (expecting.getBlockNr() != received.getBlockNr())
return false;
return true;
}
// public ACK DataTransfer(ACK send, ACK recv)
// {
// int retransmits = 0;
// int spamcount = 0;
//
// // send the packet....
// try
// {
// tftpSock.write(send);
// // if stuffed use duplicate timeout
// if (stuffedLink)
// tftpSock.setSocketTimeOut(timeout * 2);
// }
// catch (Exception e)
// {
// tftpLog.debug(getClient() + " UDP send packet failure.");
// tftpLog.debug(e.toString());
// }
//
// TFTPPacket tftpP;
// boolean receiving = true;
// // wait for succesfull acknowledgement!.....
// while (receiving)
// {
// try
// {
// tftpP = tftpSock.read();
// // set timout back because we gave enough time to clean up stuffed channel
// if (stuffedLink)
// {
// tftpSock.setSocketTimeOut(timeout);
// stuffedLink = false;
// }
// }
// catch (IOException ios)
// {
// tftpLog.info("IOException: " + ios.getMessage());
// return null;
// }
//
// // case we did not receive any packet
// if (tftpP == null)
// {
// if (retransmits++ > 5)
// {
// // Too many retries, give up.
// sendErrPacket(
// ERROR.ERR_NOT_DEFINED,
// "Retransmit limit exceeded");
// tftpLog.debug(
// getClient() + " Maximum retransmit count exceeded");
// return null;
// }
// else
// {
// // resend the packet and wait again!
// tftpLog.debug(
// getClient()
// + " expected packet before time out, sending ACK/DATA again");
// try
// {
// tftpSock.write(send);
// // set the flag to indicate that we might be stuffing the pipe for the
// // client, next packet needs to have longer timeout to give client some
// // time to clean up the pipe
// stuffedLink = true;
// }
// catch (Exception e)
// {
// tftpLog.debug(getClient() + " UDP send packet failure.");
// tftpLog.debug(e.toString());
// }
// continue;
// }
// }
//
// // case we received error
// if (tftpP instanceof ERROR)
// {
// tftpLog.debug(
// getClient() + " " + ((ERROR) tftpP).getErrorMessage());
// return null;
// }
//
// // case we did receive expected
// if ((tftpP instanceof ACK) && correctAnswer(recv, (ACK) tftpP))
// {
// return (ACK) tftpP;
// }
//
// // all other is spam and when too many of this crap is give up, and do not signal
// if (spamcount++ > 5)
// {
// return null;
// }
// }
// return null;
// }
public boolean receiveFileFromClient(OutputStream os, InetAddress clientAddress, int clientPort, boolean useOptions)
{
/* filename should not be empty, else send ERROR and say goodbye */
tftpSock.connect(clientAddress, clientPort);
Vector dataBlocks = new Vector(10);
// not expecting more then 10 * 512 bytes
// Send the first ackowledgement on received WRQ
int blockNumber = 0;
byte[] data = new byte[1];
int totalSize = 0;
ACK send;
DATA receive;
DATA expect;
// if frq has options then we oack instead of ack to receive data
if (useOptions)
{
TFTPOptions options = new TFTPOptions();
options.setTimeout(getTimeout());
options.setTransferSize(getTransferSize());
send = new OACK(0, options);
}
else
{
send = new ACK(blockNumber);
}
do
{
try
{
expect = new DATA(blockNumber + 1, data);
receive = (DATA) TFTPUtils.dataTransfer(tftpSock, send, expect);
}
catch (ClassCastException e)
{
tftpLog.debug("CODING ERROR? this case should be fetched in DataTransfer!");
ret
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
tftp.rar (66个子文件)
tftp
FileSystem.java 2KB
Tftp_server.java 2KB
.svn
props
all-wcprops 490B
entries 731B
text-base
Tftp.java.svn-base 13KB
FileSystem.java.svn-base 2KB
Tftp_server.java.svn-base 2KB
tmp
props
text-base
prop-base
prop-base
server
TFTPServer.java 3KB
TFTPSocket.java 8KB
.svn
props
all-wcprops 1KB
entries 1KB
text-base
TFTPSocket.java.svn-base 8KB
TFTPPool.java.svn-base 18KB
TFTPRequestHandler.java.svn-base 7KB
TFTPServerSocket.java.svn-base 5KB
ClientHandler.java.svn-base 20KB
TFTPServer.java.svn-base 3KB
EventListener.java.svn-base 727B
tmp
props
text-base
prop-base
prop-base
TFTPServerSocket.java 5KB
EventListener.java 727B
TFTPPool.java 18KB
ClientHandler.java 20KB
TFTPRequestHandler.java 7KB
Tftp.java 13KB
mbean
Server.java 3KB
.svn
props
all-wcprops 238B
entries 380B
text-base
Server.java.svn-base 3KB
tmp
props
text-base
prop-base
prop-base
client
TFTPClient.java 18KB
.svn
props
all-wcprops 248B
entries 386B
text-base
TFTPClient.java.svn-base 18KB
tmp
props
text-base
prop-base
prop-base
common
TFTPPacket.java 3KB
TFTPOptions.java 4KB
TFTPSocket.java 7KB
OACK.java 5KB
VirtualFileSystem.java 763B
.svn
props
all-wcprops 2KB
entries 3KB
text-base
TFTPSocket.java.svn-base 7KB
TFTPPacket.java.svn-base 3KB
RRQ.java.svn-base 594B
WRQ.java.svn-base 576B
TFTPFile.java.svn-base 736B
TFTPException.java.svn-base 1KB
ACK.java.svn-base 1KB
TFTPOptions.java.svn-base 4KB
ERROR.java.svn-base 3KB
DATA.java.svn-base 2KB
TFTPUtils.java.svn-base 6KB
VirtualFile.java.svn-base 1KB
VirtualFileImpl.java.svn-base 2KB
VirtualFileSystem.java.svn-base 763B
FRQ.java.svn-base 10KB
OACK.java.svn-base 5KB
tmp
props
text-base
prop-base
prop-base
DATA.java 2KB
RRQ.java 594B
TFTPFile.java 736B
TFTPUtils.java 6KB
WRQ.java 576B
VirtualFileImpl.java 2KB
VirtualFile.java 1KB
ACK.java 1KB
ERROR.java 3KB
TFTPException.java 1KB
FRQ.java 10KB
共 66 条
- 1
yanjian1978
- 粉丝: 2
- 资源: 82
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页