private void sendMessage(MetaLinkProtobuf.Edit message) { MemoryStream messageMemoryStream = new MemoryStream(); ProtoBuf.Serializer.Serialize(messageMemoryStream, message); byte[] payload = messageMemoryStream.GetBuffer(); int payloadLength = (int)messageMemoryStream.Length; MemoryStream frameMemoryStream = new MemoryStream(); encodeInteger(frameMemoryStream, 0xDEADBEEF); encodeInteger(frameMemoryStream, (int)messageMemoryStream.Length); encodeInteger(frameMemoryStream, 0); byte[] payloadHash = getCrc32(payload, payloadLength); frameMemoryStream.Write(payloadHash, 0, 4); byte[] frame = frameMemoryStream.GetBuffer(); int frameLength = (int)frameMemoryStream.Length; byte[] frameHash = getCrc32(frame, frameLength); frameMemoryStream.Write(frameHash, 0, 4); frameMemoryStream.Write(payload, 0, payloadLength); frameMemoryStream.Write(payloadHash, 0, 4); _networkStream.Write(frameMemoryStream.GetBuffer(), 0, (int)frameMemoryStream.Length); _networkStream.Flush(); }
public void receiveThread() { FrameReader frameReader = new FrameReader(_socket); try { while (true) { MetaLinkProtobuf.Edit message = frameReader.getMessage(); if (EditMessageReceived != null) { EditMessageReceived(message); } } } catch (SocketException e) { ReceiveError(e); } catch (IOException e) { ReceiveError(e); } catch (ObjectDisposedException e) { ReceiveError(e); } catch (ProtoBuf.ProtoException e) { ReceiveError(e); } }
public void EditMessageReceived(MetaLinkProtobuf.Edit message) { queuedMessages.Enqueue(message); SyncControl.BeginInvoke((System.Action) delegate { if (addon != null) { ProcessQueuedEditMessages(); } }); }
public bool SendToMetaLinkBridge(MetaLinkProtobuf.Edit message) { bool status = true; lock (lockObject) { if (_socketQueue != null) { _socketQueue.enQueue(message); } else { status = false; } } return(status); // FIXME: doesn't really mean anything, since the send thread could fail. Does the consumer want a callback? }
protected void WaitForAllMetaLinkMessages() { Edit msg = new Edit(); msg.origin.Add(origin); msg.mode.Add(Edit.EditMode.INTEREST); Guid msgTopic = Guid.NewGuid(); msg.topic.Add(msgTopic.ToString()); testingClient.SendToMetaLinkBridge(msg); while (true) { Edit received; if (this.receivedMessagesQueue.TryTake(out received, 5 * 1000) == false) throw new TimeoutException(); if (received.topic.SequenceEqual(new string[] { msgTopic.ToString() })) { lock (receivedMessages) { receivedMessages.Remove(received); } break; } } }
[ComVisible(true)] // called by CPMDecorator public void SetSelected(IMgaFCO fco, bool selected) { try { // GMEConsole.Out.WriteLine(fco.Name + " selected"); if (fco.ParentModel == null) { return; } if (previouslySelectedParent != null && previouslySelectedParent != fco.ParentModel.ID) { selectedFCOIDs = new HashSet<string>(); } previouslySelectedParent = fco.ParentModel.ID; string topicGuid = null; MgaModel ancestor = fco.ParentModel; while (ancestor != null) { if (ancestor.Meta.Name == "ComponentAssembly") { var assembly = CyPhyMLClasses.ComponentAssembly.Cast(ancestor); if (designIdToCadAssemblyXml.ContainsKey(assembly.Guid.ToString())) { topicGuid = assembly.Guid.ToString(); break; } } else if (ancestor.Meta.Name == "CADModel") { /* var cadmodel = CyPhyMLClasses.CADModel.Cast(ancestor); if (assemblyExesStarted.Contains(cadmodel) || assemblyExesStarted.Contains(component.Guid.ToString())) { }*/ } else if (ancestor.Meta.Name == "Component") { var component = CyPhyMLClasses.Component.Cast(ancestor); if (syncedComponents.Count > 0 && ((topicGuid!=null&&syncedComponents.ContainsKey(topicGuid)) || syncedComponents.ContainsKey(component.Attributes.AVMID))) { topicGuid = component.Attributes.AVMID; } break; } else { return; } ancestor = ancestor.ParentModel; } if (topicGuid == null) { return; } if (selected) { selectedFCOIDs.Add(fco.ID); } else { selectedFCOIDs.Remove(fco.ID); } selectedFCOIDs.RemoveWhere(id => addon.Project.GetFCOByID(id).Status != (int)objectstatus_enum.OBJECT_EXISTS); var edit = new Edit(); edit.origin.Add(GMEOrigin); edit.editMode = Edit.EditMode.POST; edit.mode.Add(Edit.EditMode.POST); edit.actions.Add(new MetaLinkProtobuf.Action()); edit.actions[0].actionMode = MetaLinkProtobuf.Action.ActionMode.SELECT; edit.actions[0].payload = new Payload(); var selectedFCOs = selectedFCOIDs.Select(id => addon.Project.GetFCOByID(id)); edit.actions[0].payload.components.AddRange( selectedFCOs.Where(f => f.Meta.Name == "ComponentRef") .Select(CyPhyMLClasses.ComponentRef.Cast) .Select(component => new edu.vanderbilt.isis.meta.CADComponentType() { ComponentID = component.Attributes.InstanceGUID })); edit.actions[0].payload.datums.AddRange( selectedFCOs.Where(f => datumKinds.Contains(f.Meta.Name)) .Select(CyPhyMLClasses.CADDatum.Cast) .Where(datum => datum.ParentContainer.Impl.MetaBase.Name == typeof(CyPhyML.CADModel).Name && datum.ParentContainer.ParentContainer.Impl.MetaBase.Name == typeof(CyPhyML.Component).Name) .Select(datum => new DatumType() { name = datum.Attributes.DatumName, type = datumKindsMap[datum.Impl.MetaBase.Name], componentId = CyPhyMLClasses.Component.Cast(datum.ParentContainer.ParentContainer.Impl).Attributes.AVMID })); edit.topic.Add(edit.actions[0].payload.datums.Count > 0 ? ComponentUpdateTopic : CadAssemblyTopic); edit.topic.Add(topicGuid); bridgeClient.SendToMetaLinkBridge(edit); } catch (Exception e) { GMEConsole.Warning.WriteLine("An exception has been thrown during selection: " + e.Message); } }
public MetaLinkProtobuf.Edit UpdateComponentNameProtoBufMsg(CyPhyML.ComponentRef componentRef, string newName) { MetaLinkProtobuf.CADComponentType Component_msg = new MetaLinkProtobuf.CADComponentType { ComponentID = componentRef.Attributes.InstanceGUID, DisplayName = componentRef.Name }; MetaLinkProtobuf.Payload Payload_msg = new MetaLinkProtobuf.Payload(); Payload_msg.components.Add(Component_msg); MetaLinkProtobuf.Action Action_msg = new MetaLinkProtobuf.Action { actionMode = MetaLinkProtobuf.Action.ActionMode.UPDATE, payload = Payload_msg }; MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), sequence = 0, }; Edit_msg.origin.Add(GMEOrigin); Edit_msg.actions.Add(Action_msg); Edit_msg.topic.Add(CadAssemblyTopic); Edit_msg.topic.Add(AssemblyID); return Edit_msg; }
protected MetaLinkProtobuf.Edit CreateAddConstraintProtoBufMsg(string in_ComponentInstanceID, List<Tuple<CyPhy2CAD_CSharp.DataRep.Datum, CyPhy2CAD_CSharp.DataRep.Datum>> constraintPairs) { MetaLinkProtobuf.Payload Payload_msg = new MetaLinkProtobuf.Payload(); MetaLinkProtobuf.ConstraintType Constraint_msg = new MetaLinkProtobuf.ConstraintType() { ComponentID = in_ComponentInstanceID }; foreach (Tuple<CyPhy2CAD_CSharp.DataRep.Datum, CyPhy2CAD_CSharp.DataRep.Datum> element in constraintPairs) { MetaLinkProtobuf.PairType PairType_msg = new MetaLinkProtobuf.PairType { ConstraintFeatureA = new MetaLinkProtobuf.ConstraintFeatureType { ComponentID = element.Item1.ComponentID, FeatureName = element.Item1.DatumName, FeatureOrientationType = element.Item1.Orientation }, ConstraintFeatureB = new MetaLinkProtobuf.ConstraintFeatureType { ComponentID = element.Item2.ComponentID, FeatureName = element.Item2.DatumName, FeatureOrientationType = element.Item2.Orientation }, FeatureAlignmentType = element.Item1.Alignment, FeatureGeometryType = element.Item1.Type.ToString().ToUpper(), // need to add namespace to DatumType FeatureInterfaceType = "CAD_DATUM" }; Constraint_msg.Pair.Add(PairType_msg); } Payload_msg.constraints.Add(Constraint_msg); MetaLinkProtobuf.Action Action_msg = new MetaLinkProtobuf.Action { actionMode = MetaLinkProtobuf.Action.ActionMode.REPLACE, payload = Payload_msg }; MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), sequence = 0, }; Edit_msg.origin.Add(GMEOrigin); Edit_msg.actions.Add(Action_msg); Edit_msg.topic.Add(CadAssemblyTopic); Edit_msg.topic.Add(AssemblyID); return Edit_msg; }
public bool SendInterest(Action<MetaLinkProtobuf.Edit> noticeaction, params string[] topic) { MetaLinkProtobuf.Edit message = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.INTEREST, guid = Guid.NewGuid().ToString(), //sequence = 0, }; message.origin.Add(GMEOrigin); message.topic.AddRange(topic.ToList()); if (noticeaction != null) { noticeActions.Add(message.guid, noticeaction); } string topicstr = String.Join("_", topic); // MetaLink will send us the same message multiple times if we express interest multiple times if (!interests.ContainsKey(topicstr)) { interests.Add(topicstr, 1); bridgeClient.SendToMetaLinkBridge(message); return true; } else { interests[topicstr]++; } return false; }
public void SendCadAssemblyXml(string xml, string assemblyGuid, bool clear) { MetaLinkProtobuf.Edit message = new MetaLinkProtobuf.Edit { editMode = Edit.EditMode.POST, }; message.topic.Add(CadAssemblyTopic); message.topic.Add(assemblyGuid); if (clear) { message.actions.Add(new edu.vanderbilt.isis.meta.Action() { actionMode = MetaLinkProtobuf.Action.ActionMode.CLEAR }); } var action = new MetaLinkProtobuf.Action(); message.actions.Add(action); action.actionMode = MetaLinkProtobuf.Action.ActionMode.INSERT; MetaLinkProtobuf.Alien alien = new MetaLinkProtobuf.Alien(); action.alien = alien; alien.encoded = Encoding.UTF8.GetBytes(xml); alien.encodingMode = Alien.EncodingMode.XML; CyPhyML.ComponentAssembly targetAssembly = CyphyMetaLinkUtils.GetComponentAssemblyByGuid(addon.Project, assemblyGuid); if (targetAssembly != null) { var filter = targetAssembly.Impl.Project.CreateFilter(); filter.Kind = "ComponentRef"; try { var env = new MetaLinkProtobuf.Environment { name = SearchPathStr, }; foreach (var component in CyphyMetaLinkUtils.CollectComponentsRecursive(targetAssembly)) { if (component is CyPhyML.Component) AddSearchPathToEnvironment(component as CyPhyML.Component, env); } if (env.value.Count > 0) { action.environment.Add(env); } } catch (IOException) { // XXX } } bridgeClient.SendToMetaLinkBridge(message); }
private Edit CreateComponentEditMessage(string instanceId, CyPhyML.Component component, CyPhyML.CADModel cadModel) { string cadModelRelativePath = ""; if (false == cadModel.TryGetResourcePath(out cadModelRelativePath) || cadModelRelativePath == "") { // TODO log //return null; } var message = new Edit() { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), //sequence = 0, }; message.origin.Add(GMEOrigin); message.topic.Add(instanceId); edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action(); message.actions.Add(action); action.actionMode = MetaLinkProtobuf.Action.ActionMode.UPDATE_CAD_COMPONENT; action.subjectID = component.Attributes.AVMID; action.environment.Add(new edu.vanderbilt.isis.meta.Environment() { name = SearchPathStr, }); AddSearchPathToEnvironment(component, action.environment[0]); if (cadModelRelativePath.Length != 0) { action.payload = new Payload(); CADComponentType cadComponent = new CADComponentType() { AvmComponentID = component.Attributes.AVMID, CADModelID = CyphyMetaLinkUtils.GetResourceID(cadModel), // Using CADModelID to transport this information (the resource id) Name = Path.GetFileNameWithoutExtension(cadModelRelativePath), // the partial creo file name (less .prt or .asm) Type = (Path.GetExtension(cadModelRelativePath).EndsWith(".prt") || Path.GetExtension(cadModelRelativePath).EndsWith(".PRT")) ? "PART" : "ASSEMBLY" }; foreach (var connector in component.Children.ConnectorCollection) { cadComponent.Connectors.Add(new ConnectorType() { ID = connector.Guid.ToString(), DisplayName = connector.Name }); } foreach (var datum in cadModel.Children.CADDatumCollection) { cadComponent.Datums.Add(new ConnectorDatumType() { ID = datum.Attributes.DatumName, DisplayName = datum.Name }); } action.payload.components.Add(cadComponent); } return message; }
public void enQueue(MetaLinkProtobuf.Edit message) { _messageQueue.Add(message); }
// Insert something into a Component void TestInsertIntoComponent() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit(); msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add(CyPhyMetaLink.CyPhyMetaLinkAddon.ComponentUpdateTopic); msg.topic.Add(testAVMId); msg.actions.Add(new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.INSERT, payload = new Payload() }); msg.actions[0].payload.connectors.Add(new ConnectorType() { DisplayName = "TestConnector", }); msg.actions[0].payload.connectors[0].Datums.Add(new ConnectorDatumType() { DisplayName = "COMMON_PLANE_1_BOTTOM", ID = "COMMON_PLANE_1_BOTTOM" }); msg.actions[0].payload.connectors[0].Datums.Add(new ConnectorDatumType() { DisplayName = "COMMON_PLANE_1_TOP", ID = "COMMON_PLANE_1_TOP" }); msg.actions[0].payload.connectors[0].Datums.Add(new ConnectorDatumType() { DisplayName = "COMMON_AXIS", ID = "COMMON_AXIS" }); propagate.EditMessageReceived(msg); Application.DoEvents(); Thread.Sleep(1000); // XXX don't race with propagate's send message thread } project.BeginTransactionInNewTerr(); try { var imported = project.RootFolder.GetObjectByPathDisp("/@Imported_Components"); var damper = imported.ChildObjects.Cast<IMgaObject>().Where(o => o.Name == "Damper_2").First(); var testconn = damper.ChildObjects.Cast<MgaFCO>().Where(fco => fco.Name.Equals("TestConnector")).First(); CyPhyML.Connector conn = CyPhyMLClasses.Connector.Cast(testconn); Xunit.Assert.Equal(conn.Children.CADDatumCollection.Count(), 3); } finally { project.AbortTransaction(); } } ); }
// Remove component from design void TestRemoveComponent() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add(CyPhyMetaLink.CyPhyMetaLinkAddon.CadAssemblyTopic); msg.topic.Add(testAssemblyId); edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.DISCARD, payload = new Payload() }; action.payload.components.Add(new CADComponentType() { ComponentID = massInstanceGuid }); msg.actions.Add(action); propagate.EditMessageReceived(msg); Application.DoEvents(); } project.BeginTransactionInNewTerr(); try { var assembly = GetTestAssembly(project); var masscomp = assembly.ChildObjects.Cast<MgaFCO>().Where(fco => fco.Name.Equals(massName)); Xunit.Assert.Equal(0, masscomp.Count()); } finally { project.AbortTransaction(); } } ); }
public MetaLinkProtobuf.Edit getMessage() { while (true) { getMagic(); byte[] sizeBuf = new byte[4]; byte[] reserved = new byte[2]; byte[] payloadHashBuf = new byte[4]; byte[] headerHashBuf = new byte[4]; enQueue(4 + 1 + 1 + 2 + 4 + 4); Read(sizeBuf, 0, 4); ReadByte(); ReadByte(); Read(reserved, 0, 2); Read(payloadHashBuf, 0, 4); Read(headerHashBuf, 0, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(sizeBuf); Array.Reverse(payloadHashBuf); Array.Reverse(headerHashBuf); } int size = BitConverter.ToInt32(sizeBuf, 0); int payloadHash = BitConverter.ToInt32(payloadHashBuf, 0); int headerHash = BitConverter.ToInt32(headerHashBuf, 0); byte[] payload = new byte[size]; Read(payload, 0, size); byte[] finalPayloadHashBuf = new byte[4]; Read(finalPayloadHashBuf, 0, 4); if (BitConverter.IsLittleEndian) { Array.Reverse(finalPayloadHashBuf); } int finalPayloadHash = BitConverter.ToInt32(finalPayloadHashBuf, 0); byte[] frame = new byte[16]; getBytes(0, frame); byte[] expectedPayloadHashBuf = getCrc32(payload, size); byte[] expectedFrameHashBuf = getCrc32(frame, frame.Length); if (BitConverter.IsLittleEndian) { Array.Reverse(expectedPayloadHashBuf); Array.Reverse(expectedFrameHashBuf); } int expectedPayloadHash = BitConverter.ToInt32(expectedPayloadHashBuf, 0); int expectedFrameHash = BitConverter.ToInt32(expectedFrameHashBuf, 0); if (expectedPayloadHash != payloadHash) { dropMagic(); continue; } if (expectedFrameHash != headerHash) { dropMagic(); continue; } if (finalPayloadHash != payloadHash) { dropMagic(); continue; } dropAbsolute((int)_memoryStream.Position); MemoryStream payloadMemoryStream = new MemoryStream(payload); MetaLinkProtobuf.Edit message = Serializer.Deserialize <MetaLinkProtobuf.Edit>(payloadMemoryStream); return(message); } }
// Create AVM component from scratch void TestCreateComponent() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); msg.actions.Add(new edu.vanderbilt.isis.meta.Action()); msg.actions[0].actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.CREATE_CYPHY_COMPONENT; msg.actions[0].alien = new Alien(); msg.actions[0].alien.encodingMode = Alien.EncodingMode.XML; msg.actions[0].alien.encoded = Encoding.UTF8.GetBytes(File.ReadAllText(Path.Combine(TestModelDir,"CreateComponentData.xml"))); propagate.EditMessageReceived(msg); Application.DoEvents(); } project.BeginTransactionInNewTerr(); try { var imported = project.RootFolder.GetObjectByPathDisp("/@Imported_Components"); var result = imported.ChildObjects.Cast<IMgaObject>().Count(o => o.Name == "Damper_Test"); Xunit.Assert.Equal(1, result); } finally { project.AbortTransaction(); } } ); }
//[Fact] void TestAssemblySyncConnect() { // TODO: this needs to be rewritten SetupTest(); SendInterest("0"); WaitToReceiveMessage(); RunCyPhyMLSync( (project, propagate, interpreter) => { MgaFCO CAD_debug; MgaFCO CAD_debug_copy; MgaFCO hierarchy_2; MgaFCO hookInstance; MgaFCO hookCopyInstance; MgaReference CAD_debug_ref; MgaReference hierarchy_1_ref; MgaReference hierarchy_1_ref_copy; project.BeginTransactionInNewTerr(); try { var filter = project.CreateFilter(); filter.Kind = "ComponentAssembly"; CAD_debug = project.AllFCOs(filter).Cast<MgaFCO>() .Where(fco => fco.GetGuidDisp() == new Guid(testAssemblyId).ToString("B")).First(); // FIXME this copy is no good, since CyPhyMetaLink can just create the connection in CAD_debug CAD_debug_copy = CAD_debug.ParentFolder.CopyFCODisp(CAD_debug); CAD_debug_copy.Name = CAD_debug.Name + "_copy"; hierarchy_2 = project.AllFCOs(filter).Cast<MgaFCO>() .Where(fco => fco.GetGuidDisp() == new Guid(testAssemblyHierarchy_2Id).ToString("B")).First(); //hullAndHookHookGuid = (string)((MgaModel)project.RootFolder.ObjectByPath[hullAndHookHookPath]).GetAttributeByNameDisp("AVMID"); hookInstance = CAD_debug.ChildObjects.Cast<MgaFCO>().Where(f => f.Name.Contains("drawbar")).First(); hierarchy_1_ref = (MgaReference)hierarchy_2.ChildObjectByRelID[1]; CAD_debug_ref = (MgaReference)((MgaModel)hierarchy_1_ref.Referred).ChildObjectByRelID[1]; hookCopyInstance = CAD_debug_copy.ChildObjects.Cast<MgaFCO>().Where(f => f.Name.Contains("drawbar")).First(); hierarchy_1_ref_copy = (MgaReference)((IMgaModel)hierarchy_2).CopyFCODisp((MgaFCO)hierarchy_1_ref, hierarchy_1_ref.MetaRole); hierarchy_1_ref_copy.Referred = CAD_debug_copy; } finally { project.CommitTransaction(); } Edit connectMsg; project.BeginTransactionInNewTerr(); try { string hookInstanceGuid; string hookCopyInstanceGuid; hookInstanceGuid = hookInstance.GetStrAttrByNameDisp("InstanceGUID"); hookCopyInstanceGuid = hookCopyInstance.GetStrAttrByNameDisp("InstanceGUID"); // CyPhyAddon should reassign this (during the last CommitTransaction) Xunit.Assert.NotEqual(hierarchy_1_ref.GetStrAttrByNameDisp("InstanceGUID"), hierarchy_1_ref_copy.GetStrAttrByNameDisp("InstanceGUID")); Xunit.Assert.NotEqual(hookInstanceGuid, hookCopyInstanceGuid); connectMsg = new Edit(); connectMsg.mode.Add(Edit.EditMode.POST); connectMsg.origin.Add(origin); connectMsg.topic.Add("0"); var action = new edu.vanderbilt.isis.meta.Action(); action.actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.REMOVE_CYPHY_DESIGN_COMPONENT; action.payload = new Payload(); action.payload.components.Add(new CADComponentType() { ComponentID = Guid.Parse(hierarchy_2.GetGuidDisp()).ToString() }); action.payload.components.Add(new CADComponentType() { ComponentID = "2e1091d6-989e-4130-bfed-c046ef0f7011" + "_" + hierarchy_1_ref.GetStrAttrByNameDisp("InstanceGUID") + CAD_debug_ref.GetStrAttrByNameDisp("InstanceGUID") + hookInstanceGuid }); action.payload.components.Add(new CADComponentType() { ComponentID = "2e1091d6-989e-4130-bfed-c046ef0f7011" + "_" + hierarchy_1_ref_copy.GetStrAttrByNameDisp("InstanceGUID") + hookCopyInstanceGuid }); connectMsg.actions.Add(action); } finally { project.CommitTransaction(); } interpreter.StartAssemblySync(project, hierarchy_2, 128); Application.DoEvents(); SendToMetaLinkBridgeAndWaitForAddonToReceive(connectMsg); WaitForAllMetaLinkMessages(); Application.DoEvents(); Thread.Sleep(1000); // XXX don't race with propagate's send message thread WaitForAllMetaLinkMessages(); project.BeginTransactionInNewTerr(); try { Xunit.Assert.Equal((int)objectstatus_enum.OBJECT_DELETED, hookInstance.Status); } finally { project.AbortTransaction(); } } ); }
// Insert an existing AVM component into a design void TestInsertComponentIntoDesign() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.ADD_COMPONENT_TO_CYPHY_DESIGN, subjectID = testAssemblyId, payload = new Payload() }; action.payload.components.Add(new CADComponentType() { Name = "TestInsertComp", AvmComponentID = testAVMId }); msg.actions.Add(action); propagate.EditMessageReceived(msg); Application.DoEvents(); } project.BeginTransactionInNewTerr(); try { var assembly = GetTestAssembly(project); var insertcomp = assembly.ChildObjects.Cast<MgaFCO>().Where(fco => fco.Name.Contains("TestInsertComp")).First(); Xunit.Assert.NotEqual(null, insertcomp); CyPhyML.ComponentRef insertcompref = CyPhyMLClasses.ComponentRef.Cast(insertcomp); Xunit.Assert.NotEqual(null, insertcompref.AllReferred); Xunit.Assert.Equal(true, insertcompref.AllReferred is CyPhyML.Component); Xunit.Assert.Equal(testAVMId, (insertcompref.AllReferred as CyPhyML.Component).Attributes.AVMID); } finally { project.AbortTransaction(); } } ); }
public void SendComponentManifest() { MetaLinkProtobuf.Edit manifestMessage = new MetaLinkProtobuf.Edit { editMode = Edit.EditMode.POST, }; manifestMessage.topic.Add(ComponentManifestTopic); var manifestAction = new MetaLinkProtobuf.Action(); manifestMessage.actions.Add(manifestAction); manifestAction.actionMode = MetaLinkProtobuf.Action.ActionMode.INSERT; var rf = CyPhyMLClasses.RootFolder.GetRootFolder(addon.Project); Queue<CyPhyML.Components> componentsQueue = new Queue<CyPhyML.Components>(); foreach (var childComponents in rf.Children.ComponentsCollection) { componentsQueue.Enqueue(childComponents); } while (componentsQueue.Count > 0) { var components = componentsQueue.Dequeue(); manifestAction.manifest.Add(new ComponentManifestNode() { nodeMode = ComponentManifestNode.NodeMode.FOLDER, guid = components.Guid.ToString("D"), name = components.Name }); foreach (var childComponents in components.Children.ComponentsCollection) { componentsQueue.Enqueue(childComponents); } foreach (var component in components.Children.ComponentCollection) { manifestAction.manifest.Add(new ComponentManifestNode() { nodeMode = ComponentManifestNode.NodeMode.COMPONENT, guid = component.Attributes.AVMID, cyphyParentId = components.Guid.ToString("D"), name = component.Name }); } } bridgeClient.SendToMetaLinkBridge(manifestMessage); }
// Select component - only test that it won't fail void TestSelectComponent() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.SELECT_CYPHY_COMPONENT, payload = new Payload() }; action.payload.components.Add(new CADComponentType() { ComponentID = massInstanceGuid }); msg.actions.Add(action); propagate.EditMessageReceived(msg); Application.DoEvents(); } project.BeginTransactionInNewTerr(); try { // Nothing to check here } finally { project.AbortTransaction(); } } ); }
private void ProcessAVMComponentCreate(string component_xml, string topic) { try { File.WriteAllText(Path.Combine(Path.GetTempPath(), "CyPhyMetaLink_ComponentCreate.xml"), component_xml); } catch (IOException) { } XmlDocument doc = new XmlDocument(); doc.LoadXml(component_xml); XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); manager.AddNamespace("avm", "avm"); manager.AddNamespace("cad", "cad"); avm.Component component = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.DeserializeAvmComponentXml(new StringReader(doc.OuterXml)); addon.Project.BeginTransactionInNewTerr(); CyPhyML.Component createdComp = null; string createdavmid = null; try { var rf = CyPhyMLClasses.RootFolder.GetRootFolder(addon.Project); CyPhyML.Components importedComponents = CyphyMetaLinkUtils.GetImportedComponentsFolder(addon.Project); if (importedComponents == null) { importedComponents = CyPhyMLClasses.Components.Create(rf); importedComponents.Name = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.ImportedComponentsFolderName; } var avmidComponentMap = CyPhyComponentImporter.CyPhyComponentImporterInterpreter.getCyPhyMLComponentDictionary_ByAVMID(rf); createdComp = CyPhy2ComponentModel.Convert.AVMComponent2CyPhyML(importedComponents, component, true, GMEConsole); // Ricardo requirement: Classification must not be empy if (createdComp.Attributes.Classifications.Length == 0) createdComp.Attributes.Classifications = "Unknown"; List<string> notcopied = new List<string>(); CopyResources(Path.Combine(GetProjectDir(), createdComp.GetDirectoryPath(), "CAD\\"), doc, manager, notcopied, false); CyPhyML.CADModel cadModel = createdComp.Children.CADModelCollection.First(); // Ricardo requirement: ID must be cad.path if (cadModel.SrcConnections.UsesResourceCollection.Any() && cadModel.SrcConnections.UsesResourceCollection.First().DstEnds.Resource != null) { cadModel.SrcConnections.UsesResourceCollection.First().DstEnds.Resource.Attributes.ID = "cad.path"; } else if (cadModel.DstConnections.UsesResourceCollection.Any() && cadModel.DstConnections.UsesResourceCollection.First().DstEnds.Resource != null) { cadModel.DstConnections.UsesResourceCollection.First().DstEnds.Resource.Attributes.ID = "cad.path"; } foreach (var res in createdComp.Children.ResourceCollection) { res.Attributes.Path = Path.Combine("CAD", res.Name); } CyphyMetaLinkUtils.SetCADModelTypesFromFilenames(createdComp); RefreshManufacturingResources(createdComp, Path.Combine(createdComp.GetDirectoryPath(), "Manufacturing")); createdavmid = createdComp.Attributes.AVMID; } finally { addon.Project.CommitTransaction(); } if (createdavmid != null && topic != null) { MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit(){ editMode = Edit.EditMode.POST }; Edit_msg.topic.Add(CadPassiveTopic); Edit_msg.topic.Add(topic); MetaLinkProtobuf.Action action = new MetaLinkProtobuf.Action(){ actionMode = MetaLinkProtobuf.Action.ActionMode.SWITCH, interest = new Interest() }; action.interest.topic.Add(ComponentUpdateTopic); action.interest.uid.Add(createdavmid); Edit_msg.actions.Add(action); bridgeClient.SendToMetaLinkBridge(Edit_msg); SendInterest(null, ComponentUpdateTopic, createdavmid); } }
// Remove multiple components from design void TestRemoveComponents() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); edu.vanderbilt.isis.meta.Action action = new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.REMOVE_CYPHY_DESIGN_COMPONENT, subjectID = testAssemblyId, payload = new Payload() }; action.payload.components.Add(new CADComponentType() { ComponentID = massInstanceGuid }); action.payload.components.Add(new CADComponentType() { ComponentID = springInstanceGuid }); msg.actions.Add(action); propagate.EditMessageReceived(msg); Application.DoEvents(); } project.BeginTransactionInNewTerr(); try { var assembly = GetTestAssembly(project); Xunit.Assert.Equal(1, assembly.ChildObjects.Cast<MgaFCO>().Where(fco => fco.MetaRole.Kind.Name=="ComponentRef").Count()); } finally { project.AbortTransaction(); } } ); }
public bool SendDisinterest(params string[] topic) { MetaLinkProtobuf.Edit message = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.DISINTEREST, guid = Guid.NewGuid().ToString(), //sequence = 0, }; message.origin.Add(GMEOrigin); message.topic.AddRange(topic.ToList()); string topicstr = String.Join("_", topic); if (interests.ContainsKey(topicstr)) { interests[topicstr]--; if (interests[topicstr] == 0) { interests.Remove(topicstr); bridgeClient.SendToMetaLinkBridge(message); return true; } } else { GMEConsole.Warning.WriteLine("Sending disinterest for topic without interest entry"); bridgeClient.SendToMetaLinkBridge(message); } return false; }
// Receive a list of components void TestComponentList() { SetupTest(); { Edit interest = new Edit(); interest.mode.Add(Edit.EditMode.INTEREST); interest.origin.Add(origin); interest.topic.Add("0"); testingClient.SendToMetaLinkBridge(interest); } RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); msg.actions.Add(new edu.vanderbilt.isis.meta.Action() { actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.REQUEST_COMPONENT_LIST }); this.receivedMessages.Clear(); propagate.EditMessageReceived(msg); Application.DoEvents(); Thread.Sleep(1000); // XXX don't race with propagate's send message thread WaitForAllMetaLinkMessages(); } project.BeginTransactionInNewTerr(); try { Xunit.Assert.Equal(1, this.receivedMessages.Where(msg => msg.actions.Count == 1 && msg.actions[0].manifest.Count() == 11).Count()); } finally { project.AbortTransaction(); } } ); }
} // end function #region PROTOBUF protected MetaLinkProtobuf.Edit ModifyCADParameterProtoBufMsg(string in_ComponentInstanceID, CADParameter in_CADParameter) { MetaLinkProtobuf.ParametricParametersType ParametricParameters = new MetaLinkProtobuf.ParametricParametersType() { ComponentID = in_ComponentInstanceID }; MetaLinkProtobuf.CADParameterType CADParameter_msg = new MetaLinkProtobuf.CADParameterType { Name = in_CADParameter.Name, Type = in_CADParameter.Type, Value = in_CADParameter.Value }; ParametricParameters.CADParameter.Add(CADParameter_msg); MetaLinkProtobuf.Payload Payload_msg = new MetaLinkProtobuf.Payload(); Payload_msg.parameters.Add(ParametricParameters); MetaLinkProtobuf.Action Action_msg = new MetaLinkProtobuf.Action { actionMode = MetaLinkProtobuf.Action.ActionMode.REPLACE, payload = Payload_msg }; MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), sequence = 0, }; Edit_msg.origin.Add(GMEOrigin); Edit_msg.actions.Add(Action_msg); Edit_msg.topic.Add(CadAssemblyTopic); Edit_msg.topic.Add(AssemblyID); return Edit_msg; }
//[Fact] // Connect components within a design void TestConnectComponents() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { { Edit msg = new Edit(); msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); msg.actions.Add(new edu.vanderbilt.isis.meta.Action() { payload = new Payload() }); msg.actions[0].actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.CONNECT_COMPONENTS_CYPHY; msg.actions[0].payload.components.Add(new CADComponentType() { ComponentID = testAssemblyId }); msg.actions[0].payload.components.Add(new CADComponentType() { ComponentID = massInstanceGuid }); msg.actions[0].payload.components.Add(new CADComponentType() { ComponentID = springInstanceGuid }); //msg.actions[0].subjectID = testAVMId; propagate.EditMessageReceived(msg); Application.DoEvents(); Thread.Sleep(1000); // XXX don't race with propagate's send message thread } project.BeginTransactionInNewTerr(); try { var imported = project.RootFolder.GetObjectByPathDisp("/@Imported_Components"); var damper = imported.ChildObjects.Cast<IMgaObject>().Where(o => o.Name == "Damper_2").First(); var testconn = damper.ChildObjects.Cast<MgaFCO>().Where(fco => fco.Name.Equals("TestConnector")).First(); CyPhyML.Connector conn = CyPhyMLClasses.Connector.Cast(testconn); Xunit.Assert.Equal(conn.Children.CADDatumCollection.Count(), 3); } finally { project.AbortTransaction(); } } ); }
public MetaLinkProtobuf.Edit AddComponentProtoBufMsg(CyPhyML.ComponentRef componentRef) { CyPhyML.Component component = componentRef.Referred.Component; CyPhyML.CADModel creocadmodel = null; string creomodelname = ""; string creomodeltype = ""; #if DEBUG GMEConsole.Info.WriteLine("AVM: " + component.Attributes.AVMID + " InstanceGUID: " + component.Attributes.InstanceGUID + " Rev: " + component.Attributes.Revision + " Ver: " + component.Attributes.Version); #endif creocadmodel = CyphyMetaLinkUtils.FindCADModelObject(component); if (creocadmodel == null) { GMEConsole.Warning.WriteLine("[" + component.Name + "] is missing a Creo CADModel object!"); return null; } // [1] List<CyPhyML.Resource> Resource_L = new List<CyPhyML.Resource>(); foreach (var item in creocadmodel.DstConnections.UsesResourceCollection) Resource_L.Add(item.DstEnds.Resource); foreach (var item in creocadmodel.SrcConnections.UsesResourceCollection) Resource_L.Add(item.SrcEnds.Resource); if (Resource_L.Count > 0) { creomodelname = Resource_L.First().Attributes.Path; } else { creomodelname = ""; #if DEBUG GMEConsole.Warning.WriteLine("[" + component.Name + "] is not connected to a Resource, therefore does not have a Creo model name."); #endif } if (creomodelname != "") creomodelname = Path.GetFileNameWithoutExtension(creomodelname); // [2] creomodeltype = creocadmodel.Attributes.FileType.ToString().ToUpper(); MetaLinkProtobuf.CADComponentType Component_msg = new MetaLinkProtobuf.CADComponentType { ComponentID = componentRef.Attributes.InstanceGUID, AvmComponentID = component.Attributes.AVMID, Type = creomodeltype, Name = creomodelname, ParametricParameters = new MetaLinkProtobuf.ParametricParametersType(), DisplayName = componentRef.Name }; // [4] InstanceIDToConstraint_Table[componentRef.Attributes.InstanceGUID] = 0; // [5] foreach (var param in creocadmodel.Children.CADParameterCollection) { MetaLinkProtobuf.CADParameterType CADParameter_msg = new MetaLinkProtobuf.CADParameterType { Name = (param.Attributes.ParameterName == "") ? param.Name : param.Attributes.ParameterName, Type = param.Attributes.CADParameterType.ToString(), Value = (param.Attributes.Value == "") ? (param.Attributes.DefaultValue == "") ? "0" : param.Attributes.DefaultValue : param.Attributes.Value }; Component_msg.ParametricParameters.CADParameter.Add(CADParameter_msg); } foreach (var connector in component.Children.ConnectorCollection) { Component_msg.Connectors.Add(new MetaLinkProtobuf.ConnectorType(){ ID = connector.Guid.ToString(), DisplayName = connector.Name }); } MetaLinkProtobuf.Payload Payload_msg = new MetaLinkProtobuf.Payload(); Payload_msg.components.Add(Component_msg); MetaLinkProtobuf.Action Action_msg = new MetaLinkProtobuf.Action { actionMode = MetaLinkProtobuf.Action.ActionMode.REPLACE, payload = Payload_msg }; MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), sequence = 0, }; Edit_msg.origin.Add(GMEOrigin); Edit_msg.actions.Add(Action_msg); Edit_msg.topic.Add(CadAssemblyTopic); Edit_msg.topic.Add(AssemblyID); return Edit_msg; }
// Update existing AVM component - an existing resource has been replaced public void TestUpdateComponentReplaceResource() { SetupTest(); SendInterest("0"); WaitToReceiveMessage(); RunCyPhyMLSync( (project, propagate, interpreter) => { string testCompGuid = null; { interpreter.MgaGateway.PerformInTransaction(() => testCompGuid = (string)((MgaModel)project.RootFolder.ObjectByPath[testComponentPath]).GetAttributeByNameDisp("AVMID")); //SendInterest(CyPhyMetaLink.CyPhyMetaLinkAddon.ComponentTopic, testCompGuid); //WaitToReceiveMessage(); } // TODO propagate.StartEditingComponent { Edit msg = new Edit() { editMode = Edit.EditMode.POST, }; msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add("0"); msg.actions.Add(new edu.vanderbilt.isis.meta.Action()); msg.actions[0].subjectID = testCompGuid; msg.actions[0].actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.UPDATE_CYPHY_COMPONENT; msg.actions[0].alien = new Alien(); msg.actions[0].alien.encodingMode = Alien.EncodingMode.XML; msg.actions[0].alien.encoded = Encoding.UTF8.GetBytes( PrepareComponentUpdateXml(Path.Combine(TestModelDir, @"UpdateComponentData3.xml"), new Dictionary<string, string>() { { "test.asm", Path.Combine(TestModelDir, "") }, })); msg.actions[0].subjectID = testCompGuid; propagate.EditMessageReceived(msg); Application.DoEvents(); interpreter.MgaGateway.PerformInTransaction(() => { var component = ISIS.GME.Dsml.CyPhyML.Classes.Component.Cast((MgaModel)project.RootFolder.ObjectByPath[testComponentPath]); var cadModel = component.Children.CADModelCollection.First(); Assert.Equal(4, cadModel.Children.CADParameterCollection.Count()); Assert.Equal(9, cadModel.Children.CADDatumCollection.Count()); VerifyResources(component, new string[] { "cadxxx\\test.asm", "manufacturing\\damper_2.xml" }); }); } }); }
protected void SendToMetaLinkBridgeAndWaitForAddonToReceive(Edit msg) { msg.guid = Guid.NewGuid().ToString(); testingClient.SendToMetaLinkBridge(msg); Edit recvd; while (addonMessagesQueue.TryTake(out recvd, 5000)) { if (recvd.guid == msg.guid) { return; } } throw new TimeoutException(); }
void TestAssemblySyncDiscardComponent() { SetupTest(); RunCyPhyMLSync( (project, propagate, interpreter) => { MgaFCO testAssembly; MgaFCO testAssemblyHierarchy_2; MgaFCO componentInstance; string componentInstanceGuid; string testHierarchy_1RefId; string testAssemblyRefId; project.BeginTransactionInNewTerr(); try { var filter = project.CreateFilter(); filter.Kind = "ComponentAssembly"; testAssembly = project.AllFCOs(filter).Cast<MgaFCO>() .Where(fco => fco.GetGuidDisp() == new Guid(testAssemblyId).ToString("B")).First(); testAssemblyHierarchy_2 = project.AllFCOs(filter).Cast<MgaFCO>() .Where(fco => fco.GetGuidDisp() == new Guid(testAssemblyHierarchy_2Id).ToString("B")).First(); //hullAndHookHookGuid = (string)((MgaModel)project.RootFolder.ObjectByPath[hullAndHookHookPath]).GetAttributeByNameDisp("AVMID"); componentInstance = testAssembly.ChildObjects.Cast<MgaFCO>().Where(f => f.Name.Contains("Damper")).First(); componentInstanceGuid = componentInstance.GetStrAttrByNameDisp("InstanceGUID"); MgaReference testHierarchy_1Ref = testAssemblyHierarchy_2.ChildObjects.Cast<MgaFCO>().OfType<MgaReference>().First(); testHierarchy_1RefId = new Guid(testHierarchy_1Ref.GetGuidDisp()).ToString("B"); MgaReference testAssemblyRef = ((MgaModel)testHierarchy_1Ref.Referred).ChildObjects.Cast<MgaFCO>().OfType<MgaReference>().First(); testAssemblyRefId = new Guid(testAssemblyRef.GetGuidDisp()).ToString("B"); } finally { project.AbortTransaction(); } interpreter.StartAssemblySync(project, testAssemblyHierarchy_2, 128); Application.DoEvents(); var msg = new Edit(); msg.mode.Add(Edit.EditMode.POST); msg.origin.Add(origin); msg.topic.Add((CyPhyMetaLinkAddon.IdCounter-1).ToString()); var action = new edu.vanderbilt.isis.meta.Action(); action.actionMode = edu.vanderbilt.isis.meta.Action.ActionMode.REMOVE_CYPHY_DESIGN_COMPONENT; action.subjectID = testAssemblyHierarchy_2Id; action.payload = new Payload(); action.payload.components.Add(new CADComponentType() { //ComponentID = hookInstanceGuid, ComponentID = testHierarchy_1RefId + testAssemblyRefId + componentInstanceGuid }); msg.actions.Add(action); SendToMetaLinkBridgeAndWaitForAddonToReceive(msg); WaitForAllMetaLinkMessages(); Application.DoEvents(); project.BeginTransactionInNewTerr(); try { Xunit.Assert.Equal((int)objectstatus_enum.OBJECT_DELETED, componentInstance.Status); } finally { project.AbortTransaction(); } } ); }
protected void SendInterest(params string[] topics) { Edit interest = new Edit(); interest.mode.Add(Edit.EditMode.INTEREST); interest.origin.Add(origin); interest.topic.AddRange(topics); testingClient.SendToMetaLinkBridge(interest); }
public MetaLinkProtobuf.Edit UpdateComponentNameProtoBufMsg(CyPhyML.ComponentRef componentRef, string newName) { MetaLinkProtobuf.CADComponentType Component_msg = new MetaLinkProtobuf.CADComponentType { ComponentID = componentRef.Attributes.InstanceGUID, DisplayName = componentRef.Name }; MetaLinkProtobuf.Payload Payload_msg = new MetaLinkProtobuf.Payload(); Payload_msg.components.Add(Component_msg); MetaLinkProtobuf.Action Action_msg = new MetaLinkProtobuf.Action { actionMode = MetaLinkProtobuf.Action.ActionMode.UPDATE_CYPHY_DESIGN, payload = Payload_msg }; MetaLinkProtobuf.Edit Edit_msg = new MetaLinkProtobuf.Edit { editMode = MetaLinkProtobuf.Edit.EditMode.POST, guid = Guid.NewGuid().ToString(), sequence = 0, }; Edit_msg.origin.Add(GMEOrigin); Edit_msg.actions.Add(Action_msg); SyncedComponentData cdata = syncedComponents[componentRef.ParentContainer.Guid.ToString()]; Edit_msg.topic.Add(cdata.InstanceId); return Edit_msg; }