From e7374452cda7c66ee6789d29aa466299b1a9e605 Mon Sep 17 00:00:00 2001 From: Jamie Doornbos Date: Fri, 25 Apr 2008 20:22:21 +0000 Subject: [PATCH] Skeleton for bureau/agent architecture (https://wiki.earth.threerings.net/display/whirled/The+Bureacracy) git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5025 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/bureau/client/Agent.java | 29 +++++ .../bureau/client/BureauClient.java | 21 ++++ .../bureau/client/BureauDecoder.java | 80 +++++++++++++ .../bureau/client/BureauDirector.java | 33 ++++++ .../bureau/client/BureauReceiver.java | 50 ++++++++ .../bureau/client/BureauService.java | 52 ++++++++ .../threerings/bureau/data/AgentObject.java | 92 +++++++++++++++ .../bureau/data/BureauClientObject.java | 32 +++++ .../threerings/bureau/data/BureauCodes.java | 33 ++++++ .../bureau/data/BureauMarshaller.java | 71 +++++++++++ .../bureau/server/BureauDispatcher.java | 85 ++++++++++++++ .../bureau/server/BureauProvider.java | 49 ++++++++ .../bureau/server/BureauRegistry.java | 111 ++++++++++++++++++ .../bureau/server/BureauSender.java | 59 ++++++++++ 14 files changed, 797 insertions(+) create mode 100644 src/java/com/threerings/bureau/client/Agent.java create mode 100644 src/java/com/threerings/bureau/client/BureauClient.java create mode 100644 src/java/com/threerings/bureau/client/BureauDecoder.java create mode 100644 src/java/com/threerings/bureau/client/BureauDirector.java create mode 100644 src/java/com/threerings/bureau/client/BureauReceiver.java create mode 100644 src/java/com/threerings/bureau/client/BureauService.java create mode 100644 src/java/com/threerings/bureau/data/AgentObject.java create mode 100644 src/java/com/threerings/bureau/data/BureauClientObject.java create mode 100644 src/java/com/threerings/bureau/data/BureauCodes.java create mode 100644 src/java/com/threerings/bureau/data/BureauMarshaller.java create mode 100644 src/java/com/threerings/bureau/server/BureauDispatcher.java create mode 100644 src/java/com/threerings/bureau/server/BureauProvider.java create mode 100644 src/java/com/threerings/bureau/server/BureauRegistry.java create mode 100644 src/java/com/threerings/bureau/server/BureauSender.java diff --git a/src/java/com/threerings/bureau/client/Agent.java b/src/java/com/threerings/bureau/client/Agent.java new file mode 100644 index 000000000..76a1169eb --- /dev/null +++ b/src/java/com/threerings/bureau/client/Agent.java @@ -0,0 +1,29 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.client; + +/** + * Represents an agent running within a bureau client. + */ +public class Agent +{ +} diff --git a/src/java/com/threerings/bureau/client/BureauClient.java b/src/java/com/threerings/bureau/client/BureauClient.java new file mode 100644 index 000000000..6933e764a --- /dev/null +++ b/src/java/com/threerings/bureau/client/BureauClient.java @@ -0,0 +1,21 @@ +package com.threerings.bureau.client; + +import com.threerings.presents.client.Client; +import com.threerings.presents.net.Credentials; +import com.samskivert.util.RunQueue; + +/** + * Represents a client embedded in a bureau. + */ +public class BureauClient extends Client +{ + /** + * Creates a new client. + * @param creds the credentials supplied during connection + * @param runQueue the place to post tasks required by clients + */ + public BureauClient (Credentials creds, RunQueue runQueue) + { + super(creds, runQueue); + } +} diff --git a/src/java/com/threerings/bureau/client/BureauDecoder.java b/src/java/com/threerings/bureau/client/BureauDecoder.java new file mode 100644 index 000000000..fd59631c5 --- /dev/null +++ b/src/java/com/threerings/bureau/client/BureauDecoder.java @@ -0,0 +1,80 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.client; + +import com.threerings.bureau.client.BureauReceiver; +import com.threerings.presents.client.InvocationDecoder; +import com.threerings.presents.data.ClientObject; + +/** + * Dispatches calls to a {@link BureauReceiver} instance. + */ +public class BureauDecoder extends InvocationDecoder +{ + /** The generated hash code used to identify this receiver class. */ + public static final String RECEIVER_CODE = "3e98f7a30deb5a8e25e05c71c6081bf4"; + + /** The method id used to dispatch {@link BureauReceiver#createAgent} + * notifications. */ + public static final int CREATE_AGENT = 1; + + /** The method id used to dispatch {@link BureauReceiver#destroyAgent} + * notifications. */ + public static final int DESTROY_AGENT = 2; + + /** + * Creates a decoder that may be registered to dispatch invocation + * service notifications to the specified receiver. + */ + public BureauDecoder (BureauReceiver receiver) + { + this.receiver = receiver; + } + + @Override // documentation inherited + public String getReceiverCode () + { + return RECEIVER_CODE; + } + + @Override // documentation inherited + public void dispatchNotification (int methodId, Object[] args) + { + switch (methodId) { + case CREATE_AGENT: + ((BureauReceiver)receiver).createAgent( + (ClientObject)args[0], ((Integer)args[1]).intValue() + ); + return; + + case DESTROY_AGENT: + ((BureauReceiver)receiver).destroyAgent( + (ClientObject)args[0], ((Integer)args[1]).intValue() + ); + return; + + default: + super.dispatchNotification(methodId, args); + return; + } + } +} diff --git a/src/java/com/threerings/bureau/client/BureauDirector.java b/src/java/com/threerings/bureau/client/BureauDirector.java new file mode 100644 index 000000000..c15cdb90f --- /dev/null +++ b/src/java/com/threerings/bureau/client/BureauDirector.java @@ -0,0 +1,33 @@ +package com.threerings.bureau.client; + +import com.threerings.presents.client.BasicDirector; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.util.PresentsContext; + +/** + * Allows the server to create and destroy agents on a client. + * @see BureauRegistry + */ +public class BureauDirector extends BasicDirector + implements BureauReceiver +{ + protected BureauDirector (PresentsContext ctx) + { + super(ctx); + + // Set up our decoder so we can receive method calls + // from the server + _ctx.getClient().getInvocationDirector(). + registerReceiver(new BureauDecoder(this)); + } + + @Override // inherited documentation + public void createAgent (ClientObject client, int agentId) + { + } + + @Override // inherited documentation + public void destroyAgent (ClientObject client, int agentId) + { + } +} diff --git a/src/java/com/threerings/bureau/client/BureauReceiver.java b/src/java/com/threerings/bureau/client/BureauReceiver.java new file mode 100644 index 000000000..6b5a98441 --- /dev/null +++ b/src/java/com/threerings/bureau/client/BureauReceiver.java @@ -0,0 +1,50 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.client; + +import com.threerings.presents.client.InvocationReceiver; +import com.threerings.presents.data.ClientObject; + +/** + * Hooks for controlling a previously launched bureau client. + */ +public interface BureauReceiver extends InvocationReceiver +{ + /** + * Creates a new agent. Implementors should create a new {@link Agent} and give it access to + * the {@link AgentObject} referred to by the agentId parameter and must notify + * the service that the agent has been created using {@link BureauService#agentCreated}. + * @param client the client receiving the request + * @param agentId the id of the AgentObject that needs an Agent + */ + void createAgent (ClientObject client, int agentId); + + /** + * Destroys a previously created agent. Implementors should destroy the agent that was created + * by the call to createAgent with the same agent id and must notify + * the service that the agent has been created using {@link BureauService#agentDestroyed}. + * @param client the client receiving the request + * @param agentId the id of the AgentObject whose Agent + * should be destroyed + */ + void destroyAgent (ClientObject client, int agentId); +} diff --git a/src/java/com/threerings/bureau/client/BureauService.java b/src/java/com/threerings/bureau/client/BureauService.java new file mode 100644 index 000000000..571aa9264 --- /dev/null +++ b/src/java/com/threerings/bureau/client/BureauService.java @@ -0,0 +1,52 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.client; + +import com.threerings.presents.client.Client; +import com.threerings.presents.client.InvocationService; + +/** + * Interface for the bureau to communicate with the server. + */ +public interface BureauService extends InvocationService +{ + /** + * Notifies the server that the bureau is up and running and ready to receive + * requests via the BureauReceiver. + * @see BureauReceiver + */ + void bureauInitialized (Client client, String bureauId); + + /** + * Notify the server that a previosuly requested agent is not created and ready to use. + * @see BureauReceiver#createAgent + */ + void agentCreated (Client client, int agentId); + + /** + * Notify the server that an agent is no longer running. Normally called in response + * to a call to destroyAgent + * @see BureauReceiver#destroyAgent + */ + void agentDestroyed (Client client, int agentId); +} + diff --git a/src/java/com/threerings/bureau/data/AgentObject.java b/src/java/com/threerings/bureau/data/AgentObject.java new file mode 100644 index 000000000..d87b9c2e5 --- /dev/null +++ b/src/java/com/threerings/bureau/data/AgentObject.java @@ -0,0 +1,92 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.data; + +import com.threerings.presents.dobj.DObject; + +public class AgentObject extends DObject +{ + // AUTO-GENERATED: FIELDS START + /** The field name of the bureauId field. */ + public static final String BUREAU_ID = "bureauId"; + + /** The field name of the agentCode field. */ + public static final String AGENT_CODE = "agentCode"; + + /** The field name of the startConfirmed field. */ + public static final String START_CONFIRMED = "startConfirmed"; + // AUTO-GENERATED: FIELDS END + + public String bureauId; + public String agentCode; + public boolean startConfirmed; + + // AUTO-GENERATED: METHODS START + /** + * Requests that the bureauId field be set to the + * specified value. The local value will be updated immediately and an + * event will be propagated through the system to notify all listeners + * that the attribute did change. Proxied copies of this object (on + * clients) will apply the value change when they received the + * attribute changed notification. + */ + public void setBureauId (String value) + { + String ovalue = this.bureauId; + requestAttributeChange( + BUREAU_ID, value, ovalue); + this.bureauId = value; + } + + /** + * Requests that the agentCode field be set to the + * specified value. The local value will be updated immediately and an + * event will be propagated through the system to notify all listeners + * that the attribute did change. Proxied copies of this object (on + * clients) will apply the value change when they received the + * attribute changed notification. + */ + public void setAgentCode (String value) + { + String ovalue = this.agentCode; + requestAttributeChange( + AGENT_CODE, value, ovalue); + this.agentCode = value; + } + + /** + * Requests that the startConfirmed field be set to the + * specified value. The local value will be updated immediately and an + * event will be propagated through the system to notify all listeners + * that the attribute did change. Proxied copies of this object (on + * clients) will apply the value change when they received the + * attribute changed notification. + */ + public void setStartConfirmed (boolean value) + { + boolean ovalue = this.startConfirmed; + requestAttributeChange( + START_CONFIRMED, Boolean.valueOf(value), Boolean.valueOf(ovalue)); + this.startConfirmed = value; + } + // AUTO-GENERATED: METHODS END +} diff --git a/src/java/com/threerings/bureau/data/BureauClientObject.java b/src/java/com/threerings/bureau/data/BureauClientObject.java new file mode 100644 index 000000000..6ebd369d6 --- /dev/null +++ b/src/java/com/threerings/bureau/data/BureauClientObject.java @@ -0,0 +1,32 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.data; + +import com.threerings.presents.dobj.DObject; + +/** + * Defines the fields available for the distributed part of a bureau. + */ +public class BureauClientObject extends DObject +{ + // TODO: add some fields +} diff --git a/src/java/com/threerings/bureau/data/BureauCodes.java b/src/java/com/threerings/bureau/data/BureauCodes.java new file mode 100644 index 000000000..d7ea7c1f6 --- /dev/null +++ b/src/java/com/threerings/bureau/data/BureauCodes.java @@ -0,0 +1,33 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.data; + +import com.threerings.presents.data.InvocationCodes; + +/** + * Codes and constants global to the Bureau services. + */ +public interface BureauCodes extends InvocationCodes +{ + /** Defines our invocation services group. */ + public static final String BUREAU_GROUP = "bureau"; +} diff --git a/src/java/com/threerings/bureau/data/BureauMarshaller.java b/src/java/com/threerings/bureau/data/BureauMarshaller.java new file mode 100644 index 000000000..2769f9054 --- /dev/null +++ b/src/java/com/threerings/bureau/data/BureauMarshaller.java @@ -0,0 +1,71 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.data; + +import com.threerings.bureau.client.BureauService; +import com.threerings.presents.client.Client; +import com.threerings.presents.data.InvocationMarshaller; +import com.threerings.presents.dobj.InvocationResponseEvent; + +/** + * Provides the implementation of the {@link BureauService} interface + * that marshalls the arguments and delivers the request to the provider + * on the server. Also provides an implementation of the response listener + * interfaces that marshall the response arguments and deliver them back + * to the requesting client. + */ +public class BureauMarshaller extends InvocationMarshaller + implements BureauService +{ + /** The method id used to dispatch {@link #agentCreated} requests. */ + public static final int AGENT_CREATED = 1; + + // from interface BureauService + public void agentCreated (Client arg1, int arg2) + { + sendRequest(arg1, AGENT_CREATED, new Object[] { + Integer.valueOf(arg2) + }); + } + + /** The method id used to dispatch {@link #agentDestroyed} requests. */ + public static final int AGENT_DESTROYED = 2; + + // from interface BureauService + public void agentDestroyed (Client arg1, int arg2) + { + sendRequest(arg1, AGENT_DESTROYED, new Object[] { + Integer.valueOf(arg2) + }); + } + + /** The method id used to dispatch {@link #bureauInitialized} requests. */ + public static final int BUREAU_INITIALIZED = 3; + + // from interface BureauService + public void bureauInitialized (Client arg1, String arg2) + { + sendRequest(arg1, BUREAU_INITIALIZED, new Object[] { + arg2 + }); + } +} diff --git a/src/java/com/threerings/bureau/server/BureauDispatcher.java b/src/java/com/threerings/bureau/server/BureauDispatcher.java new file mode 100644 index 000000000..fad99ce07 --- /dev/null +++ b/src/java/com/threerings/bureau/server/BureauDispatcher.java @@ -0,0 +1,85 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.server; + +import com.threerings.bureau.client.BureauService; +import com.threerings.bureau.data.BureauMarshaller; +import com.threerings.presents.client.Client; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.data.InvocationMarshaller; +import com.threerings.presents.server.InvocationDispatcher; +import com.threerings.presents.server.InvocationException; + +/** + * Dispatches requests to the {@link BureauProvider}. + */ +public class BureauDispatcher extends InvocationDispatcher +{ + /** + * Creates a dispatcher that may be registered to dispatch invocation + * service requests for the specified provider. + */ + public BureauDispatcher (BureauProvider provider) + { + this.provider = provider; + } + + @Override // documentation inherited + public InvocationMarshaller createMarshaller () + { + return new BureauMarshaller(); + } + + @SuppressWarnings("unchecked") + @Override // documentation inherited + public void dispatchRequest ( + ClientObject source, int methodId, Object[] args) + throws InvocationException + { + switch (methodId) { + case BureauMarshaller.AGENT_CREATED: + ((BureauProvider)provider).agentCreated( + source, + ((Integer)args[0]).intValue() + ); + return; + + case BureauMarshaller.AGENT_DESTROYED: + ((BureauProvider)provider).agentDestroyed( + source, + ((Integer)args[0]).intValue() + ); + return; + + case BureauMarshaller.BUREAU_INITIALIZED: + ((BureauProvider)provider).bureauInitialized( + source, + (String)args[0] + ); + return; + + default: + super.dispatchRequest(source, methodId, args); + return; + } + } +} diff --git a/src/java/com/threerings/bureau/server/BureauProvider.java b/src/java/com/threerings/bureau/server/BureauProvider.java new file mode 100644 index 000000000..ad0b420c2 --- /dev/null +++ b/src/java/com/threerings/bureau/server/BureauProvider.java @@ -0,0 +1,49 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.server; + +import com.threerings.bureau.client.BureauService; +import com.threerings.presents.client.Client; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.server.InvocationException; +import com.threerings.presents.server.InvocationProvider; + +/** + * Defines the server-side of the {@link BureauService}. + */ +public interface BureauProvider extends InvocationProvider +{ + /** + * Handles a {@link BureauService#agentCreated} request. + */ + public void agentCreated (ClientObject caller, int arg1); + + /** + * Handles a {@link BureauService#agentDestroyed} request. + */ + public void agentDestroyed (ClientObject caller, int arg1); + + /** + * Handles a {@link BureauService#bureauInitialized} request. + */ + public void bureauInitialized (ClientObject caller, String arg1); +} diff --git a/src/java/com/threerings/bureau/server/BureauRegistry.java b/src/java/com/threerings/bureau/server/BureauRegistry.java new file mode 100644 index 000000000..0fc5b6ddd --- /dev/null +++ b/src/java/com/threerings/bureau/server/BureauRegistry.java @@ -0,0 +1,111 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.server; + +import java.util.Map; +import java.util.Set; + +import com.threerings.bureau.data.AgentObject; +import com.threerings.bureau.data.BureauCodes; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.dobj.RootDObjectManager; +import com.threerings.presents.server.InvocationManager; + +/** + * Abstracts the launching and termination of external processes (bureaus) that host instances of + * server-side code (agents). + * TODO: this class needs lots of fleshing out and as such is not 100% documented + */ +public class BureauRegistry + implements BureauProvider +{ + /** + * The binary executable for the external vm being used. + * TODO: what args are needed? + * TODO: how to format the args? + * TODO: where does the binary path really come from - a bureau specification? + */ + public String vmBinary = "java %{jar} %{host}:%{port} %{agentclassname}"; + + /** + * Creates a new registry, prepared to provide bureau services. + */ + public BureauRegistry (InvocationManager invmgr, RootDObjectManager omgr) + { + _invmgr = invmgr; + _omgr = omgr; + + invmgr.registerDispatcher( + new BureauDispatcher(this), + BureauCodes.BUREAU_GROUP); + } + + /** + * Starts a new agent using the data in the given object, creating a new bureau if necessary. + */ + public void startAgent (AgentObject agent) + { + // TODO: create new bureau if needed + // TODO: otherwise call BureauSender.createAgent and pend + } + + /** + * Destorys a previously started agent using the data in the given object. + */ + public void destroyAgent (AgentObject agent) + { + // TODO: call BureauSender.destroyAgent and pend + // TODO: schedule a shutdown event for the bureau if this is the last agent + } + + @Override // inherited documentation + public void bureauInitialized (ClientObject client, String bureauId) + { + // TODO: create pending agents + // TODO: synchronization + } + + @Override // inherited documentation + public void agentCreated (ClientObject client, int agentId) + { + // TODO: remove from pending + } + + @Override // inherited documentation + public void agentDestroyed (ClientObject caller, int arg1) + { + // TODO: remove from pending destroyed + } + + // first stab at the data structures for holding bureaus and agents + protected static class Bureau + { + Process process; + ClientObject clientObj; + Set agents; + } + + protected InvocationManager _invmgr; + protected RootDObjectManager _omgr; + protected Map _bureaus; + protected Map> _pending; +} diff --git a/src/java/com/threerings/bureau/server/BureauSender.java b/src/java/com/threerings/bureau/server/BureauSender.java new file mode 100644 index 000000000..8fdab530d --- /dev/null +++ b/src/java/com/threerings/bureau/server/BureauSender.java @@ -0,0 +1,59 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2008 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.bureau.server; + +import com.threerings.bureau.client.BureauDecoder; +import com.threerings.bureau.client.BureauReceiver; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.server.InvocationSender; + +/** + * Used to issue notifications to a {@link BureauReceiver} instance on a + * client. + */ +public class BureauSender extends InvocationSender +{ + /** + * Issues a notification that will result in a call to {@link + * BureauReceiver#createAgent} on a client. + */ + public static void createAgent ( + ClientObject target, ClientObject arg1, int arg2) + { + sendNotification( + target, BureauDecoder.RECEIVER_CODE, BureauDecoder.CREATE_AGENT, + new Object[] { arg1, Integer.valueOf(arg2) }); + } + + /** + * Issues a notification that will result in a call to {@link + * BureauReceiver#destroyAgent} on a client. + */ + public static void destroyAgent ( + ClientObject target, ClientObject arg1, int arg2) + { + sendNotification( + target, BureauDecoder.RECEIVER_CODE, BureauDecoder.DESTROY_AGENT, + new Object[] { arg1, Integer.valueOf(arg2) }); + } + +}