Wait why were these not in the Git
This commit is contained in:
parent
6f8f424d8d
commit
785ea6fd7f
|
@ -0,0 +1,65 @@
|
|||
package moe.nekojimi.friendcloud.network.requests;
|
||||
|
||||
import com.google.protobuf.Any;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import moe.nekojimi.friendcloud.Model;
|
||||
import moe.nekojimi.friendcloud.objects.NetworkObject;
|
||||
import moe.nekojimi.friendcloud.protos.ObjectStatements;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ObjectListRequest extends Request<ObjectStatements.ObjectListRequest, List<NetworkObject>>
|
||||
{
|
||||
private final Set<ObjectStatements.ObjectType> types;
|
||||
|
||||
public ObjectListRequest(Set<ObjectStatements.ObjectType> types)
|
||||
{
|
||||
this.types = types;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectStatements.ObjectListRequest buildMessage()
|
||||
{
|
||||
return ObjectStatements.ObjectListRequest.newBuilder().addAllTypes(types).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleReply(Any reply) throws InvalidProtocolBufferException
|
||||
{
|
||||
if (super.handleReply(reply))
|
||||
return true;
|
||||
try
|
||||
{
|
||||
if (reply.is(ObjectStatements.ObjectList.class))
|
||||
{
|
||||
ObjectStatements.ObjectList objectList = reply.unpack(ObjectStatements.ObjectList.class);
|
||||
|
||||
System.out.println("Received ObjectList, objects=" + objectList.getStatesList());
|
||||
|
||||
List<NetworkObject> ret = new ArrayList<>();
|
||||
|
||||
for (ObjectStatements.ObjectState objectState : objectList.getStatesList())
|
||||
{
|
||||
System.out.println("Received state of object " + objectState.getObjectId());
|
||||
NetworkObject object = Model.getInstance().getOrCreateObject(new NetworkObject.ObjectID(objectState.getObjectId()));
|
||||
object.updateFromStateMessage(objectState);
|
||||
ret.add(object);
|
||||
}
|
||||
|
||||
future.complete(ret);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace(System.err);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package moe.nekojimi.friendcloud.network.requests;
|
||||
|
||||
import com.google.protobuf.Any;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.google.protobuf.Message;
|
||||
import moe.nekojimi.friendcloud.protos.CommonMessages;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public abstract class Request<MessageType extends Message, ReturnType>
|
||||
{
|
||||
protected CompletableFuture<ReturnType> future = null;
|
||||
|
||||
public abstract MessageType buildMessage();
|
||||
|
||||
public boolean handleReply(Any reply) throws InvalidProtocolBufferException
|
||||
{
|
||||
if (reply.is(CommonMessages.ErrorMessage.class))
|
||||
{
|
||||
CommonMessages.ErrorMessage errorMessage = reply.unpack(CommonMessages.ErrorMessage.class);
|
||||
future.completeExceptionally(new RuntimeException("Request received error response: " + errorMessage.getError().name()));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public CompletableFuture<ReturnType> getFuture()
|
||||
{
|
||||
if (future == null)
|
||||
future = new CompletableFuture<>();
|
||||
return future;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue