10 changed files with 338 additions and 82 deletions
-
12README.md
-
92src/main/java/com/corundumstudio/socketio/BroadcastOperations.java
-
94src/main/java/com/corundumstudio/socketio/ClientOperations.java
-
56src/main/java/com/corundumstudio/socketio/JoinIterator.java
-
71src/main/java/com/corundumstudio/socketio/SocketIOClient.java
-
13src/main/java/com/corundumstudio/socketio/SocketIOPipelineFactory.java
-
10src/main/java/com/corundumstudio/socketio/SocketIOServer.java
-
7src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java
-
17src/main/java/com/corundumstudio/socketio/transport/XHRPollingTransport.java
-
48src/test/java/com/corundumstudio/socketio/JoinIteratorsTest.java
@ -0,0 +1,92 @@ |
|||||
|
/** |
||||
|
* Copyright 2012 Nikita Koksharov |
||||
|
* |
||||
|
* 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 com.corundumstudio.socketio; |
||||
|
|
||||
|
import com.corundumstudio.socketio.parser.Packet; |
||||
|
|
||||
|
public class BroadcastOperations implements ClientOperations { |
||||
|
|
||||
|
private final Iterable<SocketIOClient> clients; |
||||
|
|
||||
|
public BroadcastOperations(Iterable<SocketIOClient> clients) { |
||||
|
super(); |
||||
|
this.clients = clients; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendMessage(String message) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendMessage(message); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendMessage(String message, AckCallback ackCallback) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendMessage(message, ackCallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendJsonObject(Object object) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendJsonObject(object); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendJsonObject(Object object, AckCallback ackCallback) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendJsonObject(object, ackCallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void send(Packet packet) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.send(packet); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void send(Packet packet, AckCallback ackCallback) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.send(packet, ackCallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void disconnect() { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.disconnect(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendEvent(String name, Object data) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendEvent(name, data); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void sendEvent(String name, Object data, AckCallback ackCallback) { |
||||
|
for (SocketIOClient client : clients) { |
||||
|
client.sendEvent(name, data, ackCallback); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,94 @@ |
|||||
|
/** |
||||
|
* Copyright 2012 Nikita Koksharov |
||||
|
* |
||||
|
* 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 com.corundumstudio.socketio; |
||||
|
|
||||
|
import com.corundumstudio.socketio.parser.Packet; |
||||
|
|
||||
|
/** |
||||
|
* Available client operations |
||||
|
* |
||||
|
*/ |
||||
|
public interface ClientOperations { |
||||
|
|
||||
|
/** |
||||
|
* Send message |
||||
|
* |
||||
|
* @param message - message to send |
||||
|
*/ |
||||
|
void sendMessage(String message); |
||||
|
|
||||
|
/** |
||||
|
* Send message with ack callback |
||||
|
* |
||||
|
* @param message - message to send |
||||
|
* @param ackCallback - ack callback |
||||
|
*/ |
||||
|
void sendMessage(String message, AckCallback ackCallback); |
||||
|
|
||||
|
/** |
||||
|
* Send object. Object will be encoded to json-format. |
||||
|
* |
||||
|
* @param object - object to send |
||||
|
*/ |
||||
|
void sendJsonObject(Object object); |
||||
|
|
||||
|
/** |
||||
|
* Send object with ack callback |
||||
|
* |
||||
|
* @param object - object to send |
||||
|
* @param ackCallback - ack callback |
||||
|
*/ |
||||
|
void sendJsonObject(Object object, AckCallback ackCallback); |
||||
|
|
||||
|
/** |
||||
|
* Send packet |
||||
|
* |
||||
|
* @param packet - packet to send |
||||
|
*/ |
||||
|
void send(Packet packet); |
||||
|
|
||||
|
/** |
||||
|
* Send packet with ack callback |
||||
|
* |
||||
|
* @param packet - packet to send |
||||
|
* @param ackCallback - ack callback |
||||
|
*/ |
||||
|
void send(Packet packet, AckCallback ackCallback); |
||||
|
|
||||
|
/** |
||||
|
* Disconnect client |
||||
|
* |
||||
|
*/ |
||||
|
void disconnect(); |
||||
|
|
||||
|
/** |
||||
|
* Send event |
||||
|
* |
||||
|
* @param name - event name |
||||
|
* @param data - event data |
||||
|
*/ |
||||
|
void sendEvent(String name, Object data); |
||||
|
|
||||
|
/** |
||||
|
* Send event with ack callback |
||||
|
* |
||||
|
* @param name - event name |
||||
|
* @param data - event data |
||||
|
* @param ackCallback - ack callback |
||||
|
*/ |
||||
|
void sendEvent(String name, Object data, AckCallback ackCallback); |
||||
|
|
||||
|
} |
@ -0,0 +1,56 @@ |
|||||
|
/** |
||||
|
* Copyright 2012 Nikita Koksharov |
||||
|
* |
||||
|
* 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 com.corundumstudio.socketio; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
import java.util.Iterator; |
||||
|
|
||||
|
public class JoinIterator<T> implements Iterator<T> { |
||||
|
|
||||
|
private final Iterator<Iterator<T>> listIterator; |
||||
|
private Iterator<T> currentIterator; |
||||
|
|
||||
|
public JoinIterator(Iterator<T> ... iterators) { |
||||
|
listIterator = Arrays.asList(iterators).iterator(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean hasNext() { |
||||
|
if (currentIterator == null || !currentIterator.hasNext()) { |
||||
|
while (listIterator.hasNext()) { |
||||
|
Iterator<T> iterator = listIterator.next(); |
||||
|
if (iterator.hasNext()) { |
||||
|
currentIterator = iterator; |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
return currentIterator.hasNext(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public T next() { |
||||
|
return currentIterator.next(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void remove() { |
||||
|
currentIterator.remove(); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,48 @@ |
|||||
|
/** |
||||
|
* Copyright 2012 Nikita Koksharov |
||||
|
* |
||||
|
* 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 com.corundumstudio.socketio; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.Iterator; |
||||
|
import java.util.List; |
||||
|
|
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Test; |
||||
|
|
||||
|
public class JoinIteratorsTest { |
||||
|
|
||||
|
@Test |
||||
|
public void testIterator() { |
||||
|
List<Integer> list1 = Arrays.asList(1, 2); |
||||
|
List<Integer> list2 = Arrays.asList(3, 4); |
||||
|
Iterator<Integer> i = list1.iterator(); |
||||
|
Iterator<Integer> i2 = list1.iterator(); |
||||
|
JoinIterator<Integer> iterators = new JoinIterator<Integer>(i, i2); |
||||
|
|
||||
|
List<Integer> mainList = new ArrayList<Integer>(); |
||||
|
for (; iterators.hasNext();) { |
||||
|
Integer integer = iterators.next(); |
||||
|
mainList.add(integer); |
||||
|
} |
||||
|
Assert.assertEquals(list1.size() + list2.size(), mainList.size()); |
||||
|
mainList.removeAll(list1); |
||||
|
mainList.removeAll(list2); |
||||
|
Assert.assertTrue(mainList.isEmpty()); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue