Ejemplo n.º 1
1
  public Message send(Message query) {
    int q, r;
    Message best = null;
    byte rcode;

    for (q = 0; q < 20; q++) {
      boolean ok = false;
      for (r = 0; r < resolvers.length; r++) ok |= sendTo(query, r, q);
      if (!ok) break;
      Message m = null;
      synchronized (queue) {
        try {
          queue.wait((quantum + 1) * 1000);
        } catch (InterruptedException e) {
          System.out.println("interrupted");
        }
        if (queue.size() == 0) continue;
        m = (Message) queue.firstElement();
        queue.removeElementAt(0);
        Integer I = (Integer) queue.firstElement();
        queue.removeElementAt(0);
        r = I.intValue();
      }
      if (m == null) invalid[r] = true;
      else {
        rcode = m.getHeader().getRcode();
        if (rcode == Rcode.NOERROR) return m;
        else {
          if (best == null) best = m;
          invalid[r] = true;
        }
      }
    }
    return best;
  }
Ejemplo n.º 2
0
 public static int rules(
     String top, Vector<String> first, Vector<String> second, Vector<String> afia) {
   while (!first.isEmpty() && !second.isEmpty()) {
     if (count % 2 == 0) {
       boolean check = false;
       for (int i = 0; i < first.size(); i++) {
         check =
             matcher(
                 top,
                 first.elementAt(
                     i)); // performs a linear search on player1's whot to see if any matches the
         // top card
         if (check == true) {
           place.addElement(first.elementAt(i));
           first.removeElementAt(i);
           break;
         }
       }
       if (check == false) {
         Random rd = new Random();
         int pick = Math.abs(rd.nextInt(afia.size() - 1)) % (afia.size() - 1);
         first.addElement(afia.elementAt(pick));
         afia.removeElementAt(pick);
       }
     } else {
       boolean check = false;
       for (int i = 0; i < second.size(); i++) {
         check =
             matcher(
                 top,
                 second.elementAt(
                     i)); // performs a linear search on player1's whot to see if any matches the
         // top card
         if (check == true) {
           place.addElement(second.elementAt(i));
           second.removeElementAt(i);
           break;
         }
       }
       if (check == false) {
         Random rd = new Random();
         int pick = Math.abs(rd.nextInt(afia.size() - 1)) % (afia.size() - 1);
         second.addElement(afia.elementAt(pick));
         afia.removeElementAt(pick);
       }
     }
   }
   return 1;
 }
Ejemplo n.º 3
0
 public static ArrayList<Vector> share(
     Vector<String> allgames) { // shares the games to the players
   Vector<String> play1 = new Vector<String>();
   Vector<String> play2 = new Vector<String>();
   ArrayList<Vector> al = new ArrayList<Vector>();
   Random rd = new Random();
   for (int i = 0; i < 4; i++) {
     int first = Math.abs(rd.nextInt(allgames.size() - 1)) % (allgames.size());
     play1.addElement(allgames.elementAt(first));
     allgames.removeElementAt(first);
     int second = Math.abs(rd.nextInt(allgames.size() - 1)) % (allgames.size());
     play2.addElement(allgames.elementAt(second));
     allgames.removeElementAt(second);
   }
   al.add(play1);
   al.add(play2);
   v = allgames;
   return al;
 }
