/*************************************************************************
* Ce programme cree un socket TCP associe au port argv[0], attend une
* demande de connexion, pour chaque demande de connexion. Il attend
* la reception d'un fichier. Les donnees recues sont les suivantes:
*
* -
* taille du nom du fichier sur un entier,
*
* -
* taille du fichier sur un entier long,
*
* -
* tableau de bytes contenant le nom du fichier,
*
* - successions de tableaux de bytes de taille maximum 4096 octets
* correspondant aux donnees du fichier.
*
*
*
* ex: java FileServerNioMain 2005
************************************************************************/
package fileClientReceiverSingleMsg;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import csc4509.HalfDuplexFTPWorker;
import csc4509.ReadMessageStatus;
import csc4509.MessageSequenceException;
public class FileServerSenderMsgMain {
private static boolean debugState = true;
public static void main(String[] argv) throws MessageSequenceException {
ServerSocketChannel listenChannel;
ServerSocket listenSocket;
FileChannel fc;
FileInputStream fis;
File file;
ByteBuffer readBuf = ByteBuffer.allocate(4096);
SocketChannel rwChan;
InetSocketAddress rcvAddress;
int i = 1;
if (argv.length != 1) {
System.out.println("usage: java fileServer ");
return;
}
// on cree le socket de connexion et lui associe un port
// accept() est bloquant jusqu'a arrivee de demande de connexion
try {
listenChannel = ServerSocketChannel.open();
listenSocket = listenChannel.socket();
rcvAddress = new InetSocketAddress(Integer.parseInt(argv[0]));
listenSocket.setReuseAddress(true);
listenSocket.bind(rcvAddress);
while (i > 0) {
// accept one incoming connection
System.out.println("Waiting for connection request...");
rwChan = listenChannel.accept();
System.out.println("Connection request accepted...");
long fileSize;
String fileName;
try {
HalfDuplexFTPWorker worker = new HalfDuplexFTPWorker(rwChan);
ReadMessageStatus status;
int messType;
worker.clear();
status = worker.readMessage();
fileName = (String) worker.getData();
if (status != ReadMessageStatus.ReadDataCompleted) {
System.out
.println("Connection closed in read message 1");
break;
}
messType = worker.getMessType();
if (messType == 1) {
fileName = (String) worker.getData();
file = new File(fileName);
fis = new FileInputStream(file);
fileSize = file.length();
fc = fis.getChannel();
if (debugState) {
System.out.println("filename:" + fileName
+ " filesize:" + fileSize);
}
} else
throw new MessageSequenceException(
"Awaiting MessageType 1 and receiving "
+ messType);
worker.sendMsg(2, fileSize);
sendWithLoop(fc, readBuf, worker);
fc.close();
fis.close();
rwChan.close();
worker.close();
} catch (IOException ie) {
ie.printStackTrace();
return;
}
}
listenChannel.close();
listenSocket.close();
} catch (SocketException se) {
se.printStackTrace();
return;
} catch (IOException ie) {
ie.printStackTrace();
return;
}
}
static void sendWithLoop(FileChannel fc, ByteBuffer readBuf,
HalfDuplexFTPWorker worker) throws IOException {
// loop to send file content
int readSize = 0;
do {
// lecture d'un bloc du fichier original
readSize = fc.read(readBuf);
if (debugState) {
System.out.println("Read size : " + readSize);
}
if (readSize > 0) {
worker.setHeader(3, readSize);
readBuf.flip();
worker.setDataByteBuffer(readBuf);
worker.sendBuffers();
readBuf.clear();
}
} while (readSize > 0);
}
}