public void setData(Serializable data) {
   InetSocketAddress[] sockets =
       (InetSocketAddress[]) ((Address) whitePagesGsd.getAddresses().get("socket")).getObject();
   CommandImpl cmd =
       new CommandImpl("GridServiceDescription.setData", Arrays.asList(new Object[] {data}));
   sendMessage(this.conversationManager, sockets, whitePagesGsd.getId(), cmd);
 }
 public Serializable getData() {
   InetSocketAddress[] sockets =
       (InetSocketAddress[]) ((Address) whitePagesGsd.getAddresses().get("socket")).getObject();
   CommandImpl cmd = new CommandImpl("GridServiceDescription.getData", null);
   Serializable data =
       (Serializable) sendMessage(this.conversationManager, sockets, whitePagesGsd.getId(), cmd);
   return data;
 }
 @Override
 public boolean equals(Object obj) {
   // @TODO: improve equals comparision
   final GridServiceDescription other = (GridServiceDescription) obj;
   if (!this.getId().equals(other.getId())) {
     return false;
   }
   return true;
 }
 public void setServiceInterface(Class cls) {
   InetSocketAddress[] sockets =
       (InetSocketAddress[]) ((Address) whitePagesGsd.getAddresses().get("socket")).getObject();
   CommandImpl cmd =
       new CommandImpl(
           "GridServiceDescription.setServiceInterface", Arrays.asList(new Object[] {id, cls}));
   sendMessage(this.conversationManager, sockets, whitePagesGsd.getId(), cmd);
   this.serviceInterface = cls;
 }
 public void removeAddress(String transport) {
   InetSocketAddress[] sockets =
       (InetSocketAddress[]) ((Address) whitePagesGsd.getAddresses().get("socket")).getObject();
   CommandImpl cmd =
       new CommandImpl(
           "GridServiceDescription.removeAddress", Arrays.asList(new Object[] {id, transport}));
   sendMessage(this.conversationManager, sockets, whitePagesGsd.getId(), cmd);
   this.addresses.remove(transport);
 }
 public GridServiceDescriptionClient(
     GridServiceDescription gsd,
     GridServiceDescription whitePagesGsd,
     ConversationManager conversationManager) {
   this.id = gsd.getId();
   this.serviceInterface = gsd.getServiceInterface();
   this.addresses = new HashMap(gsd.getAddresses());
   this.data = gsd.getData();
   this.whitePagesGsd = whitePagesGsd;
   this.conversationManager = conversationManager;
 }
  public Address addAddress(String transport) {
    InetSocketAddress[] sockets =
        (InetSocketAddress[]) ((Address) whitePagesGsd.getAddresses().get("socket")).getObject();
    CommandImpl cmd =
        new CommandImpl(
            "GridServiceDescription.addAddress", Arrays.asList(new Object[] {this.id, transport}));
    Address address =
        (Address) sendMessage(this.conversationManager, sockets, whitePagesGsd.getId(), cmd);

    this.addresses.put(transport, address);

    return new AddressClient(address, whitePagesGsd, this.conversationManager);
  }
  public <T> GridConnection<T> createConnection(GridServiceDescription<T> gsd) {
    GridConnection<T> conn = null;

    if (this.localAllowed) {
      // internal boolean to disallow local connections
      GridNode gnode = this.grid.getGridNode(gsd.getId());
      if (gnode != null) {
        conn = new LocalGridNodeConnection(gnode);
      }
    }

    if (conn == null) {
      conn = new RemoteGridNodeConnection(this.grid, gsd);
    }

    return conn;
  }
Esempio n. 9
0
  public static void doRegisterSocketService(Grid grid, String id, String ip, int port) {
    CoreServicesLookupImpl coreServicesWP =
        (CoreServicesLookupImpl) grid.get(CoreServicesLookup.class);

    GridServiceDescriptionImpl gsd =
        (GridServiceDescriptionImpl) coreServicesWP.lookup(WhitePages.class);
    if (gsd == null) {
      gsd = new GridServiceDescriptionImpl(WhitePages.class);
    }

    GridServiceDescription<WhitePages> service =
        coreServicesWP.getServices().get(WhitePages.class.getName());
    if (service == null) {
      coreServicesWP.getServices().put(WhitePages.class.getName(), gsd);
      service = gsd;
    }
    Address address = null;
    if (service.getAddresses().get("socket") != null) {
      address = service.getAddresses().get("socket");
    } else {
      address = service.addAddress("socket");
    }

    InetSocketAddress[] addresses = (InetSocketAddress[]) address.getObject();
    if (addresses != null && addresses.length >= 1) {
      InetSocketAddress[] newAddresses = new InetSocketAddress[addresses.length + 1];
      if (addresses != null) {
        System.arraycopy(addresses, 0, newAddresses, 0, addresses.length);
      }

      newAddresses[addresses.length] = new InetSocketAddress(ip, port);
      ServiceConfiguration conf = new WhitePagesServiceConfiguration(newAddresses);
      service.setData(conf);
    } else {
      InetSocketAddress[] newAddress = new InetSocketAddress[1];
      newAddress[0] = new InetSocketAddress(ip, port);
      address.setObject(newAddress);
      ServiceConfiguration conf = new WhitePagesServiceConfiguration(newAddress);
      service.setData(conf);
    }
  }
  @Test
  public void test1() {

    Map<String, GridServiceDescription> coreServicesMap =
        new HashMap<
            String,
            GridServiceDescription>(); // Hazelcast.newHazelcastInstance( null ).getMap(
                                       // CoreServicesLookup.class.getName() );

    SystemEventListener l = SystemEventListenerFactory.getSystemEventListener();

    GridImpl grid1 = new GridImpl(new ConcurrentHashMap<String, Object>());

    GridPeerConfiguration conf = new GridPeerConfiguration();

    GridPeerServiceConfiguration coreSeviceConf =
        new CoreServicesLookupConfiguration(coreServicesMap);
    conf.addConfiguration(coreSeviceConf);

    MultiplexSocketServiceCongifuration socketConf =
        new MultiplexSocketServiceCongifuration(
            new MultiplexSocketServerImpl("127.0.0.1", new MinaAcceptorFactoryService(), l, grid1));
    conf.addConfiguration(socketConf);

    WhitePagesLocalConfiguration wplConf = new WhitePagesLocalConfiguration();
    wplConf.setWhitePages(
        new JpaWhitePages(Persistence.createEntityManagerFactory("org.drools.grid")));
    conf.addConfiguration(wplConf);

    socketConf.addService(WhitePages.class.getName(), wplConf.getWhitePages(), 5012);

    conf.configure(grid1);

    GridImpl grid2 = new GridImpl(new ConcurrentHashMap<String, Object>());
    conf = new GridPeerConfiguration();

    // coreServicesMap = Hazelcast.newHazelcastInstance( null ).getMap(
    // CoreServicesLookup.class.getName() );
    coreSeviceConf = new CoreServicesLookupConfiguration(coreServicesMap);
    conf.addConfiguration(coreSeviceConf);

    GridPeerServiceConfiguration wprConf = new WhitePagesRemoteConfiguration();
    conf.addConfiguration(wprConf);

    conf.configure(grid2);

    WhitePages wp = grid2.get(WhitePages.class);

    wp.create("s1");
    wp.create("s2");
    wp.create("s3");

    GridServiceDescription<String> gs1 = wp.lookup("s1");

    gs1.addAddress("p1").setObject("v1");
    gs1.addAddress("p2").setObject("v2");

    gs1 = wp.lookup("s1");
    assertEquals(2, gs1.getAddresses().size());
    assertEquals("v1", gs1.getAddresses().get("p1").getObject());
    assertEquals("v2", gs1.getAddresses().get("p2").getObject());

    gs1.removeAddress("p2");

    gs1 = wp.lookup("s1");
    assertEquals(1, gs1.getAddresses().size());
    assertEquals("v1", gs1.getAddresses().get("p1").getObject());

    wp.remove("s1");

    assertNull(wp.lookup("s1"));

    GridServiceDescription gs2 = wp.lookup("s2");
    assertNotNull(gs2);
    grid1.get(SocketService.class).close();
  }
Esempio n. 11
0
 public GridServiceDescription create(String serviceDescriptionId) {
   GridServiceDescription gsd = new GridServiceDescriptionImpl(serviceDescriptionId);
   this.directory.put(gsd.getId(), gsd);
   return gsd;
 }
  public static synchronized GridNode getGridNode(String name, Grid grid, boolean forceRemote) {

    if (logger.isDebugEnabled()) {
      logger.debug(" ### Grid Helper trying to locate GridNode: " + name);
    }

    if (nodeCache.containsKey(name)) {
      logger.debug(" ### Grid Helper found node " + name + " in cache");
      return nodeCache.get(name);
    }

    GridServiceDescription<GridNode> nGsd = grid.get(WhitePages.class).lookup(name);

    if (nGsd == null) {
      if (logger.isDebugEnabled()) {
        logger.error(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " ### Grid Helper DOES NOT Found a Node Descriptor for: "
                + name);
      }
      return null;
    }
    if (logger.isDebugEnabled()) {

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Found Node Descriptor: "
              + nGsd);
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t id: "
              + nGsd.getId());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Address size: "
              + nGsd.getAddresses().size());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Addresses: "
              + nGsd.getAddresses());
      for (String key : nGsd.getAddresses().keySet()) {
        logger.debug(
            "("
                + Thread.currentThread().getId()
                + ")"
                + Thread.currentThread().getName()
                + " \t ### Address: "
                + nGsd.getAddresses().get(key));
      }

      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t Interface: "
              + nGsd.getServiceInterface());
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### \t DATA: "
              + nGsd.getData());
    }

    ConnectionFactoryService csf = grid.get(ConnectionFactoryService.class);
    boolean allowsLocal = csf.isLocalAllowed();
    csf.setLocalAllowed(!forceRemote);
    GridConnection<GridNode> conn = csf.createConnection(nGsd);
    csf.setLocalAllowed(allowsLocal);

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper Create a Conection: "
              + name);
    }
    GridNode node = conn.connect();

    if (logger.isDebugEnabled()) {
      logger.debug(
          "("
              + Thread.currentThread().getId()
              + ")"
              + Thread.currentThread().getName()
              + " ### Grid Helper found GridNode: ("
              + name
              + ") -> "
              + node);
    }

    nodeCache.put(name, node);

    return node;
  }