Ejemplo n.º 4
0
  /**
   * This method returns an array of all the tables referenced in the datastore.
   *
   * @param updateable True if the table list should only include updateable tables and false if it
   *     should include all.
   */
  public String[] getTableList(boolean updateable) {

    DSColumnDescriptor col = null;

    Vector tables = new Vector();
    Vector pkey = new Vector();

    for (int i = 0; i < _desc.getColumnCount(); i++) {
      col = _desc.getColumn(i);
      String tableName = col.getTable();
      if (tableName == null) tableName = _desc.getDefaultTable();

      if ((!updateable) || col.isUpdateable()) {
        boolean found = false;
        for (int j = 0; j < tables.size(); j++) {
          if ((tables.elementAt(j)).equals(tableName)) {
            if (col.isPrimaryKey()) pkey.setElementAt(new Boolean(true), j);
            found = true;
            break;
          }
        }
        if (!found && tableName != null) {
          tables.addElement(tableName);
          pkey.addElement(new Boolean(col.isPrimaryKey()));
        }
      }
    }

    if (updateable) {
      for (int i = pkey.size() - 1; i > -1; i--) {
        if (!((Boolean) pkey.elementAt(i)).booleanValue()) tables.removeElementAt(i);
      }
    } else {
      for (int i = 0; i < getAliasCount(); i++) {
        try {
          String table = getTable(i);
          boolean found = false;
          for (int j = 0; j < tables.size(); j++) {
            if ((tables.elementAt(j)).equals(table)) found = true;
          }
          if (!found && table != null) tables.addElement(table);
        } catch (Exception e) {
        }
      }
    }

    String retVal[] = new String[tables.size()];
    tables.copyInto(retVal);

    return retVal;
  }
Ejemplo n.º 5
0
 private synchronized String getLine() {
   while (isOpen && zLines.isEmpty()) {
     try {
       wait(pingInterval);
     } catch (InterruptedException e) {
     }
   }
   String message = isOpen ? "" : null;
   if (isOpen && zLines.size() > 0) {
     message = (String) zLines.elementAt(0);
     zLines.removeElementAt(0);
   }
   return message;
 }
Ejemplo n.º 6
0
 /** @param args the command line arguments */
 public static void main(String[] args) throws IOException {
   File names = new File("C:\\Users\\FRANKCHUKY\\Desktop\\whotnames.txt");
   Scanner sc = new Scanner(names);
   v = new Vector<String>();
   while (sc.hasNext()) {
     String get = sc.nextLine();
     v.addElement(get);
   }
   ArrayList<Vector> view = share(v);
   player1 = view.get(0);
   player2 = view.get(1);
   place.addElement(v.elementAt(0));
   v.removeElementAt(0);
   int determine = rules(place.elementAt(place.size() - 1), player1, player2, v);
   System.out.println(view);
   // System.out.println(player2);
 }
Ejemplo n.º 7
0
 /** @see javax.bluetooth.L2CAPConnection#receive(byte[]) */
 public int receive(byte[] inBuf) throws IOException {
   while (incommingPackets.isEmpty()) {
     if (channelState == CLOSED) throw new IOException("L2CAP Channel is closed.");
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
     }
   }
   byte[] packet = (byte[]) incommingPackets.elementAt(0);
   if (packet != null) {
     incommingPackets.removeElementAt(0);
     int length = inBuf.length;
     if (packet.length < length) length = packet.length;
     System.arraycopy(packet, 0, inBuf, 0, length);
     return length;
   }
   throw new InterruptedIOException();
 }
