[P2play-commit] SF.net SVN: p2play: [10] trunk/P2Play/src/org/p2play
Status: Pre-Alpha
Brought to you by:
tisoft
|
From: <ti...@us...> - 2006-12-26 22:59:40
|
Revision: 10
http://p2play.svn.sourceforge.net/p2play/?rev=10&view=rev
Author: tisoft
Date: 2006-12-26 14:59:39 -0800 (Tue, 26 Dec 2006)
Log Message:
-----------
transfered from university cvs
added license header
changed package name to org.p2play.*
Modified Paths:
--------------
trunk/P2Play/src/org/p2play/net/region/RegionInstance.java
Added Paths:
-----------
trunk/P2Play/src/org/p2play/scripting/
trunk/P2Play/src/org/p2play/scripting/pnuts/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/ScriptAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/Chat.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/ChatAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/
trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/Statistic.java
trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/StatisticAPI.java
trunk/P2Play/src/org/p2play/scripting/pnuts/manager/
trunk/P2Play/src/org/p2play/scripting/pnuts/manager/PackageHandler.java
trunk/P2Play/src/org/p2play/util/EmptyIterator.java
trunk/P2Play/src/org/p2play/util/ListMap.java
Removed Paths:
-------------
trunk/P2Play/src/org/p2play/scripted/
Modified: trunk/P2Play/src/org/p2play/net/region/RegionInstance.java
===================================================================
--- trunk/P2Play/src/org/p2play/net/region/RegionInstance.java 2006-12-26 22:18:35 UTC (rev 9)
+++ trunk/P2Play/src/org/p2play/net/region/RegionInstance.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -20,9 +20,7 @@
import java.io.Serializable;
-import org.p2play.net.region.Region;
-
public class RegionInstance implements Serializable{
private Region region;
Copied: trunk/P2Play/src/org/p2play/scripting (from rev 9, trunk/P2Play/src/org/p2play/scripted)
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/ScriptAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/ScriptAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/ScriptAPI.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api;
+
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+
+/**
+ *
+ * @author Johannes Lintner, Markus Heberling
+ *
+ */
+public interface ScriptAPI
+{
+ /**
+ * Add this script API to the package Handler
+ * @param packageHandler
+ */
+ public void registerAPI(PackageHandler packageHandler);
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/Chat.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/Chat.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/Chat.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.chat;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.TreeSet;
+
+import pnuts.lang.Context;
+import pnuts.lang.PnutsFunction;
+
+public class Chat {
+ public static final String TEXT="text";
+ public static final String LEAVE="leave";
+ public static final String JOIN="join";
+
+ private PnutsFunction textCallback;
+
+ private Context context;
+
+ private String channel;
+
+ private String user;
+
+ private ChatAPI chatAPI;
+
+ private TreeSet<String> users;
+
+ public Chat(ChatAPI chatAPI, String channel, String user,
+ PnutsFunction callback, Context context) {
+ super();
+ this.chatAPI = chatAPI;
+ this.channel = channel;
+ this.user = user;
+ this.textCallback = callback;
+ this.context = context;
+ users=new TreeSet<String>();
+ }
+
+ public String getChannel() {
+ return channel;
+ }
+
+ public void sendText(String text) {
+ chatAPI.sendText(channel, user, text);
+ }
+
+ public void leave() {
+ chatAPI.leaveChannel(this);
+ }
+
+ public List<String> getUsers() {
+ return new LinkedList<String>(users);
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ void userJoined(String user){
+ users.add(user);
+ if (textCallback != null)
+ textCallback.call(new Object[] {Chat.JOIN, this, user, "" }, context);
+ }
+
+ void userLeft(String user){
+ users.remove(user);
+ if (textCallback != null)
+ textCallback.call(new Object[] {Chat.LEAVE, this, user, "" }, context);
+ }
+
+ void receiveMessage(String user, String text) {
+ if (textCallback != null)
+ textCallback.call(new Object[] {Chat.TEXT, this, user, text }, context);
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/ChatAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/ChatAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/chat/ChatAPI.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,241 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.chat;
+
+import java.util.Iterator;
+
+import org.p2play.scripting.pnuts.api.ScriptAPI;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+import org.p2play.util.ListMap;
+
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+import rice.p2p.commonapi.Node;
+import rice.p2p.commonapi.NodeHandle;
+import rice.p2p.scribe.Scribe;
+import rice.p2p.scribe.ScribeClient;
+import rice.p2p.scribe.ScribeContent;
+import rice.p2p.scribe.ScribeImpl;
+import rice.p2p.scribe.Topic;
+/**
+ *
+ * @author Marco Giessmann
+ *
+ */
+public class ChatAPI implements ScribeClient, ScriptAPI {
+ private ListMap<String, Chat> callbackMap;
+
+ private Node node;
+
+ private Scribe scribe;
+
+ public ChatAPI(Node node) {
+ this.node = node;
+ this.scribe = new ScribeImpl(node, "scribe.chat");
+ callbackMap = new ListMap<String, Chat>();
+ }
+
+ public void registerAPI(PackageHandler scriptAPI) {
+ Package chatPackage = scriptAPI.getRootPackage();//TODO: Root package for now
+
+ PackageHandler.addFunction(chatPackage, "joinChannel",
+ new PnutsFunction() {
+
+ @Override
+ public boolean defined(int narg) {
+ return narg == 3;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return joinChannel(context, (String) args[0],
+ (String) args[1], (PnutsFunction) args[2]);
+
+ }
+ });
+
+ }
+
+ public Chat joinChannel(Context context, String channel, String user,
+ PnutsFunction callback) {
+ Chat chat = new Chat(this, channel, user, callback, context);
+ synchronized (callbackMap) {
+ callbackMap.add(channel, chat);
+ }
+ // System.out.println(channel + ": (join) " + user);
+ scribe.subscribe(new Topic(node.getIdFactory(), channel), this);
+ scribe.publish(new Topic(node.getIdFactory(), chat.getChannel()),
+ new JoinMessage(chat.getChannel(), chat.getUser()));
+ return chat;
+ }
+
+ void sendText(String channel, String user, String text) {
+ // System.out.println(channel + ": " + user + " says: " + text);
+ scribe.publish(new Topic(node.getIdFactory(), channel),
+ new ChatTextMessage(channel, user, text));
+ }
+
+ void leaveChannel(Chat chat) {
+ synchronized (callbackMap) {
+ callbackMap.remove(chat.getChannel(), chat);
+ if (callbackMap.isEmpty(chat.getChannel())) {
+ scribe.publish(
+ new Topic(node.getIdFactory(), chat.getChannel()),
+ new LeaveMessage(chat.getChannel(), chat.getUser()));
+ scribe.unsubscribe(new Topic(node.getIdFactory(), chat
+ .getChannel()), this);
+ }
+ }
+ // System.out.println(channel + ": (leave) " + user);
+ }
+
+ private void receiveText(String channel, String user, String text) {
+ synchronized (callbackMap) {
+ Iterator<Chat> iterator = callbackMap.iterator(channel);
+
+ while (iterator.hasNext()) {
+ Chat fc = iterator.next();
+
+ fc.receiveMessage(user, text);
+ }
+ }
+
+ }
+
+ public boolean anycast(Topic topic, ScribeContent content) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void childAdded(Topic topic, NodeHandle child) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void childRemoved(Topic topic, NodeHandle child) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void deliver(Topic topic, ScribeContent content) {
+ if (content instanceof ChatTextMessage) {
+ ChatTextMessage chatMessage = (ChatTextMessage) content;
+
+ receiveText(chatMessage.getChannel(), chatMessage.getUser(),
+ chatMessage.getText());
+ } else if (content instanceof JoinMessage) {
+ JoinMessage joinMessage = (JoinMessage) content;
+ synchronized (callbackMap) {
+ Iterator<Chat> iterator = callbackMap.iterator(joinMessage
+ .getChannel());
+
+ while (iterator.hasNext()) {
+ Chat fc = iterator.next();
+
+ fc.userJoined(joinMessage.getUser());
+ }
+ }
+ } else if (content instanceof LeaveMessage) {
+ LeaveMessage leaveMessage = (LeaveMessage) content;
+ synchronized (callbackMap) {
+ Iterator<Chat> iterator = callbackMap.iterator(leaveMessage
+ .getChannel());
+
+ while (iterator.hasNext()) {
+ Chat fc = iterator.next();
+
+ fc.userJoined(leaveMessage.getUser());
+ }
+ }
+ }
+
+ }
+
+ public void subscribeFailed(Topic topic) {
+ scribe.subscribe(topic, this);
+ }
+}
+
+class ChatTextMessage implements ScribeContent {
+ private String channel;
+
+ private String user;
+
+ private String text;
+
+ public ChatTextMessage(String channel, String user, String text) {
+ super();
+ this.channel = channel;
+ this.user = user;
+ this.text = text;
+ }
+
+ public String getChannel() {
+ return channel;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public String getText() {
+ return text;
+ }
+}
+
+class JoinMessage implements ScribeContent {
+ private String channel;
+
+ private String user;
+
+ public JoinMessage(String channel, String user) {
+ super();
+ this.channel = channel;
+ this.user = user;
+ }
+
+ public String getChannel() {
+ return channel;
+ }
+
+ public String getUser() {
+ return user;
+ }
+}
+
+class LeaveMessage implements ScribeContent {
+ private String channel;
+
+ private String user;
+
+ public LeaveMessage(String channel, String user) {
+ super();
+ this.channel = channel;
+ this.user = user;
+ }
+
+ public String getChannel() {
+ return channel;
+ }
+
+ public String getUser() {
+ return user;
+ }
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/Statistic.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/Statistic.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/Statistic.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.statistic;
+
+import java.net.InetSocketAddress;
+
+public class Statistic {
+ private int upload = 0;
+ private int download = 0;
+ private int nextCurrentUpload = 0;
+ private int nextCurrentDownload = 0;
+ private int currentUpload = 0;
+ private int currentDownload = 0;
+ private long lastNullifyUpload;
+ private long lastNullifyDownload;
+ private int packageCountUp;
+ private int packageCountDown;
+
+
+ public synchronized int getCurrentDownload() {
+ return currentDownload;
+ }
+ public synchronized int getCurrentUpload() {
+ return currentUpload;
+ }
+ public synchronized int getDownload() {
+ return download;
+ }
+ public synchronized int getUpload() {
+ return upload;
+ }
+
+ synchronized void dataSent(int msgAddress, short msgType, InetSocketAddress socketAddress, int size, int wireType) {
+ // TODO Auto-generated method stub
+ if(System.currentTimeMillis()-lastNullifyUpload>1000){
+ currentUpload=nextCurrentUpload;
+ nextCurrentUpload=0;
+ lastNullifyUpload=System.currentTimeMillis();
+ }
+ upload+=size;
+ nextCurrentUpload+=size;
+ packageCountUp++;
+ }
+
+ synchronized void dataReceived(int msgAddress, short msgType, InetSocketAddress socketAddress, int size, int wireType) {
+ if(System.currentTimeMillis()-lastNullifyDownload>1000){
+ currentDownload=nextCurrentDownload;
+ nextCurrentDownload=0;
+ lastNullifyDownload=System.currentTimeMillis();
+ }
+ download+=size;
+ nextCurrentDownload+=size;
+ packageCountDown++;
+ }
+
+ public synchronized int getPackageSize(){
+ if(packageCountDown+packageCountUp==0)
+ return 0;
+ else
+ return (download+upload)/(packageCountDown+packageCountUp);
+ }
+
+ @Override
+ public String toString() {
+ return "Network Statistics:"+" DOWN: "+getCurrentDownload()+" UP: "+getCurrentUpload()+" DOWN: "+getDownload()+" UP: "+getUpload();
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/StatisticAPI.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/StatisticAPI.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/api/statistic/StatisticAPI.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.api.statistic;
+
+import java.net.InetSocketAddress;
+
+import org.p2play.scripting.pnuts.api.ScriptAPI;
+import org.p2play.scripting.pnuts.manager.PackageHandler;
+
+import pnuts.lang.Context;
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+import rice.p2p.commonapi.Node;
+import rice.pastry.NetworkListener;
+import rice.pastry.dist.DistPastryNode;
+
+public class StatisticAPI implements NetworkListener, ScriptAPI{
+ private Statistic statistic;
+
+ public StatisticAPI(Node node) {
+ statistic=new Statistic();
+ //only add if we have a DistPastryNode
+ if(node instanceof DistPastryNode)
+ ((DistPastryNode) node).addNetworkListener(this);
+ }
+
+ public void registerAPI(PackageHandler packageHandler) {
+ Package chatPackage = packageHandler.getRootPackage();
+ //TODO: Root package for now
+
+ PackageHandler.addFunction(chatPackage, "getNetStats",
+ new PnutsFunction() {
+
+ @Override
+ public boolean defined(int narg) {
+ return narg == 0;
+ }
+
+ @Override
+ protected Object exec(Object[] args, Context context) {
+ return getNetStats();
+
+ }
+ });
+
+
+ }
+
+ public Statistic getNetStats(){
+ return statistic;
+ }
+
+ public void channelOpened(InetSocketAddress addr, int reason) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void channelClosed(InetSocketAddress addr) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void dataSent(int msgAddress, short msgType, InetSocketAddress socketAddress, int size, int wireType) {
+ statistic.dataSent(msgAddress, msgType, socketAddress, size, wireType);
+ }
+
+ public void dataReceived(int msgAddress, short msgType, InetSocketAddress socketAddress, int size, int wireType) {
+ statistic.dataReceived(msgAddress, msgType, socketAddress, size, wireType);
+ }
+}
Added: trunk/P2Play/src/org/p2play/scripting/pnuts/manager/PackageHandler.java
===================================================================
--- trunk/P2Play/src/org/p2play/scripting/pnuts/manager/PackageHandler.java (rev 0)
+++ trunk/P2Play/src/org/p2play/scripting/pnuts/manager/PackageHandler.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.scripting.pnuts.manager;
+
+
+import java.util.HashMap;
+import java.util.NoSuchElementException;
+
+import pnuts.lang.Package;
+import pnuts.lang.PnutsFunction;
+
+/**
+ * @author Johannes Lintner, Markus Heberling
+ *
+ * PackageHandler stores a set of PnutsFunctions in packages and provides
+ * access.
+ *
+ */
+public class PackageHandler {
+ public String ROOT = "root"+(count++);
+
+ private HashMap<String, Package> pkgMap = new HashMap<String, Package>();
+
+ private static int count = 0;
+
+ public PackageHandler() {
+ newPackage(ROOT,null);
+
+ newPackage("self", getRootPackage());
+ }
+
+ public PackageHandler(Package rootPackage) {
+ ROOT=rootPackage.getName();
+ pkgMap.put(ROOT, rootPackage);
+ }
+
+ /**
+ * Creates a new Package, named pPkgName. If parentPackage is NULL or
+ * "ROOT", the parentPackage will be the GLOBAL rootPackage.
+ *
+ * @param pPkgName
+ * the name of the Package
+ * @param parentPackage
+ * the parent package of the new package
+ *
+ * @return Package returns the new package
+ */
+ public Package newPackage(String pkgName, Package parentPackage) {
+ Package pkg = null;
+
+ if (pkgName.equalsIgnoreCase(ROOT) && parentPackage == null) {
+ pkg = new Package(pkgName);
+ } else {
+ pkgName=pkgName.intern();
+ pkg = new Package(pkgName, parentPackage);
+ parentPackage.set(pkgName, pkg);
+ }
+
+ pkgMap.put(pkgName, pkg);
+ return pkg;
+ }
+
+ /**
+ * Puts a custom function, named by pFunctionName and the defined by the
+ * functionDeclaration (Object) to the Package, defined by pPackage.
+ *
+ * @param pPackage
+ * the Package, the new function should be added to.
+ * @param pFunctionName
+ * the name of the function, with which the function can be
+ * called from the scripts.
+ * @param pObject
+ * to Object casted PnutsFunction, the definition (logic) of the
+ * function
+ */
+ public void setMethod(Package pkg, String pfn, Object obj) {
+ pkg.set(pfn, obj);
+ }
+
+ /**
+ * returns the package specified by pPkgName. Searches in the HashMap<String,
+ * Package> for the given package
+ *
+ * @param pPkgName
+ * the name (key) of the wanted package
+ * @return Package the found package
+ */
+ public Package getPackage(String pkgName) {
+ if(pkgName.equalsIgnoreCase("root"))
+ throw new IllegalArgumentException("Package name may not be ROOT.");
+
+ Package pkg = pkgMap.get(pkgName);
+ if (pkg == null)
+ throw new NoSuchElementException("No such Package: " + pkgName);
+ return pkg;
+ }
+
+ /**
+ * registers a package in the HashMap of Packages.
+ *
+ * @param pkgName,
+ * The name of the package, which should be registred.
+ * @param newPackage,
+ * The Package, which should be registered.
+ */
+ public void putPackage(String pkgName, Package newPackage) {
+ pkgMap.put(pkgName, newPackage);
+ }
+
+ /**
+ * Gives back a HashMap<String, Package>, containing the names of the
+ * Packages as keys and the packages itself as values. With Object []
+ * packages = ScriptAPI.getAllPackages().keySet().toArray(); you get an
+ * Array of the keys of all packages. Read out e.g. with for (int index = 0;
+ * index < functions.length; index++)
+ * System.out.println(functions[index].toString());
+ *
+ * @return HashMap<String, Package>, containing the names as keys and the
+ * packages as values
+ */
+ public HashMap<String, Package> getAllPackages() {
+ if (pkgMap == null || pkgMap.isEmpty())
+ throw new NoSuchElementException("There are no Packages available");
+ return pkgMap;
+ }
+
+ public Package getRootPackage() {
+ return getPackage(ROOT);
+ }
+
+ public static void addFunction(Package package1, String name,
+ PnutsFunction function) {
+ package1.set(name.intern(), function);
+ }
+}
Added: trunk/P2Play/src/org/p2play/util/EmptyIterator.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/EmptyIterator.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/EmptyIterator.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+public class EmptyIterator<E> implements Iterator<E> {
+ public boolean hasNext() {
+ return false;
+ }
+
+ public E next() {
+ throw new NoSuchElementException();
+ }
+
+ public void remove() {
+
+ }
+
+}
\ No newline at end of file
Added: trunk/P2Play/src/org/p2play/util/ListMap.java
===================================================================
--- trunk/P2Play/src/org/p2play/util/ListMap.java (rev 0)
+++ trunk/P2Play/src/org/p2play/util/ListMap.java 2006-12-26 22:59:39 UTC (rev 10)
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2006 P2Play.org
+ * All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.p2play.util;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javolution.util.FastList;
+import javolution.util.FastMap;
+
+public class ListMap<K, V> implements Iterable<K> {
+ private Map<K, List<V>> map;
+
+ public ListMap() {
+ map = new FastMap<K, List<V>>();
+ }
+
+ public void add(K key, V value) {
+ synchronized (map) {
+ if (!getOrCreateList(key).contains(value)) {
+ getOrCreateList(key).add(value);
+ }
+ }
+ }
+
+ public void remove(K key, V value) {
+ synchronized (map) {
+ getOrCreateList(key).remove(value);
+ if (isEmpty(key)) {
+ map.remove(key);
+ }
+ }
+ }
+
+ public List<V> remove(K key) {
+ return map.remove(key);
+ }
+
+ public List<V> getList(K key) {
+ synchronized (map) {
+ return map.get(key);
+ }
+ }
+
+ private List<V> getOrCreateList(K key) {
+ synchronized (map) {
+ List<V> list = getList(key);
+ if (list == null) {
+ list = new FastList<V>();
+ map.put(key, list);
+ }
+
+ return list;
+ }
+ }
+
+ public boolean isEmpty(K key) {
+ synchronized (map) {
+ return getList(key) == null || getList(key).isEmpty();
+ }
+ }
+
+ public Set<K> keySet() {
+ synchronized (map) {
+ return map.keySet();
+ }
+ }
+
+ public void clear() {
+ synchronized (map) {
+ map.clear();
+ }
+ }
+
+ public boolean containsKey(K key) {
+ synchronized (map) {
+ return map.containsKey(key) && map.get(key) != null
+ && !((List) map.get(key)).isEmpty();
+ }
+ }
+
+ public boolean contains(K key, V value) {
+ synchronized (map) {
+ return getOrCreateList(key).contains(value);
+ }
+ }
+
+ /**
+ * Iterator of all values associated with the given key
+ *
+ * @param key
+ * @return
+ */
+ public Iterator<V> iterator(K key) {
+ synchronized (map) {
+ if (containsKey(key)) {
+ return getList(key).iterator();
+ } else {
+ return new EmptyIterator<V>();
+ }
+ }
+ }
+
+ /**
+ * Iterator of all values associated with the given key changes
+ *
+ * @param key
+ * @return
+ */
+ public Iterator<V> clonedIterator(K key) {
+ synchronized (map) {
+ if (containsKey(key)) {
+ return new FastList<V>(getList(key)).iterator();
+ } else {
+ return new EmptyIterator<V>();
+ }
+ }
+ }
+
+ /**
+ * cloned Iterator of the keys
+ *
+ * @return
+ */
+ public Iterator<K> clonedIterator() {
+ return new HashSet<K>(keySet()).iterator();
+ }
+
+ /**
+ * Iterator of the keys
+ *
+ * @return
+ */
+ public Iterator<K> iterator() {
+ return keySet().iterator();
+ }
+
+ @Override
+ public String toString() {
+ return map.toString();
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|