// --- Reflection mechanisms -------------------------------- static public bool StorePropertiesAndFields(DistObject distobj, object obj, bool allProperties = false) { foreach (System.Reflection.PropertyInfo prop in obj.GetType().GetProperties()) { if (allProperties || Attribute.IsDefined(prop, typeof(DistProperty))) { if (!distobj.SetAttributeValue(prop.Name, DynamicType.CreateDynamicType(prop.GetValue(obj), allProperties))) { return(false); } } } foreach (System.Reflection.FieldInfo field in obj.GetType().GetFields()) { if (allProperties || Attribute.IsDefined(field, typeof(DistProperty))) { if (!distobj.SetAttributeValue(field.Name, DynamicType.CreateDynamicType(field.GetValue(obj), allProperties))) { return(false); } } } return(true); }
private void RegisterObjectPrototype(Type objectType, string nativeBaseTypename) { const System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.CreateInstance | System.Reflection.BindingFlags.OptionalParamBinding; // manually construct native dist object to use as factory object var factoryObj = new DistObject($"{objectType.Name}_factory"); var nativeRef = factoryObj.GetNativeReference(); // create managed implementation, supplying the dist object we just created var prototype = (Reference)Activator.CreateInstance(objectType, flags, null, new object[] { nativeRef }, null); // register a factory if (!RegisterObjecttHierarchy(objectType.Name, nativeBaseTypename, factoryObj)) { Message.Send(nameof(DistManager), MessageLevel.DEBUG, $"Using native factory >>> {objectType.Name}"); } else { Message.Send(nameof(DistManager), MessageLevel.DEBUG, $"Using managed factory >>> {objectType.Name}"); } // use the overloaded AddFactory to register with correct typename AddFactory(prototype, objectType.Name); }
public bool UpdateObject(DistTransaction transaction, DistObject o, Int32 timeOut = 0) { bool result = DistClient_updateObject(GetNativeReference(), transaction.GetNativeReference(), o.GetNativeReference(), timeOut); // Invalidate the tranaction transaction.Release(); return(result); }
static private DistObject GetObject(IntPtr o) { DistObject obj = ReferenceDictionary <DistObject> .GetObject(o); if (obj == null) { obj = Reference.CreateObject(o) as DistObject; } return(obj); }
private static void Client_OnNewObject(DistClient sender, DistObject o, DistSession session) { if (object_counter == 0) { new_object_timer = new Timer(); } object_counter++; sender.SubscribeAttributes(o, true); if (object_counter == OBJECTS) { Console.WriteLine($"Got {OBJECTS} new objects in {new_object_timer.GetTime()} seconds -> Frequency: {new_object_timer.GetFrequency(OBJECTS)} "); object_counter = 0; } }
static public void RestorePropertiesAndFields(DistObject distobj, object obj, bool allProperties = false) { foreach (System.Reflection.PropertyInfo prop in obj.GetType().GetProperties()) { if (allProperties || Attribute.IsDefined(prop, typeof(DistProperty))) { prop.SetValue(obj, distobj.GetAttributeValue(prop.Name).GetObject(prop.PropertyType, allProperties)); } } foreach (System.Reflection.FieldInfo field in obj.GetType().GetFields()) { if (allProperties || Attribute.IsDefined(field, typeof(DistProperty))) { field.SetValue(obj, distobj.GetAttributeValue(field.Name).GetObject(field.FieldType, allProperties)); } } }
public bool UpdateObject(DistTransaction transaction, DistObject o, Int32 timeOut = 0) { return(DistClient_updateObject(GetNativeReference(), transaction.GetNativeReference(), o.GetNativeReference(), timeOut)); }
public bool RemoveObject(DistObject o, Int32 timeOut = 0) { return(DistClient_removeObject(GetNativeReference(), o.GetNativeReference(), timeOut)); }
public bool AddObject(DistObject o, DistSession session, Int32 timeOut = 0) { return(DistClient_addObject(GetNativeReference(), o.GetNativeReference(), session.GetNativeReference(), timeOut)); }
static public void UninitializeFactories() { DistEvent.UninitializeFactory(); DistObject.UninitializeFactory(); }
public bool SubscribeAttributeValue(DistTransaction transaction, DistObject o, bool notifyExisting = false, Int32 timeOut = 0) { return(DistClient_subscribeAttributeValue_transaction(GetNativeReference(), transaction.GetNativeReference(), o.GetNativeReference(), notifyExisting, timeOut)); }
public bool SubscribeAllAttributeValues(DistObject o, bool notifyExisting = false, Int32 timeOut = 0) { return(DistClient_subscribeAllAttributeValues(GetNativeReference(), o.GetNativeReference(), notifyExisting, timeOut)); }
private static void Client_OnNewObject(DistClient sender, DistObject o, DistSession session) { sender.SubscribeAttributes(o, true); }
private static void Client_OnUpdateAttributes(DistClient sender, DistNotificationSet notif, DistObject o, DistSession session) { foreach (DistAttribute attr in notif) { System.Console.WriteLine(attr.ToString()); } }
private static void Client_OnNewAttributes(DistClient sender, DistNotificationSet notif, DistObject o, DistSession session) { sender.SubscribeAttributeValue(notif, o, true); }
static void Main(string[] args) { // Add a message receiver Message.OnMessage += Message_OnMessage; // Set message level to debug Message.SetMessageLevel(MessageLevel.DEBUG | MessageLevel.INTERNAL); // Initialize platforms for various used SDKs GizmoSDK.GizmoBase.Platform.Initialize(); GizmoSDK.GizmoDistribution.Platform.Initialize(); // Create a manager. The manager controls it all DistManager manager = DistManager.GetManager(true); // Register some factories manager.RegisterObject <BattlefieldSoldierObject>(); manager.RegisterObject <BattlefieldVehicleObject>(); manager.RegisterObject <BattlefieldTimeObject>(); // Events manager.RegisterEvent <BattlefieldTimeSyncEvent>(); DistObject x = manager.GetObject("sold", "BattlefieldSoldierObject"); // Start the manager with settting for transport protocols var MCastAddress = "234.2.3.100"; //var networkInterface = "10.23.24.50"; // Update this to your interface ip. E.g. 192.168.100.100. ushort serverPort = 6667; ushort sessionPort = 6668; var serverChannel = DistRemoteChannel.CreateChannel(5000, DistTransportType.MULTICAST, MCastAddress, serverPort /*,networkInterface*/); var sessionChannel = DistRemoteChannel.CreateChannel(5000, DistTransportType.MULTICAST, MCastAddress, sessionPort /*,networkInterface*/); manager.Start(sessionChannel, serverChannel); //If we want to attach the DistMonitor debugger manager.EnableDebug(true); // Client set up. You are a client that sends and receives information DistClient client = new DistClient("Our Test Client", manager); // We need to tell the client how to initialize client.Initialize(); // Now we can get a session. A kind of a meeting room that is used to exchange various "topics" DistSession session = client.GetSession("Battlefield", true, true); // Joint that session and subribe all events client.JoinSession(session); // Create a delegete client.OnNewObject += Client_OnNewObject; client.OnNewAttributes += Client_OnNewAttributes; client.OnUpdateAttributes += Client_OnUpdateAttributes; client.OnEvent += Client_OnEvent; // Subscribe all standard objects client.SubscribeObjects(session, null, true); // Subscribe all standard events client.SubscribeEvents(session); const int COUNT = 30; BattlefieldSoldierObject[] objs = new BattlefieldSoldierObject[COUNT]; DistTransaction update = new DistTransaction(); for (int i = 0; i < 1000; i++) { int idx = ((int)Time.SystemSeconds) % COUNT; if (objs[idx] == null) { objs[idx] = manager.GetObject <BattlefieldSoldierObject>($"Soldier{idx}"); client.AddObject(objs[idx], session); objs[idx] = client.WaitForObject(objs[idx].GetName(), session, -1) as BattlefieldSoldierObject; if (objs[idx] == null) { System.Console.WriteLine($"Failed to Created Soldier{idx} ---- No server"); System.Threading.Thread.Sleep(1000); continue; } else { System.Console.WriteLine($"Created Soldier{idx} --- OK"); } } update.NewTransaction(); update.SetAttributeValue("Updater", client.GetClientID().InstanceID.ToString()); update.SetAttributeValue("Obj", idx); if (!client.UpdateObject(update, objs[idx])) { System.Console.WriteLine($"Update failed for Soldier{idx} --- FAIL"); } System.Threading.Thread.Sleep(1000); } client.ResignSession(session); client.Uninitialize(); // Some kind of graceful shutdown manager.Shutdown(); // GC and platform uninit is managed by the system automatically }
static void Main(string[] args) { // Initialize platforms for various used SDKs GizmoSDK.GizmoBase.Platform.Initialize(); GizmoSDK.GizmoDistribution.Platform.Initialize(); GizmoSDK.GizmoBase.Message.SetMessageLevel(MessageLevel.DEBUG); // Create a manager. The manager controls it all DistManager manager = DistManager.GetManager(true); DistTransportType protocol = DistTransportType.MULTICAST; //string iface = "127.0.0.1"; string iface = ""; // Start the manager with settting for transport protocols manager.Start(DistRemoteChannel.CreateDefaultSessionChannel(false, protocol, iface), DistRemoteChannel.CreateDefaultServerChannel(false, protocol, iface)); // Client set up. You are a client that sends and receives information DistClient client = new DistClient("PerfClient", manager); // We need to tell the client how to initialize client.Initialize(0, 0, true); // Now we can get a session. A kind of a meeting room that is used to exchange various "topics" DistSession session = client.GetSession("PerfSession", true, true); // Joint that session and subribe all events client.JoinSession(session); client.SubscribeObjects(session); // Subscribe All Objects // Create a delegete client.OnNewObject += Client_OnNewObject; client.OnNewAttributes += Client_OnNewAttributes; client.OnUpdateAttributes += Client_OnUpdateAttributes; Console.WriteLine($"Press <RETURN> to start sending"); // Now loops around some simple program to get strings from console and distribute them as a message app while (true) { string result = Console.ReadLine(); if (result == "quit") { break; } Timer timer = new Timer(); DistObject [] objects = new DistObject[OBJECTS]; for (int i = 0; i < OBJECTS; i++) { objects[i] = manager.GetObject($"Object {i}"); client.AddObject(objects[i], session); objects[i] = client.WaitForObject($"Object {i}", session); } Console.WriteLine($"Added {OBJECTS} objects in {timer.GetTime()} seconds -> Frequency: {timer.GetFrequency(OBJECTS)}"); // make sure all objects are updated once DistTransaction transaction = new DistTransaction(); for (int j = 0; j < OBJECTS; j++) { transaction.NewTransaction(); transaction.SetAttributeValue("Test", 0.0); transaction.SetAttributeValue("id", Guid.NewGuid()); if (!client.UpdateObject(transaction, objects[j])) { Console.WriteLine("Boo"); } } System.Threading.Thread.Sleep(10); // Send COUNT updates on OBJECTS objects timer = new Timer(); for (int i = 0; i < COUNT; i++) { for (int j = 0; j < OBJECTS; j++) { transaction.NewTransaction(); transaction.SetAttributeValue("Test", timer.GetTime()); if (!client.UpdateObject(transaction, objects[j])) { Console.WriteLine("Failed to update object"); } } } Console.WriteLine($"Updated {OBJECTS} objects with {COUNT} updates in {timer.GetTime()} seconds -> Frequency: {timer.GetFrequency(COUNT*OBJECTS)}"); } while (manager.HasPendingData()) { System.Threading.Thread.Sleep(10); } client.Uninitialize(true); // Some kind of graceful shutdown manager.Shutdown(); // GC and platform uninit is managed by the system automatically }
public bool UpdateObject(string name, DynamicType value, DistObject o, Int32 timeOut = 0) { return(DistClient_updateObject_name(GetNativeReference(), name, value.GetNativeReference(), o.GetNativeReference(), timeOut)); }
public bool UnSubscribeAttributeValue(string attributeName, DistObject o, Int32 timeOut = 0) { return(DistClient_unsubscribeAttributeValue(GetNativeReference(), attributeName, o.GetNativeReference(), timeOut)); }
static void Main(string[] args) { // Add a message receiver Message.OnMessage += Message_OnMessage; // Set message level to debug Message.SetMessageLevel(MessageLevel.DEBUG); // Initialize platforms for various used SDKs GizmoSDK.GizmoBase.Platform.Initialize(); GizmoSDK.GizmoDistribution.Platform.Initialize(); // Create a manager. The manager controls it all DistManager manager = DistManager.GetManager(true); // Start the manager with settting for transport protocols manager.Start(DistRemoteChannel.CreateDefaultSessionChannel(), DistRemoteChannel.CreateDefaultServerChannel()); //If we want to attach the DistMonitor debugger manager.EnableDebug(true); // Client set up. You are a client that sends and receives information DistClient client = new DistClient("Our Test Client", manager); // We need to tell the client how to initialize client.Initialize(); // Now we can get a session. A kind of a meeting room that is used to exchange various "topics" DistSession session = client.GetSession("MessageSession", true, true); // Joint that session and subribe all events client.JoinSession(session); // Subscribe standard events client.SubscribeObjects(session, null, true); // Create a delegete client.OnNewObject += Client_OnNewObject; client.OnNewAttributes += Client_OnNewAttributes; client.OnUpdateAttributes += Client_OnUpdateAttributes; DistObject o = manager.GetObject("TestObject"); client.AddObject(o, session); o = client.WaitForObject("TestObject", session); DistTransaction update = new DistTransaction(); for (int i = 0; i < 100; i++) { update.NewTransaction(); update.SetAttributeValue("Updater", client.GetClientID().InstanceID.ToString()); update.SetAttributeValue("Time", Time.SystemSeconds); client.UpdateObject(update, o); System.Threading.Thread.Sleep(1000); } client.ResignSession(session); client.Uninitialize(); // Some kind of graceful shutdown manager.Shutdown(); // GC and platform uninit is managed by the system automatically }
public bool UnSubscribeAllAttributeValues(DistObject o, Int32 timeOut = 0) { return(DistClient_unsubscribeAllAttributeValues(GetNativeReference(), o.GetNativeReference(), timeOut)); }
public bool SubscribeAttributeValue(DistNotificationSet notification_set, DistObject o, bool notifyExisting = false, Int32 timeOut = 0) { return(DistClient_subscribeAttributeValue_notificationSet(GetNativeReference(), notification_set.GetNativeReference(), o.GetNativeReference(), notifyExisting, timeOut)); }
public bool UnSubscribeAttributeValue(DistTransaction transaction, DistObject o, Int32 timeOut = 0) { return(DistClient_unsubscribeAttributeValue_transaction(GetNativeReference(), transaction.GetNativeReference(), o.GetNativeReference(), timeOut)); }
private static void Client_OnUpdateAttributes(DistClient sender, DistNotificationSet notif, DistObject o, DistSession session) { if (update_counter == OBJECTS) { update_timer = new Timer(); } update_counter++; if (update_counter == OBJECTS * (COUNT + 1)) { Console.WriteLine($"Got {OBJECTS * COUNT} objects updates in {update_timer.GetTime()} seconds -> Frequency: {update_timer.GetFrequency(OBJECTS * COUNT)} "); update_counter = 0; } foreach (var a in notif) { var b = a.GetValue(); if (b.Is(DynamicType.Type.GUID)) { Guid c = b.GetGuid(); } } }
public bool UnSubscribeAttributeValue(DistNotificationSet notification_set, DistObject o, Int32 timeOut = 0) { return(DistClient_unsubscribeAttributeValue_notificationSet(GetNativeReference(), notification_set.GetNativeReference(), o.GetNativeReference(), timeOut)); }
public bool RegisterObjecttHierarchy(string typeName, string parentTypeName = "gzDistObject", DistObject factoryObject = null) { return(DistManager_registerObjectHierarchy(GetNativeReference(), typeName, parentTypeName, factoryObject?.GetNativeReference() ?? IntPtr.Zero)); }