Ejemplo n.º 8
0
  // Parse cluster info from Clusters table and
  // place all cluster=>plugin info into hashtable
  // Schema :
  // create table Clusters (
  //    Cluster String,
  //    Plugin  String
  // );
  private void parseClusterInfo(
      Hashtable all_clusters, Statement stmt, String community, String node) throws SQLException {

    String sql = null;
    ResultSet rset;
    int number = 0;

    // Grab the plugin groups
    Hashtable plugInGroups = new Hashtable();
    sql = "select GroupName, GroupSequence from GroupMaster";

    number = 0;
    System.out.println(sql);

    rset = stmt.executeQuery(sql);

    while (rset.next()) {
      number++;
      String groupName = rset.getString("GroupName");
      System.out.println(groupName);
      Integer index = new Integer(rset.getInt("GroupSequence"));
      System.out.println(index);
      plugInGroups.put(index, groupName);
    }
    System.out.println("Query returned " + number + " results");

    // Grab the group definitions
    Hashtable groupDefinitions = new Hashtable();
    for (int ind = 0; ind < plugInGroups.size(); ind++) {

      String group = (String) plugInGroups.get(new Integer(ind));
      if (group != null) {
        sql =
            "select InGroupSequence, Plugin from GroupDefinitions"
                + " where GroupName='"
                + group
                + "'";
        rset = stmt.executeQuery(sql);
        System.out.println(sql);
        groupDefinitions.put(group, new Hashtable());

        number = 0;
        while (rset.next()) {
          number++;
          Hashtable plugins = (Hashtable) groupDefinitions.get(group);
          int index = rset.getInt("InGroupSequence");
          String plugIn = rset.getString("Plugin");
          plugins.put(new Integer(index), plugIn);
        }
        System.out.println("Query returned " + number + " results");
      } else {
        break;
      }
    }

    // Add the plugIn groups per cluster to the plugIn vector
    number = 0;
    sql = "select Cluster, PluginGroup from GroupsInCluster";

    if (community != null && node != null) {
      sql =
          sql
              + ", Organizations where Cluster=Organizations.Name and"
              + " Organizations.Community='"
              + community
              + "' and Organizations.Node='"
              + node
              + "'";
    } else if (community != null) {
      sql =
          sql
              + ", Organizations where Cluster=Organizations.Name and"
              + " Organizations.Community='"
              + community
              + "'";
    } else if (node != null) {
      sql = sql + ", Organizations where Organizations.Node='" + node + "'";
    }

    System.out.println(sql);

    number = 0;
    rset = stmt.executeQuery(sql);
    while (rset.next()) {
      number++;
      String cluster_name = rset.getString("Cluster");
      String pluginGroup = rset.getString("PluginGroup");

      Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
      if (current_cluster_plugins == null) {
        current_cluster_plugins = new Vector();
        all_clusters.put(cluster_name, current_cluster_plugins);
      }
      Hashtable groupPlugins = (Hashtable) groupDefinitions.get(pluginGroup);
      for (int ind = 0; ind < groupPlugins.size(); ind++) {
        current_cluster_plugins.addElement(groupPlugins.get(new Integer(ind)));
      }
    }
    System.out.println("Query returned " + number + " results");

    // Grab the plugins that go in every community or every cluster within specific communities
    Vector mdbCommunities = new Vector();

    sql = "select Community from CommunityMaster";
    number = 0;
    System.out.println(sql);

    rset = stmt.executeQuery(sql);

    while (rset.next()) {
      number++;
      String community_name = rset.getString("Community");
      mdbCommunities.addElement(community_name);
    }
    System.out.println("Query returned " + number + " results");

    while (!mdbCommunities.isEmpty()) {
      number = 0;
      String sCommunity = (String) mdbCommunities.firstElement();
      mdbCommunities.removeElementAt(0);

      sql =
          "select Organizations.Name, CommunityPlugins.Plugin from Organizations, CommunityPlugins"
              + " where CommunityPlugins.Community='"
              + sCommunity
              + "'";

      if (!sCommunity.equals("Society")) {
        if (community != null) {
          if (!community.equals(sCommunity)) {
            continue;
          }
        }
        sql = sql + " and Organizations.Community='" + sCommunity + "'";
      } else {
        if (community != null) {
          sql = sql + " and Organizations.Community='" + community + "'";
        }
      }

      if (node != null) {
        sql = sql + " and Organizations.Node='" + node + "'";
      }

      System.out.println(sql);
      number = 0;
      rset = stmt.executeQuery(sql);
      while (rset.next()) {
        number++;
        String cluster_name = rset.getString("Name");
        String plugin = rset.getString("Plugin");

        Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
        if (current_cluster_plugins == null) {
          current_cluster_plugins = new Vector();
          all_clusters.put(cluster_name, current_cluster_plugins);
        }

        current_cluster_plugins.addElement(plugin);
      }
      System.out.println("Query returned " + number + " results");
    }

    // Query for all Cluster/Plugin info and populate 'all_clusters' hashtable

    sql =
        "select Clusters.Cluster, Clusters.Plugin, Clusters.Parameters from Clusters, Organizations"
            + " where Clusters.Cluster=Organizations.Name";

    if (community != null && node != null) {
      sql =
          sql
              + "and Organizations.Node='"
              + node
              + "'"
              + " and Organizations.Community='"
              + community
              + "'";
    } else if (community != null) {
      sql = sql + " and Organizations.Community='" + community + "'";
    } else if (node != null) {
      sql = sql + " and Organizations.Node='" + node + "'";
    }

    System.out.println(sql);

    number = 0;
    rset = stmt.executeQuery(sql);
    while (rset.next()) {
      number++;
      String cluster_name = rset.getString("Cluster");
      String plugin = rset.getString("Plugin");
      String parameters = rset.getString("Parameters");

      // put the parameters into the plugin
      if (parameters != null) {
        plugin = plugin + "(" + parameters + ")";
      }

      Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
      if (current_cluster_plugins == null) {
        current_cluster_plugins = new Vector();
        all_clusters.put(cluster_name, current_cluster_plugins);
      }

      current_cluster_plugins.addElement(plugin);
    }
    System.out.println("Query returned " + number + " results");
  }
