/* Copyright 2009 Ramon Servadei Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package fulmine.demo; import java.io.IOException; import fulmine.Domain; import fulmine.Type; import fulmine.context.FulmineContext; import fulmine.context.IFulmineContext; import fulmine.distribution.connection.tcp.TcpNetwork; import fulmine.event.EventFrameExecution; import fulmine.event.IEvent; import fulmine.event.IEventSource; import fulmine.event.listener.EventListenerUtils; import fulmine.event.subscription.ISubscription; import fulmine.event.subscription.ISubscriptionListener; import fulmine.model.container.IContainer; import fulmine.model.field.StringField; public class HelloWorld2 { public static void main(String[] args) throws InterruptedException { IFulmineContext context = new FulmineContext("HelloWorldPublisher"); context.setNetwork(new TcpNetwork()); context.start(); IFulmineContext context2 = new FulmineContext("HelloWorldSubscriber"); context2.setNetwork(new TcpNetwork(null, 22222)); context2.start(); context2.subscribe("HelloWorldPublisher", "helloWorld", ISubscription.WILDCARD_TYPE, ISubscription.WILDCARD_DOMAIN, new ISubscriptionListener() { private final Class[] FILTER = EventListenerUtils.createFilter(IContainer.class); public void addedAsListenerFor(IEventSource source) { // noop } public void removedAsListenerFrom(IEventSource source) { // noop } public Class[] getEventTypeFilter() { // this method is called whenever an event is to be // distributed so we should not be re-creating the array // each time return FILTER; } public void update(IEvent event) { System.out.println("received " + event); } }); final IContainer record = context.getLocalContainer("helloWorld", Type.RECORD, Domain.get(1, "Tutorial")); StringField field1 = null; // this is to allow time for subscriptions to occur Thread.sleep(10000); // all record changes must be executed in a 'frame' record.beginFrame(new EventFrameExecution()); try { field1 = new StringField("field1", "an initial value"); record.add(field1); } finally { record.endFrame(); } // here we change the record again in another frame record.beginFrame(new EventFrameExecution()); try { // this overwrites the value of field1 from the previous frame field1.set("changed value"); record.add(new StringField("field2", "some other value")); } finally { record.endFrame(); } // allow time for change Thread.sleep(10000); // update field1 again record.beginFrame(new EventFrameExecution()); try { field1.set("changed value again"); } finally { record.endFrame(); } Thread.sleep(5000); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }