Skip to content

Commit 32b2908

Browse files
committed
added file transfers
1 parent 01fb988 commit 32b2908

18 files changed

+204
-39
lines changed

src/main/java/de/cubeisland/teamcom/query/Command.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package de.cubeisland.teamcom.query;
2424

25+
import de.cubeisland.teamcom.query.exception.TeamComException;
26+
2527
import java.util.HashMap;
2628
import java.util.Map;
2729
import java.util.Map.Entry;

src/main/java/de/cubeisland/teamcom/query/NotConnectedException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package de.cubeisland.teamcom.query;
2424

25+
import de.cubeisland.teamcom.query.exception.TeamComException;
26+
2527
public class NotConnectedException extends TeamComException
2628
{
2729
public NotConnectedException()

src/main/java/de/cubeisland/teamcom/query/QueryConnection.java

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@
2323
package de.cubeisland.teamcom.query;
2424

2525
import de.cubeisland.teamcom.query.command.*;
26+
import de.cubeisland.teamcom.query.exception.FileTransferException;
27+
import de.cubeisland.teamcom.query.exception.NetworkingException;
28+
import de.cubeisland.teamcom.query.exception.TeamComException;
2629
import de.cubeisland.teamcom.query.value.EventMode;
2730

2831
import java.io.*;
2932
import java.net.InetAddress;
3033
import java.net.Socket;
3134
import java.net.SocketTimeoutException;
32-
import java.util.LinkedList;
33-
import java.util.Map;
34-
import java.util.Timer;
35-
import java.util.TimerTask;
35+
import java.util.*;
36+
import java.util.concurrent.atomic.AtomicInteger;
3637

38+
import static de.cubeisland.teamcom.query.QueryConnectionBuilder.NO_DEFAULT_VIRTUAL_SERVER;
3739
import static de.cubeisland.teamcom.query.property.ClientProperty.CLIENT_NICKNAME;
3840
import static de.cubeisland.teamcom.query.value.EventMode.CHANNEL;
3941
import static de.cubeisland.teamcom.query.Response.parseLine;
42+
import static java.lang.Integer.parseInt;
4043

4144
/**
4245
* JTS3ServerQuery
@@ -48,10 +51,10 @@ public class QueryConnection implements Closeable
4851

4952
private final InetAddress address;
5053
private final int queryPort;
54+
private final int fileTransferPort;
5155

5256
private final String username;
5357
private final String password;
54-
private final String displayName;
5558
private final int defaultVirtualServer;
5659
private final Integer timeout;
5760

@@ -68,13 +71,15 @@ public class QueryConnection implements Closeable
6871

6972
private QueryCurrent queryCurrent;
7073

71-
public QueryConnection(InetAddress address, int queryPort, String username, String password, String displayName, int defaultVirtualServer, Integer timeout)
74+
private final AtomicInteger downloadCounter = new AtomicInteger(0);
75+
76+
public QueryConnection(InetAddress address, int queryPort, int fileTransferPort, String username, String password, int defaultVirtualServer, Integer timeout)
7277
{
7378
this.address = address;
7479
this.queryPort = queryPort;
80+
this.fileTransferPort = fileTransferPort;
7581
this.username = username;
7682
this.password = password;
77-
this.displayName = displayName;
7883
this.defaultVirtualServer = defaultVirtualServer;
7984
this.timeout = timeout;
8085
}
@@ -95,7 +100,7 @@ public Permission getPermission(int permID) throws TeamComException
95100
{
96101
for (Map<String, String> permInfo : this.build(PermissionList.class).execute().asMapList())
97102
{
98-
if (Integer.parseInt(permInfo.get("permid")) == permID)
103+
if (parseInt(permInfo.get("permid")) == permID)
99104
{
100105
return new Permission(permID, permInfo.get("permname"), permInfo.get("permdesc"));
101106
}
@@ -180,6 +185,16 @@ private void connect() throws TeamComException
180185
}
181186

182187
socketQuery.setSoTimeout(timeout);
188+
189+
190+
if (username != null)
191+
{
192+
build(Login.class).with(username, password).execute();
193+
}
194+
if (defaultVirtualServer != NO_DEFAULT_VIRTUAL_SERVER)
195+
{
196+
build(Use.class).serverId(defaultVirtualServer).execute();
197+
}
183198
}
184199
catch (IOException e)
185200
{
@@ -197,10 +212,10 @@ private void connect() throws TeamComException
197212
public void updateCurrent() throws TeamComException
198213
{
199214
Map<String, String> map = build(WhoAmI.class).execute().asMap();
200-
this.queryCurrent = new QueryCurrent(Integer.parseInt(map.get("client_id")),
201-
Integer.parseInt(map.get("virtualserver_id")),
202-
Integer.parseInt(map.get("client_channel_id")), null,
203-
Integer.parseInt(map.get("client_database_id")));
215+
this.queryCurrent = new QueryCurrent(parseInt(map.get("client_id")),
216+
parseInt(map.get("virtualserver_id")),
217+
parseInt(map.get("client_channel_id")), null,
218+
parseInt(map.get("client_database_id")));
204219
}
205220

206221
public <T extends Command> T build(Class<T> clazz)
@@ -498,6 +513,64 @@ public void run()
498513
return true;
499514
}
500515

516+
public FileTransfer downloadIcon(long id) throws TeamComException
517+
{
518+
return downloadFile("/icon_" + id, 0, null);
519+
}
520+
521+
public FileTransfer downloadFile(String path, int channel, String channelPassword) throws TeamComException
522+
{
523+
try
524+
{
525+
downloadCounter.compareAndSet(0xFFFF, 0);
526+
System.out.println("Getting icon #" + path);
527+
final Command c = build(FtInitDownload.class).init(downloadCounter.getAndIncrement(), path, channel, channelPassword, 0);
528+
Map<String, String> parsedResponse = c.execute().asMap();
529+
String ftkey = parsedResponse.get("ftkey");
530+
if (ftkey != null)
531+
{
532+
final Socket socket = new Socket(address, fileTransferPort);
533+
socket.getOutputStream().write((ftkey + "\n").getBytes("ASCII"));
534+
socket.setSoTimeout(2000);
535+
536+
return new FileTransfer(socket.getInputStream(), parseInt(parsedResponse.get("size")));
537+
}
538+
else
539+
{
540+
throw new FileTransferException("File transfer initialization did not return a key");
541+
}
542+
}
543+
catch (IOException e)
544+
{
545+
throw new NetworkingException(e.getMessage(), e);
546+
}
547+
}
548+
549+
public static final class FileTransfer implements Closeable
550+
{
551+
private final InputStream stream;
552+
private final int size;
553+
554+
public FileTransfer(InputStream stream, int size)
555+
{
556+
this.stream = stream;
557+
this.size = size;
558+
}
559+
560+
public InputStream getStream()
561+
{
562+
return stream;
563+
}
564+
565+
public int getSize() {
566+
return size;
567+
}
568+
569+
public void close() throws IOException
570+
{
571+
this.stream.close();
572+
}
573+
}
501574

502575
public static class QueryCurrent
503576
{

src/main/java/de/cubeisland/teamcom/query/QueryConnectionBuilder.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ public class QueryConnectionBuilder
3535

3636
private String username;
3737
private String password;
38-
private String displayName;
3938
private int defaultVirtualServer = NO_DEFAULT_VIRTUAL_SERVER;
4039
private Integer timeout = DEFAULT_TIMEOUT;
4140

4241

4342
public QueryConnection build()
4443
{
45-
return new QueryConnection(address, queryPort, username, password, displayName, defaultVirtualServer, timeout);
44+
return new QueryConnection(address, queryPort, fileTransferPort, username, password, defaultVirtualServer, timeout);
4645
}
4746

4847
public QueryConnectionBuilder withAddress(InetAddress addr)
@@ -73,17 +72,15 @@ public QueryConnectionBuilder withFileTransferPort(int port)
7372

7473
public QueryConnectionBuilder withCredentials(String username, String password)
7574
{
75+
if (username == null)
76+
{
77+
throw new IllegalArgumentException("Username must not be null!");
78+
}
7679
this.username = username;
7780
this.password = password;
7881
return this;
7982
}
8083

81-
public QueryConnectionBuilder withDisplayName(String displayName)
82-
{
83-
this.displayName = displayName;
84-
return this;
85-
}
86-
8784
public QueryConnectionBuilder withDefaultVirtualServer(int id)
8885
{
8986
this.defaultVirtualServer = id >= 0 ? id : NO_DEFAULT_VIRTUAL_SERVER;

src/main/java/de/cubeisland/teamcom/query/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static List<Map<String, String>> parseList(String rawResponse)
6969
*
7070
* @param line
7171
* @return
72-
* @throws TeamComException
72+
* @throws de.cubeisland.teamcom.query.exception.TeamComException
7373
*/
7474
public static Map<String, String> parseLine(String line)
7575
{

src/main/java/de/cubeisland/teamcom/query/ServerErrorException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package de.cubeisland.teamcom.query;
2424

25+
import de.cubeisland.teamcom.query.exception.TeamComException;
26+
2527
import java.util.Map;
2628

2729
import static de.cubeisland.teamcom.util.StringUtils.decodeString;

src/main/java/de/cubeisland/teamcom/query/command/ChannelDelete.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package de.cubeisland.teamcom.query.command;
2424

2525
import de.cubeisland.teamcom.query.Command;
26-
import de.cubeisland.teamcom.query.TeamComException;
26+
import de.cubeisland.teamcom.query.exception.TeamComException;
2727
import de.cubeisland.teamcom.query.QueryConnection;
2828

2929
/**

src/main/java/de/cubeisland/teamcom/query/command/ClientMove.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package de.cubeisland.teamcom.query.command;
2424

2525
import de.cubeisland.teamcom.query.Command;
26-
import de.cubeisland.teamcom.query.TeamComException;
26+
import de.cubeisland.teamcom.query.exception.TeamComException;
2727
import de.cubeisland.teamcom.query.QueryConnection;
2828

2929
/**

src/main/java/de/cubeisland/teamcom/query/command/FtInitDownload.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package de.cubeisland.teamcom.query.command;
2424

2525
import de.cubeisland.teamcom.query.Command;
26-
import de.cubeisland.teamcom.query.TeamComException;
26+
import de.cubeisland.teamcom.query.exception.TeamComException;
2727

2828
/**
2929
* Initializes a file transfer download.
@@ -35,14 +35,13 @@
3535
*/
3636
public class FtInitDownload extends Command
3737
{
38-
public FtInitDownload init(int clientFileTransferId, String filePath, int channelId, String channelPw,
39-
int seekpos) throws TeamComException
38+
public FtInitDownload init(int transferId, String path, int channelId, String channelPassword, int seekOffset) throws TeamComException
4039
{
41-
this.setParam("clientftfid", clientFileTransferId);
42-
this.setParam("name", filePath);
40+
this.setParam("clientftfid", transferId);
41+
this.setParam("name", path);
4342
this.setParam("cid", channelId);
44-
this.setParam("cpw", channelPw);
45-
this.setParam("seekpos", seekpos);
43+
this.setParam("cpw", channelPassword);
44+
this.setParam("seekpos", seekOffset);
4645
return this;
4746
}
4847
//Permissions:"i_ft_file_download_power i_ft_needed_file_download _power i_ft_quota_mb_download_per_client"

src/main/java/de/cubeisland/teamcom/query/command/Login.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Login extends Command
3636
public Login with(String login, String pw)
3737
{
3838
this.setParam("client_login_name", login);
39-
this.setParam("client_login_password", pw);
39+
this.setParam("client_login_password", pw == null ? "" : pw);
4040
return this;
4141
}
4242
//Permissions:"b_serverquery_login"

0 commit comments

Comments
 (0)