Sending an Object through Socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    Sending an Object through Socket

    Good day,

    I have an object, that contains a hashmap, that is dynamically utilized on client and server data transfer. that object may contain a string, an arraylist, or an object itself.

    Based on the observation, i found out that, when the database will be reset, the client could still able to send the reliable data(through showing on the stream - println) but when the server received it, the essential list of data have lost.

    What can you advice about this scenario?

    Im looking forward to your replies experts :)
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Hey sukatoa!

    Please post your code so we can have a closer look. Passers-by may also jump in if they have an idea what is happening through your code.

    In a bit!

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Here's the code

      Code:
      package com.data;
      
      import java.io.File;
      import java.io.Serializable;
      import java.util.HashMap;
      
      /**
       * This class is capable of handling objects represented in Key-Value pair where
       * the key is assumed to be unique for an accurate retrieve mechanism. This class
       * is serializable and can be passed to network using ObjectOutputStream.
       * <br><br>
       * @since SWF 1.3
       */
      public class Properties implements Serializable {
      
          private HashMap<Object,Object> p = new HashMap<Object,Object>();
          private Boolean locked = new Boolean(false);
      
          /**
           * Default contructor: no objects are stored by default.
           */
          public Properties(){
              //
          }
      
          /**
           * Another contructor: receives a Map object that will be copied and used
           * as a reference for the default object handled in this instance
           * @param p Map object that contains desired properties to be handled by default.
           */
          public Properties(HashMap<Object,Object> p){
              this.p = p;
          }
      
          /**
           * This will add a property in this instance and checks if the key is already
           * exists or not in the pool of properties within this instance.
           * @param key unique object
           * @param value object that will be referred by the unique key.
           * @return true if the given key is unique, false otherwise.
           */
          public Boolean setProperty(Object key,Object value){
              if(!locked){
                  if(!p.containsKey(key)){
                      p.put(key, value);
                      return true;
                  }
              }
              return new Boolean(false);
          }
      
          /**
           * This will update a property in this instance and checks if the key is already
           * exists or not in the pool of properties within this instance.
           * @param key unique object
           * @param value object that will be referred by the unique key.
           * @return true if the given key exists, false otherwise.
           */
          public  Boolean updateProperty(Object key,Object value){
              if(!locked){
                  if(p.containsKey(key)){
                      p.put(key, value);
                      return true;
                  }
              }
              return new Boolean(false);
          }
      
          /**
           * Returns the value(object) under the given key.
           * @param key unique key existing in this instance
           * @return the value(object) under the given key, null if the key doesn't exists.
           */
          public  Object getValue(Object key){
              return p.get(key);
          }
      
          /**
           * Returns the key(object) that is referring to the given value(object). If 2 or more
           * value(object) are the same and existing in this instance, the first one will be returned.
           * @param value used as a reference to find the key(object) that refers to it.
           * @return the key(object) that refers to the given value, null if not existing
           */
          public  Object getKey(Object value){
              for(Object key:p.keySet()){
                  if(p.get(key).equals(value)){
                      return key;
                  }
              }
              return null;
          }
      
          /**
           * Checks if the given key(object) exists within this instance.
           * @param key used to check for its existency.
           * @return true if exists, false otherwise.
           */
          public  Boolean isKeyExists(Object key){
              return new Boolean(p.containsKey(key));
          }
      
          /**
           * If invoked, all transactions that may modify the values inside of this instance
           * will be discarded
           */
          public void lock(){
              this.locked = new Boolean(true);
          }
      
          /**
           * If invoked, all transactions that may modify the values inside of this instance
           * will be granted
           */
          public void unlock(){
              this.locked = new Boolean(false);
          }
      
          /**
           * This Object(as an instance) will be persisted @ the specified file location given
           * @param loc filepath to be saved
           * @return true if successful, false otherwise
           */
          public Boolean persistProperties(File loc){
              return new PersistentObject().persistObject(p, loc);
          }
      
          /**
           * Replaces the current configuration and contents of this instance into an Object
           * that represents the same as this instance structure given the file location
           * @param loc where the file that represents an Object that is strictly the same with
           * this instance.
           * @return true if successful, false otherwise
           */
          public Boolean retrieveProperties(File loc){
              PersistentObject po = new PersistentObject();
              this.p = (HashMap<Object,Object>)po.retrieveObject(loc);
              return new Boolean(this.p!=null);
          }
      
          /**
           * Returns the container where the objects stored
           * @return the container represented in Map
           */
          public HashMap<Object,Object> getProperties(){
              return p;
          }
      
          /**
           * Replaces the current container with the given Map instance
           * @param p
           */
          public void setProperties(HashMap<Object,Object> p){
              this.p = p;
          }
      
          /**
           * Removes a specific object stored in the container handled by this instance
           * @param key that also holds the value
           */
          public void remove(Object key){
              this.p.remove(key);
          }
      
          /**
           * Cleans up the container: removes all objects that this instance is holding on
           */
          public void reset(){
              this.p.clear();
          }
      }

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        i found an alternative one... since, it can carry an ArrayList referenced by a string...

        Let say, setProperty("my list", ArrayList );
        in the server side, the value of mylist will appear once, and the rest is empty...

        I found an alternative, instead of using a list, i've used an array of object or an equivalent of ArrayList.toArr ay()... and now the server receives the reliable data... im confused,

        setProperty("my list",Object[] datafromlist);

        Why is this so?

        Comment

        Working...