Skip to content

Commit

Permalink
libdonet: Split Panda's DC Parameter into Attribute and Parameter
Browse files Browse the repository at this point in the history
Read the rust doc strings for more information.
  • Loading branch information
maxrdz committed Feb 21, 2024
1 parent bc1c88b commit 16d48fb
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 32 deletions.
13 changes: 7 additions & 6 deletions libdonet/src/dcatomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@

use crate::dcfield::{DCField, DCFieldInterface};
use crate::dclass::DClass;
use crate::dcparameter::DCParameterField;
use crate::dcparameter::DCParameter;
use crate::dctype::{DCTypeDefinition, DCTypeDefinitionInterface};
use crate::hashgen::DCHashGenerator;
use std::sync::{Arc, Mutex};

/// Represents an atomic field of a Distributed Class.
/// This defines the interface to a DClass object, and is
/// always implemented as a remote procedure call (RPC).
#[derive(Debug)]
pub struct DCAtomicField {
_dcatomicfield_parent: DCField,
elements: Vec<Arc<Mutex<DCParameterField>>>,
elements: Vec<Arc<Mutex<DCParameter>>>,
}

pub trait DCAtomicFieldInterface {
fn new(name: &str, dclass: Arc<Mutex<DClass>>, bogus_field: bool) -> Self;
fn generate_hash(&self, hashgen: &mut DCHashGenerator);

fn get_num_elements(&self) -> usize;
fn get_element(&self, index: usize) -> Option<Arc<Mutex<DCParameterField>>>;
fn get_element(&self, index: usize) -> Option<Arc<Mutex<DCParameter>>>;

fn add_element(&mut self, element: DCParameterField);
fn add_element(&mut self, element: DCParameter);
}

