changelog goes in SVN commit! Generalizations = bad!
Happy New Year! Yes, the current one, not a previous one; this is a new post, we swear!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts MenuQuote from: http://thedailywtf.com/Articles/A-Clean-Install.aspx
Originally posted by "Dirk"...
A friend of mine recently bought a product called PrintMaster. He had some trouble installing the software and called me up for some tech support. He was receiving an Error 1305, for which a quick Google search lead me towards the following:
... Completing a clean installation of the program will help to prevent conflicts that can occur. The remainder of this note describes the procedure...
All right, I thought a clean install, let's read on and see how to do a clean install:
1. Place a small amount of nonabrasive, liquid soap on the shiny side of the CD or DVD.
2. Using your fingertips and warm water, gently rub the soap on the disc in a circular motion.
3. Rinse the disc thoroughly and dry it using a clean, soft T-shirt or lint-free towel. Do not use paper towels or tissue paper.
4. Install the program.
Doing a "clean install" has now taken on a new meaning for me.
/*
* Find the fastest mirror! :)
*/
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class MirrorSelector {
private static boolean DEBUG = false;
private static int MAX_TIME = 1000; // timeout time in ms and default length if an error occurs
public static InetSocketAddress selectMirror(InetSocketAddress[] mirrors)
{
Socket s = new Socket();
long[] times = new long[mirrors.length];
long start;
for(int i=0;i<mirrors.length;i++)
{
try{
s = new Socket();
start = System.currentTimeMillis();
s.connect(mirrors[i], MAX_TIME);
times[i] = System.currentTimeMillis() - start;
s.close();
if(DEBUG)
System.out.println("Connecting to " + mirrors[i] + " took " + times[i] + "ms");
}
/* I considered condensing all the specific exceptions into a generic handler
* since they all respond basically the same way
*/
catch(SocketTimeoutException ste)
{
System.out.println("Address " + mirrors[i] + " timed out");
times[i] = MAX_TIME;
}
catch(ConnectException ce)
{
System.out.println("Connect Exception: " + ce.getMessage() + " for " + mirrors[i]);
if(DEBUG)
ce.printStackTrace();
times[i] = MAX_TIME;
}
catch(SocketException se)
{
System.out.println("Unable to connect to " + mirrors[i]
+", there may be a problem with your network connection.");
if(DEBUG)
se.printStackTrace();
times[i] = MAX_TIME;
}
catch(IOException ioe)
{
System.out.println("Error connecting to " + mirrors[i]);
if(DEBUG)
ioe.printStackTrace();
times[i] = MAX_TIME;
}
}
int fast_id = 0;
long fast_time = MAX_TIME;
for(int i=0;i<times.length;i++)
{
if(times[i] < fast_time)
{
fast_id = i;
fast_time = times[i];
}
}
return mirrors[fast_id];
}
public static InetSocketAddress getClosestMirror(String hostname, int port)
{
InetAddress[] ips;
try{
ips = InetAddress.getAllByName(hostname);
}
catch(UnknownHostException uhe)
{
System.out.println("Unable to resolve host: " + port);
return null;
}
InetSocketAddress[] mirrors = new InetSocketAddress[ips.length];
for(int i=0;i<ips.length;i++)
{
mirrors[i] = new InetSocketAddress(ips[i], port);
}
return selectMirror(mirrors);
}
public static void main(String[] args)
{
System.out.println("Fastest: " + getClosestMirror("google.com", 80));
}
}
:loop
;// execute a shell command
IF ERRORLEVEL == 1 GOTO loop
pause > nul
package connect;
import java.util.Timer;
import java.util.TimerTask;
import java.util.PriorityQueue;
import util.BNetPacket;
import util.QueuePacket;
/***
* Queue manages timing of outgoing data for a given PacketThread connection.
* Numbers and algorithm for computing proper delays inspired by iago's JavaOp2 code (www.javaop.com)
*
*@author unTactical
*/
public class Queue {
private final PriorityQueue<QueuePacket> queue = new PriorityQueue<QueuePacket>();
private final Timer timer = new Timer();
private TimerTask current;
private PacketThread conn;
private boolean queueReady = true;
private boolean enabled = false;
private long lastSent = System.currentTimeMillis();
//TODO: make these customizable
int packetCost = 250;
int byteCost = 17;
int byteOverThresholdCost = 17;
int thresholdBytes = 65;
int maxCredits = 800;
int creditRate = 5;
int credits = 800;
public Queue(PacketThread connection)
{
conn = connection;
}
public Queue(String DONOTUSESTHIS_FORTESTINGONLY) // for testing with QueueManager main function
{
}
/** Supports disabling of the queue temporarily (useful for faster logon)
*
* @param status - enable/disable
*/
public void enable(boolean status)
{
enabled = status;
}
/** Clear the queue if it gets too full */
public synchronized void clear()
{
queue.clear();
}
/** Primary function of Queue, schedules a packet to be sent, ordered by Priority and time scheduled
*
* @param data - packet to send to battle.net
* @param priority - higher means it will be sent sooner
*/
public synchronized void send(BNetPacket data, int priority)
{
QueuePacket q = new QueuePacket(data,priority);
queue.add(q);
if(queueReady)
{
long delay = getDelay(data.getBytes());
timer.schedule(new QueueTask(),delay);
queueReady = false;
}
}
/** Tells us if this Queue is ready to send a message immediately or if it will have to be scheduled
*
* @return true if message can be sent immediately, false if we must schedule it
*/
public synchronized boolean isReady()
{
return queueReady;
}
/** Lets us predict how long it will be before a new message can be sent based on this queue's status.
* This is the core function that allows us to use a rolling queue system.
*
* @param priority - only consider packets in the queue matching or exceeding a given priority
* @return amount to wait
*/
public synchronized long getWait(int priority)
{
QueuePacket qpacket;
updateCredits();
int wait = credits;
if(queue.size() > 0)
{
Object[] elements = queue.toArray();
int tempCredits = credits;
for(int i=0;i<elements.length;i++)
{
qpacket = (QueuePacket) elements[i];
if(qpacket.getPriority() >= priority)
getDelay(qpacket.getData().getBytes());
}
wait = credits;
credits = tempCredits;
}
return wait;
}
/** Does the math for figuring out how long to wait after a message is sent
* If you call this method, it adjusts the queue's status so if you don't really want to send the message,
* make sure you return credits to its previous value.
* I borrowed and tweaked this from iago's code.
*
* @param bytes - the longer the message, the more we need to wait. This could be replaced with a length variable
* @return time to wait after sending a message of length specified
*/
private long getDelay(byte[] bytes)
{
if(!enabled)
return 0;
// Add the credits for the elapsed time
updateCredits();
lastSent = System.currentTimeMillis();
// Get the packet's "cost"
int thisByteDelay = byteCost;
if(bytes.length > thresholdBytes)
byteCost = byteOverThresholdCost;
int thisPacketCost = packetCost + (thisByteDelay * bytes.length);
// Check how long this packet will have to wait
int requiredDelay = 0;
// If we can't "afford" the packet, figure out how much time we'll have to wait
if(credits < 0)
requiredDelay = -credits * creditRate;
// Deduct this packet from the credits
credits -= thisPacketCost;
return requiredDelay;
}
/** Since credits doesn't change on its own, we need a way to ensure its status is correct
* when determining wait time.
*/
private void updateCredits()
{
if(credits < maxCredits)
{
credits += (System.currentTimeMillis() - lastSent) / creditRate;
if(credits > maxCredits)
credits = maxCredits;
}
}
/** Waits after a message is sent and sends the next message in line, then schedules the next wait
* If there is no next message to send, it sets the queue to a 'ready' status.
*
* @author unTactical
*/
private class QueueTask extends TimerTask
{
QueuePacket data;
public void run()
{
if(!queue.isEmpty())
{
// send the message and schedule the next one
data = queue.remove();
byte[] tosend = data.getData().getBytes();
conn.send(tosend);
long delay = getDelay(tosend);
timer.schedule(current = new QueueTask(),delay);
queueReady = false;
}
else
queueReady = true;
}
}
}
package _main;
import java.util.HashMap;
import events.OutChatCommand;
import connect.Queue;
import plugins.Logger;
import util.BNetPacket;
/** Central point for all outgoing data to battle.net
* - IndividalQueueArray: add to a specific bot
* - AllQueueArray: add to all bots
* - OpOnlyArray: add to the most available bot with ops
* - NonOpArray: add to the most available both without ops
*
* @author unTactical
*/
public class QueueManager {
private static HashMap <String, Queue> all = new HashMap<String, Queue>();
private static HashMap <String, Queue> ops = new HashMap<String, Queue>();
private static HashMap <String, Queue> nonops = new HashMap<String, Queue>();
private QueueManager() {}
/** Lets us disable flood protection for a given Queue so that messages are sent immediately
*
* @param id - the name of the queue to enable/disable
* @param status - true to enable flood protection, false to disable
*/
public static void enable(String id, boolean status)
{
all.get(id).enable(status);
}
/** Add a Queue to the Manager
*
* @param id - name of the Queue
* @param q - Queue to manage
* @param hasOps - whether this bot handles 'operator' or non 'operator' messages by default
*/
public synchronized static void addQueue(String id, Queue q, boolean hasOps)
{
all.put(id,q);
if(hasOps)
ops.put(id,q);
else
nonops.put(id, q);
}
/** Remove a queue no longer in use
* This usually happens when we reset a connection.
*
* @param id - name of the Queue to remove
*/
public synchronized static void removeQueue(String id)
{
all.remove(id);
ops.remove(id);
nonops.remove(id);
}
/** Allows us to change whether a given Queue handles ops or non ops messages
*
* @param id - name of the Queue
* @param hasOps - true to handle 'operator' messages, false to handle non 'operator' messages by default
*/
public synchronized static void updateOps(String id, boolean hasOps)
{
if(hasOps)
{
nonops.remove(id);
if(!ops.containsKey(id))
ops.put(id, all.get(id));
}
else
{
ops.remove(id);
if(!nonops.containsKey(id))
nonops.put(id, all.get(id));
}
}
/** Allows us to change whether a given Queue handles ops or non ops messages
*
* @param id - int ID of the Queue to be looked up
* @param hasOps - true to handle 'operator' messages, false to handle non 'operator' messages by default
*/
public synchronized static void updateOps(int id, boolean hasOps)
{
updateOps(ConnectionManager.getInstance().getConnection(id).getName(), hasOps);
}
/** Remove all Queues
* Usually done when there is a connection error and all Queues must be reset
*/
public static void clearAll()
{
Object[] queues = all.values().toArray();
for(int i=0;i<queues.length;i++)
{
((Queue) queues[i]).clear();
}
}
/** Sends a message on all Queues in this Manager
*
* @param data - packet to send to battle.net
* @param priority - order in line, greatest first
*/
public static void sendAll(BNetPacket data, int priority)
{
Object[] queues = all.values().toArray();
for(int i=0;i<queues.length;i++)
{
((Queue) queues[i]).send(data,priority);
}
}
/** Send a message on a specific Queue
* Primarily done when a packet must be returned on the same Queue it was received (ie logon)
*
* @param data - packet to send to battle.net
* @param priority - order in line, greatest first
* @param id - name of the Queue we want to send the packet on
*/
public static void sendByName(BNetPacket data, int priority, String id)
{
Queue sender = all.get(id);
if(sender != null)
sender.send(data, priority);
else
Logger.logConsoleText("Sender id " + id + " is null!");
}
/** Send a message on a specific Queue
* Primarily done when a packet must be returned on the same Queue it was received (ie logon)
*
* @param data - packet to send to battle.net
* @param priority - order in line, greatest first
* @param id - int ID of the Queue to lookup
*/
public static void sendByID(BNetPacket data, int priority, int ID)
{
sendByName(data, priority, ConnectionManager.getInstance().getConnection(ID).getName());
}
/** Send a non 'operator' message.
* The message will be sent on the first queue that is ready. If no queue is ready, it is added to the Queue
* that will be ready to send it soonest
*
* @param data - packet to send to battle.net
* @param priority - order in line, greatest first
*/
public static void sendOps(BNetPacket data, int priority)
{
Object[] queues = ops.values().toArray();
sendSpecified(queues,data,priority);
}
/** Send an 'operator' message.
* The message will be sent on the first queue that is ready. If no queue is ready, it is added to the Queue
* that will be ready to send it soonest
*
* @param data - packet to send to battle.net
* @param priority - order in line, greatest first
*/
public static void sendNonOps(BNetPacket data, int priority)
{
Object[] queues;
if(!nonops.isEmpty())
queues = nonops.values().toArray();
else
queues = ops.values().toArray(); // If there is no non-ops Queue available, it will be sent on an op Queue instead.
sendSpecified(queues,data,priority);
}
/** Checks all of the Queues availability and chooses (ideally) the first Queue that is ready to send the message.
* If no Queue is ready, it determines which Queue will be ready to send the message soonest and adds it to that one.
* (Ideally)
*
* @param queues - spefies whether we are checking the Ops Queue or the NonOps Queue
* @param data - packet to send to battle.net
* @param priority - a message of a higher priority is sent before messages of lower priority, so only check those relevant
*/
private static synchronized void sendSpecified(Object[] queues, BNetPacket data, int priority)
{
long highCredits = -999999;
int lowID = -1;
Queue current;
for(int i=0;i<queues.length;i++)
{
current = ((Queue) queues[i]);
long thisWait = current.getWait(priority);
if(thisWait == 800)
{
current.send(data, priority);
try {Thread.sleep(500); } catch(Exception e) {};
return;
}
else if(thisWait > highCredits)
{
highCredits = thisWait;
lowID = i;
}
}
if(lowID == -1) // this will happen if the queue is empty (ie a op message is requested but no op queue exists)
Logger.logConsoleText("Message not sent! lowID invalid!!");
else
((Queue) queues[lowID]).send(data, priority);
try {Thread.sleep(500); } catch(Exception e) {}; // wait a short time to account for non-0 travel time across internet
}
public static void main(String[] args)
{
addQueue("zero",new Queue(""),false);
addQueue("one",new Queue(""),false);
System.out.println("test1");
sendNonOps(new OutChatCommand(-1,"test1").getBNetPacket(),0);
System.out.println("test2");
sendNonOps(new OutChatCommand(-1,"test2").getBNetPacket(),0);
System.out.println("test3");
sendNonOps(new OutChatCommand(-1,"test3").getBNetPacket(),0);
System.out.println("test4");
sendNonOps(new OutChatCommand(-1,"test4").getBNetPacket(),0);
System.out.println("test5");
sendNonOps(new OutChatCommand(-1,"test5").getBNetPacket(),0);
System.out.println("test6");
sendNonOps(new OutChatCommand(-1,"test6").getBNetPacket(),0);
System.out.println("test7");
sendNonOps(new OutChatCommand(-1,"test7").getBNetPacket(),0);
System.out.println("test8");
sendNonOps(new OutChatCommand(-1,"test8").getBNetPacket(),0);
System.out.println("test9");
sendNonOps(new OutChatCommand(-1,"test9").getBNetPacket(),0);
System.out.println("test10");
sendNonOps(new OutChatCommand(-1,"test10").getBNetPacket(),0);
}
}
Page created in 0.099 seconds with 16 queries.