001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.gaming;
025:
026: import org.myoodb.*;
027: import org.myoodb.objects.*;
028: import org.myoodb.collectable.*;
029:
030: public class Game {
031: public static void main(String args[]) throws Exception {
032: org.myoodb.MyOodbDatabase.setGameConfigurationFlag(true);
033:
034: //org.myoodb.util.Logger.setDebugEnabled(true);
035: org.myoodb.core.AbstractDatagramSocket.setBufferSize(1024 * 32);
036: org.myoodb.core.AbstractDatagramSocket.setRequestRetryTime(100);
037: org.myoodb.core.AbstractDatagramSocket
038: .setResponseRetryTime(1500);
039: org.myoodb.core.AbstractDatagramSocket
040: .setTunnelKeepAliveTime(250);
041: org.myoodb.core.AbstractDatagramSocket
042: .setConnectionTimeout(10000);
043:
044: int lPort = -1;
045: String lUsername = "local";
046: String lPassword = "local";
047:
048: int rPort = -1;
049: String rHost = "localhost";
050: String rUsername = "remote";
051: String rPassword = "remote";
052:
053: for (int i = 0; i < args.length; i++) {
054: if (args[i].startsWith("-localLogin=")) {
055: String usernamepassword = args[i].substring(12);
056: String[] tokens = usernamepassword.split(":");
057: lUsername = tokens[0];
058: lPassword = tokens[1];
059: } else if (args[i].startsWith("-localPort=")) {
060: lPort = Integer.parseInt(args[i].substring(11));
061: } else if (args[i].startsWith("-remoteLogin=")) {
062: String usernamepassword = args[i].substring(13);
063: String[] tokens = usernamepassword.split(":");
064: rUsername = tokens[0];
065: rPassword = tokens[1];
066: } else if (args[i].startsWith("-remoteHost=")) {
067: rHost = args[i].substring(12);
068: } else if (args[i].startsWith("-remotePort=")) {
069: rPort = Integer.parseInt(args[i].substring(12));
070: }
071: }
072:
073: if ((lPort == -1) || (rPort == -1)) {
074: System.out
075: .println("usage: -localPort=# -localLogin=username:password -remotePort=# -remoteLogin=username:password");
076: System.exit(0);
077: }
078:
079: StreamProtocol.register(); // use optimized object protocol definitions
080:
081: //
082: //
083: // Local Setup / Access
084: //
085: //
086:
087: MyOodbDatabase localDatabase = connectLocal(lUsername,
088: lPassword);
089:
090: Person localPerson = (Person) localDatabase
091: .createObject(org.myoodb.objects.PersonDbImpl.class);
092: localPerson.setName("Person: " + lPort);
093:
094: // XXX: create a container of people and add local person
095: HashMap people = (HashMap) localDatabase.createRoot("People",
096: org.myoodb.collectable.HashMapDbImpl.class);
097: people.put(localPerson.getName(), localPerson);
098:
099: // XXX: if you want, re-get as the raw object for performance ( use this only for local reference )
100: localPerson = (Person) localDatabase.getObject(localPerson);
101:
102: //
103: //
104: // Remote Setup / Access
105: //
106: //
107:
108: // XXX: peer configuration requires you to create user account for them to connect back
109: org.myoodb.core.MyOodbManager.getTheManager().getUserManager()
110: .newUser(rUsername, rPassword);
111:
112: // XXX: peer configuration requires you to create the socket ( one tunnel for every peer host )
113: java.net.DatagramSocket socket = new java.net.DatagramSocket(
114: lPort);
115: socket.setReuseAddress(true);
116: org.myoodb.core.MyOodbTunnelUdp.start(rHost, socket);
117: MyOodbDatabase remoteDatabase = connectRemote(rHost, rPort,
118: rUsername, rPassword);
119:
120: // XXX: get peer's people container and get their local person
121: people = (HashMap) remoteDatabase.getRoot("People");
122: while (people == null) {
123: people = (HashMap) remoteDatabase.getRoot("People");
124: Thread.sleep(100);
125: }
126: Person remotePerson = (Person) people.get("Person: " + rPort);
127:
128: //
129: //
130: // Start the Game
131: //
132: //
133:
134: // XXX: since we have the local object, we only need to set the location attribute once
135: java.awt.Point localPoint = new java.awt.Point(0, 0);
136: localPerson.setLocation(localPoint);
137:
138: for (int i = 0; i < 100000; i++) {
139: // XXX: change local location
140: localPoint.setLocation(i, i);
141:
142: // XXX: get remote location
143: java.awt.Point remotePoint = remotePerson.getLocation();
144:
145: if ((i % 100) == 0) {
146: System.out.println("Local Location: " + localPoint);
147: System.out.println("Remote Location: " + remotePoint);
148: }
149: }
150: }
151:
152: private static MyOodbDatabase connectLocal(String lUsername,
153: String lPassword) throws Exception {
154: final String[] fargs = new String[] { "-tcpPort=" + 0,
155: "-admin=" + lUsername + ":" + lPassword };
156:
157: Runnable startupDatabase = new Runnable() {
158: public void run() {
159: try {
160: org.myoodb.core.MyOodbMain.main(fargs);
161: } catch (Exception e) {
162: e.printStackTrace();
163: System.exit(0);
164: }
165: }
166: };
167:
168: Thread thread = new Thread(startupDatabase);
169: thread.setDaemon(true);
170: thread.start();
171:
172: while (org.myoodb.core.MyOodbManager
173: .getTheManagerInitializedFlag() == false) {
174: Thread.sleep(100);
175: }
176:
177: int port = org.myoodb.core.MyOodbManager.getTheManager()
178: .getPortNumber();
179: return MyOodbDatabase.open("tcp://localhost:" + port,
180: lUsername, lPassword);
181: }
182:
183: private static MyOodbDatabase connectRemote(String rHost,
184: int rPort, String rUsername, String rPassword) {
185: int timeout = 5000;
186: MyOodbDatabase remoteDatabase = null;
187: while (true) {
188: try {
189: remoteDatabase = MyOodbDatabase.open("udp://" + rHost
190: + ":" + rPort, rUsername, rPassword, null,
191: timeout);
192: break;
193: } catch (Exception e) {
194: System.out.println(e.getMessage());
195: }
196: }
197:
198: return remoteDatabase;
199: }
200: }
|