Ejemplo n.º 9
0
  protected void processFormattedData(String src) {
    Vector baseName = new Vector();
    Vector names = new Vector();
    Vector values = new Vector();

    int ptr = 0;
    int begin = 0;
    int nestLevel = 0;
    boolean whitespace = false;
    boolean foundEquals = false;
    String lastToken = null;
    String lastBaseName = null;
    String paramName = null;
    char c;

    if (src == null) return;

    while (ptr < src.length()) {
      foundEquals = false;
      whitespace = true;

      do {
        c = src.charAt(ptr);

        whitespace = false;

        if (Character.isSpace(c)) whitespace = true;
        else if (c == '=') {
          whitespace = true;
          foundEquals = true;
        } else if (c == '{') {
          if (lastBaseName != null) {
            baseName.addElement(lastBaseName);
            lastBaseName = null;
          }
          whitespace = true;
        } else if (c == '}') {
          if (baseName.size() > 0) {
            baseName.removeElementAt(baseName.size() - 1);
          }
          whitespace = true;
        }
      } while (whitespace && ++ptr < src.length());

      begin = ptr;
      if (foundEquals && paramName == null) paramName = lastToken;

      if (ptr < src.length()) {
        do {
          c = src.charAt(ptr);

          whitespace = false;

          if (Character.isSpace(c)) whitespace = true;
          else if (c == '=') whitespace = true;
          else if (c == '{') whitespace = true;
          else if (c == '}') whitespace = true;
        } while (!whitespace && ++ptr < src.length());

        lastToken = src.substring(begin, ptr);
        lastBaseName = lastToken;

        if (paramName != null) {
          int l = baseName.size();
          String temp = "";
          if (baseName.size() > 0) {
            for (int i = 0; i < l; i++) {
              temp += (String) (baseName.elementAt(i));
              temp += ".";
            }
          }
          temp += paramName;

          names.addElement(temp);
          values.addElement(lastToken);
          paramName = null;
        }
      }
    }

    argNames = new String[names.size()];
    args = new String[values.size()];

    for (int i = 0; i < names.size(); i++) {
      argNames[i] = (String) (names.elementAt(i));
      args[i] = (String) (values.elementAt(i));
    }
  }
