/*
 * Copyright 2009 Richard Zschech.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.zschech.gwt.comet.client.impl;

import net.zschech.gwt.comet.client.CometClient;
import net.zschech.gwt.comet.client.CometListener;
import net.zschech.gwt.comet.client.SerialMode;

import com.google.gwt.core.client.Duration;
import com.google.gwt.core.client.GWT;

/**
 * This is the base class for the comet implementations
 * 
 * @author Richard Zschech
 */
public abstract class CometTransport {
	
	public static final String MODULE_BASE_PARAMETER = "b";
	public static final String STRONG_NAME_PARAMETER = "p";
	
	protected CometClient client;
	protected CometListener listener;
	
	public void initiate(CometClient client, CometListener listener) {
		this.client = client;
		this.listener = listener;
	}
	
	public abstract void connect(int connectionCount);
	
	public abstract void disconnect();
	
	public String getUrl(int connectionCount) {
		String url = client.getUrl();
		if (client.getSerializer() != null && client.getSerializer().getMode() == SerialMode.DE_RPC) {
			url += (url.contains("?") ? "&" : "?") + MODULE_BASE_PARAMETER + '=' + GWT.getModuleBaseURL() + '&' + STRONG_NAME_PARAMETER + '=' + GWT.getPermutationStrongName();
		}
		return url + (url.contains("?") ? "&" : "?") + "t=" + Integer.toString((int) (Duration.currentTimeMillis() % Integer.MAX_VALUE), Character.MAX_RADIX).toUpperCase() + "&c=" + connectionCount;
	}
}

@@ -1510,15 +1510,40 @@
 ring
- url = 
+Builder url = new StringBuilder(
 clie
@@ -1553,16 +1553,17 @@
 getUrl()
+)
 ;%0D%0A%09%09if 
@@ -1665,137 +1665,202 @@
 %09url
- += (url.contains(%22?%22) ? %22&%22 : %22?%22) + MODULE_BASE_PARAMETER + '=' + GWT.getModuleBaseURL() + '&' + STRONG_NAME_PARAMETER + '=' + 
+.append(url.indexOf(%22?%22) %3E 0 ? '&' : '?');%0D%0A%09%09%09url.append(MODULE_BASE_PARAMETER).append('=').append(GWT.getModuleBaseURL());%0D%0A%09%09%09url.append('&').append(STRONG_NAME_PARAMETER).append('=').append(
 GWT.
@@ -1885,16 +1885,17 @@
 ngName()
+)
 ;%0D%0A%09%09%7D%0D%0A
@@ -1900,62 +1900,85 @@
 %0D%0A%09%09
-return url + (url.contains(%22?%22) ? %22&%22 : %22?%22) + %22t=%22 + 
+%0D%0A%09%09url.append(url.indexOf(%22?%22) %3E 0 ? '&' : '?');%0D%0A%09%09url.append(%22t=%22).append(
 Inte
@@ -2072,48 +2072,124 @@
 DIX)
-.toUpperCase() + %22&c=%22 + connectionCount
+);%0D%0A%09%09url.append(%22&c=%22).append(Integer.toString(connectionCount, Character.MAX_RADIX));%0D%0A%09%09%0D%0A%09%09return url.toString()
 ;%0D%0A%09

clean up