[Zerofile-svn] SF.net SVN: zerofile: [77] trunk/src
Status: Pre-Alpha
Brought to you by:
karl-bengtsson
|
From: <kar...@us...> - 2007-12-13 11:30:40
|
Revision: 77
http://zerofile.svn.sourceforge.net/zerofile/?rev=77&view=rev
Author: karl-bengtsson
Date: 2007-12-13 03:30:42 -0800 (Thu, 13 Dec 2007)
Log Message:
-----------
File transfer ftw, w00t
Modified Paths:
--------------
trunk/src/HttpServer.java
trunk/src/XMPPLinkLocalChatSession.java
trunk/src/ZeroFile.java
Modified: trunk/src/HttpServer.java
===================================================================
--- trunk/src/HttpServer.java 2007-12-05 07:25:07 UTC (rev 76)
+++ trunk/src/HttpServer.java 2007-12-13 11:30:42 UTC (rev 77)
@@ -1,14 +1,14 @@
import java.net.*;
import java.io.*;
import java.util.*;
-import java.lang.*;
public class HttpServer {
private int _portNr;
- private List<File> _files;
+ private ArrayList<File> _files = new ArrayList<File>();
private XMPPLinkLocalChatSession _session;
private Boolean _running = false;
private ServerSocket _serverSocket;
+ private HttpServer _httpServer = this;
private Thread _httpListenerThread = new Thread()
{
public void run()
@@ -21,7 +21,7 @@
System.out.println("New connection accepted " + connection.getInetAddress() + ":" + connection.getPort());
try
{
- httpRequestHandler request = new httpRequestHandler(connection);
+ httpRequestHandler request = new httpRequestHandler(_httpServer,connection);
// Create a new thread to process the request.
Thread thread = new Thread(request);
@@ -49,6 +49,7 @@
_portNr = _serverSocket.getLocalPort();
System.out.println("httpServer starting running on port " + _portNr);
_httpListenerThread.start();
+ _running = true;
}
catch (Exception e)
{
@@ -67,7 +68,7 @@
}
public void addFile(File filen)
{
- //_files.add(filen); TODO N\x8Ctt buggar h\x8Ar
+ _files.add(filen);
if (!_running)
start();
}
@@ -83,6 +84,33 @@
{
return _running;
}
+ public ArrayList<File> getFiles()
+ {
+ return _files;
+ }
+ public Boolean servesFile(String fileName)
+ {
+ for (int i = 0; i<_files.size(); i++)
+ {
+ if (_files.get(i).getName().equals(fileName))
+ return true;
+ }
+ return false;
+ }
+
+ public File getFile(String fileName)
+ {
+ for (int i = 0; i<_files.size(); i++)
+ {
+ if (_files.get(i).getName().equals(fileName))
+ return _files.get(i);
+ }
+ return null;
+ }
+ public void printDoneMsg()
+ {
+ _session.printTextToChatWindow("File transferred successfully!");
+ }
}
class httpRequestHandler implements Runnable
@@ -92,14 +120,16 @@
InputStream input;
OutputStream output;
BufferedReader br;
+ HttpServer _httpServer;
// Constructor
- public httpRequestHandler(Socket socket) throws Exception
+ public httpRequestHandler(HttpServer httpServer,Socket socket) throws Exception
{
this.socket = socket;
this.input = socket.getInputStream();
this.output = socket.getOutputStream();
this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ this._httpServer = httpServer;
}
public void run()
@@ -128,19 +158,17 @@
if(temp.equals("GET"))
{
String fileName = s.nextToken();
- fileName = "." + fileName ;
+ fileName = fileName.replaceFirst("/","");
- // Open the requested file.
FileInputStream fis = null ;
- boolean fileExists = true ;
- try
+ boolean fileExists = false;
+ File requestedFile = null;
+ if (_httpServer.servesFile(fileName))
{
- fis = new FileInputStream( fileName ) ;
+ requestedFile = _httpServer.getFile(fileName);
+ fis = new FileInputStream(requestedFile.getPath()) ;
+ fileExists = true;
}
- catch ( FileNotFoundException e )
- {
- fileExists = false ;
- }
// Construct the response message.
String serverLine = "Server: ZeroFile embedded http server/1.0" + CRLF;
@@ -150,7 +178,6 @@
String contentLengthLine = "error";
if ( fileExists )
{
- System.out.println("hittade fil");
statusLine = "HTTP/1.0 200 OK" + CRLF ;
contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF ;
contentLengthLine = "Content-Length: "
@@ -159,7 +186,6 @@
}
else
{
- System.out.println("hittade inte fil");
statusLine = "HTTP/1.0 404 Not Found" + CRLF ;
contentTypeLine = "Content-Type: text/html" + CRLF ;
entityBody = "<HTML>"
@@ -188,6 +214,8 @@
{
sendBytes(fis, output) ;
fis.close();
+ _httpServer.removeFile(requestedFile);
+ _httpServer.printDoneMsg();
}
else
{
Modified: trunk/src/XMPPLinkLocalChatSession.java
===================================================================
--- trunk/src/XMPPLinkLocalChatSession.java 2007-12-05 07:25:07 UTC (rev 76)
+++ trunk/src/XMPPLinkLocalChatSession.java 2007-12-13 11:30:42 UTC (rev 77)
@@ -52,16 +52,24 @@
public void offerFileTransfer(String localFileNameAndPath)
{
- File filen = new File(localFileNameAndPath);
- _httpServer.addFile(filen);
- long sizeOfFile = filen.length();
- String filnamn = filen.getName();
- String fileTransferStanza = "<message to=\""+_chatPartner.getServiceName()+"\" from=\""+ZeroconfRegistration.getMyServiceName()+"\"><body></body>";
- fileTransferStanza += "<x xmlns=\"jabber:x:oob\">";
- fileTransferStanza += "<url type=\"file\" size=\""+String.valueOf(sizeOfFile)+"\">";
- fileTransferStanza += "http://10.100.46.243:5297/25E8656B83FBA3A2/"+filnamn+"</url></x></message>";
- _toRemoteHost.print(fileTransferStanza);
- _toRemoteHost.flush();
+ try
+ {
+ File filen = new File(localFileNameAndPath);
+ _httpServer.addFile(filen);
+ long sizeOfFile = filen.length();
+ String filnamn = filen.getName();
+ String fileTransferStanza = "<message to=\""+_chatPartner.getServiceName()+"\" from=\""+ZeroconfRegistration.getMyServiceName()+"\"><body></body>";
+ fileTransferStanza += "<x xmlns=\"jabber:x:oob\">";
+ fileTransferStanza += "<url type=\"file\" size=\""+String.valueOf(sizeOfFile)+"\">";
+ fileTransferStanza += "http://"+InetAddress.getLocalHost().getHostAddress()+":"+_httpServer.getPort()+"/"+filnamn+"</url></x></message>";
+ System.out.println(fileTransferStanza);
+ _toRemoteHost.print(fileTransferStanza);
+ _toRemoteHost.flush();
+ }
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
}
public XMPPLinkLocalChatSession(XMPPLinkLocalHost hostToChatWith)
@@ -147,10 +155,6 @@
ZeroFile.downloadFileFromHTTP(XMPPDOMParser.getUrlFromFileTransferStanza(stanza));
_chatWindow.printText("Downloaded file from chat partner");
}
- else
- {
- // Skicka "jag vill inte ha din j\x8Avla fil-meddelande"
- }
}
}
}
@@ -191,6 +195,11 @@
}
}
+ public void printTextToChatWindow(String txt)
+ {
+ _chatWindow.printText(txt);
+ }
+
public void sendMessage(String m)
{
String messageStanza = "<message to=\""+_chatPartner.getServiceName()+"\" from=\""+ZeroconfRegistration.getMyServiceName()+"\"><body>"+m+"</body></message>";
Modified: trunk/src/ZeroFile.java
===================================================================
--- trunk/src/ZeroFile.java 2007-12-05 07:25:07 UTC (rev 76)
+++ trunk/src/ZeroFile.java 2007-12-13 11:30:42 UTC (rev 77)
@@ -7,6 +7,7 @@
* @throws IOException
*/
public static void main(String[] args) throws IOException {
+ System.out.println(InetAddress.getLocalHost().getHostAddress());
System.out.println(
new java.io.OutputStreamWriter(
new java.io.ByteArrayOutputStream()).getEncoding()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|