Ejemplo n.º 10
0
  /** Returns the value of the function to the top of the stack. */
  public void returnFromFunction() throws ProgramException {

    // make sure that there's somewhere to return to (old local <> 0)
    if (stackSegment.getValueAt(Definitions.LOCAL_POINTER_ADDRESS) == 0)
      throw new ProgramException(
          "Nowhere to return to in "
              + getCallStack().getTopFunction()
              + "."
              + getCurrentInstruction().getIndexInFunction());

    // done in order to clear the method stack's contents
    workingStackSegment.setStartAddress(getSP());

    bus.send(ram, Definitions.LOCAL_POINTER_ADDRESS, ram, Definitions.R13_ADDRESS); // R13 = lcl
    bus.send(
        stackSegment,
        stackSegment.getValueAt(Definitions.LOCAL_POINTER_ADDRESS) - 5,
        ram,
        Definitions.R14_ADDRESS); // R14 = return address
    bus.send(
        stackSegment,
        getSP() - 1,
        stackSegment,
        ram.getValueAt(Definitions.ARG_POINTER_ADDRESS)); // *arg = return value
    setSP((short) (ram.getValueAt(Definitions.ARG_POINTER_ADDRESS) + 1)); // SP = arg + 1
    bus.send(
        stackSegment,
        ram.getValueAt(Definitions.R13_ADDRESS) - 1,
        ram,
        Definitions.THAT_POINTER_ADDRESS); // that = *(R13 - 1)
    bus.send(
        stackSegment,
        ram.getValueAt(Definitions.R13_ADDRESS) - 2,
        ram,
        Definitions.THIS_POINTER_ADDRESS); // this = *(R13 - 2)
    bus.send(
        stackSegment,
        ram.getValueAt(Definitions.R13_ADDRESS) - 3,
        ram,
        Definitions.ARG_POINTER_ADDRESS); // arg = *(R13 - 3)
    bus.send(
        stackSegment,
        ram.getValueAt(Definitions.R13_ADDRESS) - 4,
        ram,
        Definitions.LOCAL_POINTER_ADDRESS); // lcl = *(R13 - 4)

    // removes the top function from the call stack
    callStack.popFunction();

    // check whether there is a "calling frame"
    if (stackFrames.size() > 0) {
      // retrieve stack frame address of old function
      int frameAddress = ((Integer) stackFrames.lastElement()).intValue();
      stackFrames.removeElementAt(stackFrames.size() - 1);
      workingStackSegment.setStartAddress(frameAddress);

      // disable non relevant range of the local segment - enable only the locals
      // of the function that we returned to.
      localSegment.setEnabledRange(
          Math.max(localSegment.getStartAddress(), Definitions.STACK_START_ADDRESS),
          frameAddress - 1,
          true);

      // enable in the arg segment only the number of args that were sent to the function
      // that we returned to.
      argSegment.setEnabledRange(
          argSegment.getStartAddress(), localSegment.getStartAddress() - 6, true);

      // enable this, that according to their retrieved pointers
      thisSegment.setEnabledRange(
          Math.max(thisSegment.getStartAddress(), Definitions.HEAP_START_ADDRESS),
          Definitions.HEAP_END_ADDRESS,
          true);
      thatSegment.setEnabledRange(
          Math.max(thatSegment.getStartAddress(), Definitions.HEAP_START_ADDRESS),
          Definitions.SCREEN_END_ADDRESS,
          true);
    } /* else {
      	error("Nowhere to return to");
      } */
    // Allow return if we previously had "function" even with no call -
    // For the SimpleFunction test

    short returnAddress = ram.getValueAt(Definitions.R14_ADDRESS);
    if (returnAddress == VMProgram.BUILTIN_FUNCTION_ADDRESS) {
      staticSegment.setEnabledRange(0, -1, true); // empty static segment
      builtInFunctionsRunner.returnToBuiltInFunction(popValue(METHOD_STACK));
    } else if (returnAddress >= 0 && returnAddress < program.getSize()) {
      // sets the static segment range
      if (stackFrames.size() > 0) {
        setStaticRange(callStack.getTopFunction());
      } else {
        staticSegment.setStartAddress(Definitions.VAR_START_ADDRESS);
        staticSegment.setEnabledRange(
            Definitions.VAR_START_ADDRESS, Definitions.VAR_END_ADDRESS - 1, true);
      }
      program.setPC((short) (returnAddress - 1)); // set previousPC correctly
      program.setPC(returnAddress); // pc = *sp
    } else {
      error("Illegal return address");
    }
  }