begin implementing peer data types

This commit is contained in:
mykola2312 2024-10-22 02:38:44 +03:00
parent f3ddaffe59
commit 879b7aa742
5 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,52 @@
package com.mykola2312.retracker.tracker;
public enum AnnounceEvent {
NONE(0),
STARTED(1),
STOPPED(2),
COMPLETED(3);
private int value;
AnnounceEvent() {
this.value = 0;
}
AnnounceEvent(int value) {
this.value = value;
}
public static AnnounceEvent fromString(String value) {
switch (value) {
case "none": return AnnounceEvent.NONE;
case "started": return AnnounceEvent.STARTED;
case "stopped": return AnnounceEvent.STOPPED;
case "completed": return AnnounceEvent.COMPLETED;
default: return null;
}
}
public static AnnounceEvent fromInteger(int value) {
switch (value) {
case 0: return AnnounceEvent.NONE;
case 1: return AnnounceEvent.STARTED;
case 2: return AnnounceEvent.STOPPED;
case 3: return AnnounceEvent.COMPLETED;
default: return null;
}
}
@Override
public String toString() {
switch (this) {
case STARTED: return "started";
case STOPPED: return "stopped";
case COMPLETED: return "completed";
default: return null;
}
}
public int toInteger() {
return this.value;
}
}

View file

@ -0,0 +1,8 @@
package com.mykola2312.retracker.tracker;
import java.net.InetSocketAddress;
public class Peer {
private PeerType type;
private InetSocketAddress address;
}

View file

@ -0,0 +1,12 @@
package com.mykola2312.retracker.tracker;
/* This data is available only from local announces,
* It shall be used for re-announces
*/
public class PeerLocalData {
private long downloaded = 0; // 8 bytes integer
private long left = 0; // 8 bytes integer
private long uploaded = 0; // 8 bytes integer
private int event = 0; // 4 bytes integer, enum
private long key = 0; // 4 bytes integer
}

View file

@ -0,0 +1,6 @@
package com.mykola2312.retracker.tracker;
public enum PeerType {
LOCAL,
REMOTE
}

View file

@ -0,0 +1,19 @@
package com.mykola2312.retracker.tracker;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class AnnounceEventTest {
@Test
public void testEventEqual() {
assertEquals(AnnounceEvent.NONE, AnnounceEvent.NONE);
assertEquals(AnnounceEvent.STARTED, AnnounceEvent.STARTED);
}
@Test
public void testEventNotEqual() {
assertNotEquals(AnnounceEvent.STOPPED, AnnounceEvent.STARTED);
}
}