Esempio n. 1
0
 public ObjectStore(Client client) {
   this.client = client;
   client.addMessageListener(
       clientEventHandler,
       RemoteObjectDefMessage.class,
       RemoteMethodCallMessage.class,
       RemoteMethodReturnMessage.class);
   client.addClientStateListener(clientEventHandler);
 }
  public static void main(String[] args) throws IOException, InterruptedException {
    Serializer.registerClass(SomeObject.class);
    Serializer.registerClass(TestSerializationMessage.class);

    Server server = Network.createServer(5110);
    server.start();

    Client client = Network.connectToServer("localhost", 5110);
    client.start();

    server.addMessageListener(new TestSerialization(), TestSerializationMessage.class);
    client.send(new TestSerializationMessage(true));

    Thread.sleep(10000);
  }
Esempio n. 3
0
  public void exposeObject(String name, Object obj) throws IOException {
    // Create a local object
    LocalObject localObj = new LocalObject();
    localObj.objectName = name;
    localObj.objectId = objectIdCounter++;
    localObj.theObject = obj;
    // localObj.methods   = obj.getClass().getMethods();

    ArrayList<Method> methodList = new ArrayList<Method>();
    for (Method method : obj.getClass().getMethods()) {
      if (method.getDeclaringClass() == obj.getClass()) {
        methodList.add(method);
      }
    }
    localObj.methods = methodList.toArray(new Method[methodList.size()]);

    // Put it in the store
    localObjects.put(localObj.objectId, localObj);

    // Inform the others of its existence
    RemoteObjectDefMessage defMsg = new RemoteObjectDefMessage();
    defMsg.objects = new ObjectDef[] {makeObjectDef(localObj)};

    if (client != null) {
      client.send(defMsg);
      logger.log(Level.FINE, "Client: Sending {0}", defMsg);
    } else {
      server.broadcast(defMsg);
      logger.log(Level.FINE, "Server: Sending {0}", defMsg);
    }
  }
Esempio n. 4
0
  Object invokeRemoteMethod(RemoteObject remoteObj, Method method, Object[] args) {
    Integer methodIdInt = remoteObj.methodMap.get(method);
    if (methodIdInt == null)
      throw new RuntimeException("Method not implemented by remote object owner: " + method);

    boolean needReturn = method.getReturnType() != void.class;
    short objectId = remoteObj.objectId;
    short methodId = methodIdInt.shortValue();
    RemoteMethodCallMessage call = new RemoteMethodCallMessage();
    call.methodId = methodId;
    call.objectId = objectId;
    call.args = args;

    Invocation invoke = null;
    if (needReturn) {
      call.invocationId = invocationIdCounter++;
      invoke = new Invocation();
      // Note: could cause threading issues if used from multiple threads
      pendingInvocations.put(call.invocationId, invoke);
    }

    if (server != null) {
      remoteObj.client.send(call);
      logger.log(Level.FINE, "Server: Sending {0}", call);
    } else {
      client.send(call);
      logger.log(Level.FINE, "Client: Sending {0}", call);
    }

    if (invoke != null) {
      synchronized (invoke) {
        while (!invoke.available) {
          try {
            invoke.wait();
          } catch (InterruptedException ex) {
            ex.printStackTrace();
          }
        }
      }
      // Note: could cause threading issues if used from multiple threads
      pendingInvocations.remove(call.invocationId);
      return invoke.retVal;
    } else {
      return null;
    }
  }
Esempio n. 5
0
  private void onMessage(HostedConnection source, Message message) {
    // Might want to do more strict validation of the data
    // in the message to prevent crashes

    if (message instanceof RemoteObjectDefMessage) {
      RemoteObjectDefMessage defMsg = (RemoteObjectDefMessage) message;

      ObjectDef[] defs = defMsg.objects;
      for (ObjectDef def : defs) {
        RemoteObject remoteObject = new RemoteObject(this, source);
        remoteObject.objectId = (short) def.objectId;
        remoteObject.methodDefs = def.methodDefs;
        remoteObjects.put(def.objectName, remoteObject);
        remoteObjectsById.put(def.objectId, remoteObject);
      }

      synchronized (receiveObjectLock) {
        receiveObjectLock.notifyAll();
      }
    } else if (message instanceof RemoteMethodCallMessage) {
      RemoteMethodCallMessage call = (RemoteMethodCallMessage) message;
      LocalObject localObj = localObjects.get(call.objectId);
      if (localObj == null) return;

      if (call.methodId < 0 || call.methodId >= localObj.methods.length) return;

      Object obj = localObj.theObject;
      Method method = localObj.methods[call.methodId];
      Object[] args = call.args;
      Object ret = null;
      try {
        ret = method.invoke(obj, args);
      } catch (IllegalAccessException ex) {
        logger.log(Level.WARNING, "RMI: Error accessing method", ex);
      } catch (IllegalArgumentException ex) {
        logger.log(Level.WARNING, "RMI: Invalid arguments", ex);
      } catch (InvocationTargetException ex) {
        logger.log(Level.WARNING, "RMI: Invocation exception", ex);
      }

      if (method.getReturnType() != void.class) {
        // send return value back
        RemoteMethodReturnMessage retMsg = new RemoteMethodReturnMessage();
        retMsg.invocationID = call.invocationId;
        retMsg.retVal = ret;
        if (server != null) {
          source.send(retMsg);
          logger.log(Level.FINE, "Server: Sending {0}", retMsg);
        } else {
          client.send(retMsg);
          logger.log(Level.FINE, "Client: Sending {0}", retMsg);
        }
      }
    } else if (message instanceof RemoteMethodReturnMessage) {
      RemoteMethodReturnMessage retMsg = (RemoteMethodReturnMessage) message;
      Invocation invoke = pendingInvocations.get(retMsg.invocationID);
      if (invoke == null) {
        logger.log(Level.WARNING, "Cannot find invocation ID: {0}", retMsg.invocationID);
        return;
      }

      synchronized (invoke) {
        invoke.retVal = retMsg.retVal;
        invoke.available = true;
        invoke.notifyAll();
      }
    }
  }