gvsu/cs654/proj1/KaZaClient.java
josh 3f0d05cf79 GUI-induced downloading and notifications implemented!
git-svn-id: svn://anubis/gvsu@53 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-03-09 03:30:24 +00:00

230 lines
7.1 KiB
Java

import java.io.*;
import java.net.*;
import java.util.*;
public class KaZaClient
{
public static final int LISTEN_PORT = 3443;
private static int m_transferIndex = 0;
private String m_sharedFolder;
private boolean m_connected = false;
private Socket m_socket;
private DataOutputStream m_os;
private FileServer m_fileServer;
private HashMap<Integer, ClientDownloader> m_clientDownloaders;
public KaZaClient(String userName, int kbps,
String sharedFolder, String server)
{
m_sharedFolder = sharedFolder;
try {
m_socket = new Socket(server, KaZaServer.LISTEN_PORT);
m_os = new DataOutputStream(m_socket.getOutputStream());
m_os.writeBytes("HELO " + userName + "\n");
m_os.writeBytes("SPED " + kbps + "\n");
File sharedDir = new File(m_sharedFolder);
if (sharedDir.isDirectory())
{
String[] files = sharedDir.list();
for (String fName : files)
{
if (fName.endsWith(".kaza"))
{
/* we found a description file, publish this file */
FileInputStream fis = new FileInputStream(m_sharedFolder +
File.separator +
fName);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
String sharedFileName = br.readLine();
String sharedFileDesc = br.readLine();
m_os.writeBytes("DESC " + sharedFileName + "\n" +
sharedFileDesc + "\n");
}
}
}
} catch (Exception e) {
return;
}
m_fileServer = new FileServer(LISTEN_PORT, m_sharedFolder);
Thread fsThread = new Thread(m_fileServer);
m_connected = true;
m_clientDownloaders = new HashMap<Integer, ClientDownloader>();
}
public boolean isDownloadActive(int index)
{
synchronized (m_clientDownloaders)
{
return m_clientDownloaders.containsKey(index);
}
}
public boolean connected() { return m_connected; }
public void close()
{
if (m_connected)
{
try {
m_os.writeBytes("QUIT\n");
m_os.close();
m_socket.close();
} catch (Exception e) { }
m_connected = false;
}
}
public Vector<SearchResult> performSearch(String query)
{
Vector<SearchResult> results = new Vector<SearchResult>();
try
{
m_os.writeBytes("SRCH 2 " + query + "\n");
BufferedReader br = new BufferedReader(
new InputStreamReader(
m_socket.getInputStream()));
for (;;)
{
String resultStr = br.readLine();
if (resultStr.equals("."))
break;
StringTokenizer tokens = new StringTokenizer(resultStr, "|");
SearchResult result = new SearchResult();
for (int i = 0; i < 5; i++)
{
if (tokens.hasMoreTokens())
{
String t = tokens.nextToken();
switch (i)
{
case 0:
result.peerAddress = t;
break;
case 1:
result.userName = t;
break;
case 2:
result.speed = Integer.parseInt(t);
break;
case 3:
result.fileName = t;
break;
case 4:
result.fileDescription = t;
break;
}
}
}
results.add(result);
}
}
catch (Exception e) { }
return results;
}
public class SearchResult
{
String peerAddress = "";
String userName = "";
int speed = 0;
String fileName = "";
String fileDescription = "";
public String toString()
{
return "User: \"" + userName + "\" [" +
peerAddress + ", " + getSpeedString(speed) +
"], File: \"" +
fileName + "\" (" + fileDescription + ")";
}
/* val is measured in Kbps */
private String getSpeedString(int val)
{
if (val > 999999)
return (val/1000000) + " Gbps";
if (val > 999)
return (val / 1000) + " Mbps";
return val + " Kbps";
}
}
public int downloadFile(String host, String fileName)
{
ClientDownloader cd = new ClientDownloader(host, fileName);
synchronized (m_clientDownloaders)
{
m_clientDownloaders.put(cd.getIndex(), cd);
}
Thread t = new Thread(cd);
t.start();
return cd.getIndex();
}
public synchronized int getTransferIndex()
{
return m_transferIndex++;
}
public class ClientDownloader implements Runnable
{
private String m_host;
private String m_fileName;
private int m_index;
public ClientDownloader(String host, String fileName)
{
m_host = host;
m_fileName = fileName;
m_index = getTransferIndex();
}
public int getIndex() { return m_index; }
public void run()
{
FileOutputStream fos;
try {
fos = new FileOutputStream(m_fileName);
} catch (Exception e) {
return;
}
try
{
Socket socket = new Socket(m_host, LISTEN_PORT);
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
DataInputStream is = new DataInputStream(socket.getInputStream());
os.writeBytes(m_fileName + "\n");
byte[] buff = new byte[1500];
while (!socket.isClosed())
{
int bytesRead = is.read(buff, 0, buff.length);
if (bytesRead < 0)
break;
fos.write(buff, 0, bytesRead);
}
}
catch (Exception e) { }
try {
fos.close();
} catch (Exception e) {
}
synchronized (m_clientDownloaders)
{
m_clientDownloaders.remove(getIndex());
}
}
}
}