impl DCAtomicFieldInterface for DCAtomicField {
Expand All @@ -62,14 +63,14 @@ impl DCAtomicFieldInterface for DCAtomicField {
self.elements.len()
}

fn get_element(&self, index: usize) -> Option<Arc<Mutex<DCParameterField>>> {
fn get_element(&self, index: usize) -> Option<Arc<Mutex<DCParameter>>> {
match self.elements.get(index) {
Some(pointer) => Some(pointer.clone()), // make a new rc pointer
None => None,
}
}

fn add_element(&mut self, element: DCParameterField) {
fn add_element(&mut self, element: DCParameter) {
self.elements.push(Arc::new(Mutex::new(element)));
}
}
Expand Down
38 changes: 38 additions & 0 deletions libdonet/src/dcattribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// DONET SOFTWARE
// Copyright (c) 2024, Donet Authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License version 3.
// You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;

/// A DC Attribute Field is a type of DC Field which can be found
/// in DC Structs and Distributed Classes.
///
/// Unlike the Panda source, structure elements are called attributes,
/// instead of parameters, as it raises confusion with DC Atomic Field's
/// elements, which are a simpler form of Panda's DC Parameters, as they
/// do not carry DC Keywords, but their corresponding DC Atomic Field does.
#[derive(Debug)]
pub struct DCAttributeField {
_dcattributefield_parent: DCField,
}

/// See issue #22.
impl std::ops::Deref for DCAttributeField {
type Target = DCField;
fn deref(&self) -> &Self::Target {
&self._dcattributefield_parent
}
}
35 changes: 34 additions & 1 deletion libdonet/src/dcfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::datagram::Datagram;
use crate::dcatomic::DCAtomicField;
use crate::dcattribute::DCAttributeField;
use crate::dckeyword::{DCKeywordList, DCKeywordListInterface, IdentifyKeyword};
use crate::dclass::DClass;
use crate::dcmolecular::DCMolecularField;
use crate::dcstruct::DCStruct;
use crate::dctype::{DCTypeDefinition, DCTypeDefinitionInterface};
use crate::globals;
Expand All @@ -26,7 +29,7 @@ use std::sync::{Arc, Mutex};

/// A field of a Distributed Class. The DCField struct is a base for
/// struct and dclass fields. In the DC language, there are three types
/// of field declarations, which are: parameter, atomic, and molecular.
/// of field declarations, which are: attribute, atomic, and molecular.
#[derive(Debug)]
pub struct DCField {
_dcfield_parent: DCKeywordList,
Expand All @@ -42,6 +45,36 @@ pub struct DCField {
bogus_field: bool,
}

/// Enumerator representing the 3 types of fields that inherit DC Field,
/// which can legally be declared within a Distributed Class.
///
/// DC Attribute Fields represent a property, or member, of a structure
/// or class. Attribute fields have a data type assigned to them.
///
/// DC Atomic Fields represent a method of a Distributed Class, which
/// is always implemented as a remote procedure call (RPC). Unlike
/// attribute fields, atomic fields cannot be declared within structs.
///
/// DC Molecular Fields represent a collection of DC Attribute or
/// DC Atomic Fields as one field under one identifier. The parameters
/// of a molecular field are the parameters of all the fields it
/// represents, joined together in the order in which they were declared
/// when the molecular field was declared.
#[derive(Debug)]
pub enum ClassField {
Attribute(DCAttributeField),
Atomic(DCAtomicField),
Molecular(DCMolecularField),
}

/// A different enumerator representing DC Field types used
/// for DC Structs, since they cannot contain DC Atomic Fields.
#[derive(Debug)]
pub enum StructField {
Attribute(DCAttributeField),
Molecular(DCMolecularField),
}

pub trait DCFieldInterface {
fn new(name: &str, dtype: DCTypeDefinition) -> Self;
fn dcfield_generate_hash(&self, hashgen: &mut DCHashGenerator);
Expand Down
52 changes: 41 additions & 11 deletions libdonet/src/dclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;
use crate::dcatomic::{DCAtomicField, DCAtomicFieldInterface};
use crate::dcfield::ClassField;
use crate::globals;
use crate::hashgen::DCHashGenerator;
use multimap::MultiMap;
use std::sync::{Arc, Mutex};
use std::ops::Deref;
use std::sync::{Arc, Mutex, MutexGuard};

pub type FieldName2Field = MultiMap<String, Arc<Mutex<DCField>>>;
pub type FieldId2Field = MultiMap<globals::FieldId, Arc<Mutex<DCField>>>;
pub type FieldName2Field = MultiMap<String, Arc<Mutex<ClassField>>>;
pub type FieldId2Field = MultiMap<globals::FieldId, Arc<Mutex<ClassField>>>;

#[derive(Debug)]
pub struct DClass {
class_name: String,
class_id: globals::DClassId,
is_struct: bool,
is_bogus_class: bool,

class_parents: Vec<Arc<Mutex<DClass>>>,
constructor: Option<Arc<Mutex<DCField>>>,
fields: Vec<Arc<Mutex<DCField>>>,
inherited_fields: Vec<Arc<Mutex<DCField>>>,
constructor: Option<Arc<Mutex<DCAtomicField>>>,
fields: Vec<Arc<Mutex<ClassField>>>,
inherited_fields: Vec<Arc<Mutex<ClassField>>>,
field_name_2_field: FieldName2Field,
field_id_2_field: FieldId2Field,
}
Expand All @@ -51,7 +52,7 @@ pub trait DClassInterface {
fn get_num_parents(&mut self) -> usize;
fn get_parent(&mut self, index: usize) -> Option<Arc<Mutex<DClass>>>;
fn has_constructor(&mut self) -> bool;
fn get_constructor(&mut self) -> Option<Arc<Mutex<DCField>>>;
fn get_constructor(&mut self) -> Option<Arc<Mutex<DCAtomicField>>>;
}

impl DClassInterface for DClass {
Expand All @@ -71,7 +72,36 @@ impl DClassInterface for DClass {
}

fn generate_hash(&mut self, hashgen: &mut DCHashGenerator) {
() // TODO: Implement once hash gen is written
hashgen.add_string(self.get_name());
hashgen.add_int(self.get_num_parents().try_into().unwrap());

for parent_ptr in &self.class_parents {
{
let new_ptr: Arc<Mutex<DClass>> = parent_ptr.clone();
let mut parent: MutexGuard<'_, DClass> = new_ptr.deref().lock().unwrap();

hashgen.add_int(u32::from(parent.get_dclass_id()));
}

if let Some(constructor_ptr) = &self.constructor {
let new_ptr: Arc<Mutex<DCAtomicField>> = constructor_ptr.clone();
let atomic: MutexGuard<'_, DCAtomicField> = new_ptr.deref().lock().unwrap();

atomic.generate_hash(hashgen);
}
}
hashgen.add_int(self.fields.len().try_into().unwrap());

for field_ptr in &self.fields {
let new_ptr: Arc<Mutex<ClassField>> = field_ptr.clone();
let field: MutexGuard<'_, ClassField> = new_ptr.deref().lock().unwrap();

match &field.deref() {
ClassField::Attribute(_) => todo!(),
ClassField::Atomic(atomic) => atomic.generate_hash(hashgen),
ClassField::Molecular(_) => todo!(),
}
}
}

fn set_parent(&mut self, parent: Arc<Mutex<DClass>>) {
Expand Down Expand Up @@ -103,7 +133,7 @@ impl DClassInterface for DClass {
self.constructor.is_some()
}

fn get_constructor(&mut self) -> Option<Arc<Mutex<DCField>>> {
fn get_constructor(&mut self) -> Option<Arc<Mutex<DCAtomicField>>> {
self.constructor.clone()
}
}
3 changes: 2 additions & 1 deletion libdonet/src/dcmolecular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

use crate::dcfield::DCField;

struct DCMolecularField {
#[derive(Debug)]
pub struct DCMolecularField {
_dcmolecularfield_parent: DCField,
}

Expand Down
15 changes: 2 additions & 13 deletions libdonet/src/dcparameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,5 @@
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use crate::dcfield::DCField;

pub struct DCParameterField {
_dcparameterfield_parent: DCField,
}

/// See issue #22.
impl std::ops::Deref for DCParameterField {
type Target = DCField;
fn deref(&self) -> &Self::Target {
&self._dcparameterfield_parent
}
}
#[derive(Debug)]
pub struct DCParameter {}
1 change: 1 addition & 0 deletions libdonet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ cfg_if! {
if #[cfg(feature = "dcfile")] {
pub mod dcarray;
pub mod dcatomic;
pub mod dcattribute;
pub mod dcfield;
pub mod dcfile;
pub mod dckeyword;
Expand Down

0 comments on commit 16d48fb

Please sign in to comment.