├── third_party ├── junit4.jar ├── hamcrest-core.jar ├── junit4-4.11.jar └── hamcrest-core-1.3.jar ├── teams ├── .travis.yml ├── clean.sh ├── test.sh ├── run_simple_gui_client.sh ├── src └── codeu │ └── chat │ ├── common │ ├── ListViewable.java │ ├── RandomUuidGenerator.java │ ├── LinearUuidGenerator.java │ ├── SinglesView.java │ ├── RawController.java │ ├── User.java │ ├── BasicView.java │ ├── NetworkCode.java │ ├── ConversationSummary.java │ ├── Message.java │ ├── Secret.java │ ├── BasicController.java │ ├── Conversation.java │ ├── LogicalView.java │ └── Relay.java │ ├── util │ ├── connections │ │ ├── ConnectionSource.java │ │ ├── Connection.java │ │ ├── ClientConnectionSource.java │ │ └── ServerConnectionSource.java │ ├── Serializer.java │ ├── Method.java │ ├── store │ │ ├── StoreAccessor.java │ │ ├── StoreLink.java │ │ ├── LinkIterable.java │ │ ├── LinkIterator.java │ │ └── Store.java │ ├── RemoteAddress.java │ ├── Time.java │ ├── Logger.java │ ├── Serializers.java │ ├── Uuid.java │ └── Timeline.java │ ├── client │ ├── ClientContext.java │ ├── Controller.java │ ├── simplegui │ │ ├── ChatSimpleGui.java │ │ ├── ConversationPanel.java │ │ ├── UserPanel.java │ │ └── MessagePanel.java │ ├── ClientUser.java │ ├── commandline │ │ └── ListNavigator.java │ └── ClientConversation.java │ ├── ClientMain.java │ ├── server │ ├── NoOpRelay.java │ ├── Model.java │ ├── Controller.java │ ├── RemoteRelay.java │ └── View.java │ ├── SimpleGuiClientMain.java │ ├── ServerMain.java │ ├── RelayMain.java │ └── relay │ ├── ServerFrontEnd.java │ └── Server.java ├── run_client.sh ├── make.sh ├── run_relay.sh ├── test └── codeu │ └── chat │ ├── util │ ├── TimeTest.java │ ├── UuidTest.java │ └── store │ │ └── StoreTest.java │ ├── TestRunner.java │ ├── server │ ├── BasicControllerTest.java │ └── RawControllerTest.java │ └── common │ └── SecretTest.java ├── CONTRIBUTING.md ├── run_server.sh └── README.md /third_party/junit4.jar: -------------------------------------------------------------------------------- 1 | junit4-4.11.jar -------------------------------------------------------------------------------- /third_party/hamcrest-core.jar: -------------------------------------------------------------------------------- 1 | hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /teams: -------------------------------------------------------------------------------- 1 | # sample team 2 | # this will match the values in the run_server.sh 3 | 100.101:ABABAB 4 | -------------------------------------------------------------------------------- /third_party/junit4-4.11.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/codeu_project_2017/HEAD/third_party/junit4-4.11.jar -------------------------------------------------------------------------------- /third_party/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/codeu_project_2017/HEAD/third_party/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | sudo: false 4 | 5 | jdk: 6 | - openjdk8 7 | 8 | os: 9 | - linux 10 | 11 | 12 | script: 13 | - ./clean.sh 14 | - ./make.sh 15 | - ./test.sh 16 | -------------------------------------------------------------------------------- /clean.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -e 18 | 19 | rm -rf ./bin/* 20 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -e 18 | 19 | java -cp ./third_party/junit4.jar:./bin codeu.chat.TestRunner 20 | -------------------------------------------------------------------------------- /run_simple_gui_client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | LOCAL_MACHINE="localhost@2007" 18 | 19 | cd './bin' 20 | 21 | java codeu.chat.SimpleGuiClientMain "${LOCAL_MACHINE}" 22 | -------------------------------------------------------------------------------- /src/codeu/chat/common/ListViewable.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | public interface ListViewable { 18 | 19 | // Produce a string for presenting this object within a list. 20 | String listView(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /run_client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | HOST="$1" 18 | PORT="$2" 19 | 20 | if [[ "${HOST}" == "" || "${PORT}" == "" ]] ; then 21 | echo 'usage: ' 22 | exit 1 23 | fi 24 | 25 | cd './bin' 26 | java codeu.chat.ClientMain "${HOST}@${PORT}" 27 | -------------------------------------------------------------------------------- /make.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | set -e 18 | 19 | mkdir -p bin 20 | 21 | javac -Xlint $(find * | grep "\\.java$") -d ./bin -sourcepath ./src -cp ./third_party/junit4.jar:./bin 22 | javac -Xlint $(find * | grep "\\.java$") -d ./bin -sourcepath ./test -cp ./third_party/junit4.jar:./bin 23 | -------------------------------------------------------------------------------- /src/codeu/chat/util/connections/ConnectionSource.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.connections; 16 | 17 | import java.io.Closeable; 18 | import java.io.IOException; 19 | 20 | public interface ConnectionSource extends Closeable { 21 | 22 | Connection connect() throws IOException; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/codeu/chat/util/Serializer.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | 21 | public interface Serializer { 22 | 23 | void write(OutputStream out, T value) throws IOException; 24 | 25 | T read(InputStream in) throws IOException; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /run_relay.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2017 Google Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | PORT="$1" 18 | TEAM_FILE="$2" 19 | 20 | if [[ "${PORT}" == "" || "${TEAM_FILE}" == "" ]] ; then 21 | echo 'usage: ' 22 | exit 1 23 | fi 24 | 25 | if [ ! -f "${TEAM_FILE}" ] ; then 26 | echo "No file at ${TEAM_FILE}" 27 | exit 1 28 | fi 29 | 30 | cd './bin' 31 | java codeu.chat.RelayMain "${PORT}" "${TEAM_FILE}" 32 | -------------------------------------------------------------------------------- /test/codeu/chat/util/TimeTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util; 16 | 17 | import static org.junit.Assert.*; 18 | import org.junit.Test; 19 | import org.junit.Before; 20 | 21 | public final class TimeTest { 22 | 23 | @Test 24 | public void testFromMs() { 25 | assertEquals(0, Time.fromMs(0).inMs()); 26 | assertEquals(10, Time.fromMs(10).inMs()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/codeu/chat/util/connections/Connection.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.connections; 16 | 17 | import java.io.Closeable; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.io.OutputStream; 21 | 22 | public interface Connection extends Closeable { 23 | 24 | InputStream in() throws IOException; 25 | OutputStream out() throws IOException; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/codeu/chat/util/Method.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util; 16 | 17 | public final class Method { 18 | 19 | // Common "not implemented" method 20 | public static void notImplemented() { 21 | System.out.format("NOT IMPLEMENTED: %s::%s\n", 22 | Thread.currentThread().getStackTrace()[2].getClassName(), 23 | Thread.currentThread().getStackTrace()[2].getMethodName()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/codeu/chat/util/store/StoreAccessor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.store; 16 | 17 | public interface StoreAccessor { 18 | 19 | VALUE first(KEY key); 20 | 21 | Iterable all(); 22 | 23 | Iterable at(KEY key); 24 | 25 | Iterable after(KEY start); 26 | 27 | Iterable before(KEY end); 28 | 29 | Iterable range(KEY start, KEY end); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/codeu/chat/util/store/StoreLink.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.store; 16 | 17 | final class StoreLink { 18 | 19 | public final KEY key; 20 | public final VALUE value; 21 | public StoreLink next; 22 | 23 | public StoreLink(KEY key, VALUE value, StoreLink next) { 24 | this.key = key; 25 | this.value = value; 26 | this.next = next; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/codeu/chat/util/RemoteAddress.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util; 16 | 17 | public final class RemoteAddress { 18 | 19 | public final String host; 20 | public final int port; 21 | 22 | public RemoteAddress(String host, int port) { 23 | this.host = host; 24 | this.port = port; 25 | } 26 | 27 | @Override 28 | public String toString() { return String.format("%s@%d", host, port); } 29 | 30 | // TODO : add tests for this 31 | public static RemoteAddress parse(String string) { 32 | final String[] tokens = string.split("@"); 33 | return new RemoteAddress(tokens[0], Integer.parseInt(tokens[1])); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | We'd love to accept your patches and contributions to this project. Just remember 4 | this project is designed as a playground for people to develop their skills. As 5 | such your proposed changes should keep in mind this project's intended purpose as 6 | platform for skill development. If you simply want to develop cool features based 7 | on this project, that's great - just fork this project and go wild. 8 | 9 | ## Contributor License Agreement 10 | 11 | Contributions to this project must be accompanied by a Contributor License 12 | Agreement. You (or your employer) retain the copyright to your contribution, 13 | this simply gives us permission to use and redistribute your contributions as 14 | part of the project. Head over to to see 15 | your current agreements on file or to sign a new one. 16 | 17 | You generally only need to submit a CLA once, so if you've already submitted one 18 | (even if it was for a different project), you probably don't need to do it 19 | again. 20 | 21 | ## Code reviews 22 | 23 | All submissions, including submissions by project members, require review. We 24 | use GitHub pull requests for this purpose. Consult [GitHub Help] for more 25 | information on using pull requests. 26 | 27 | [GitHub Help]: https://help.github.com/articles/about-pull-requests/ 28 | -------------------------------------------------------------------------------- /src/codeu/chat/common/RandomUuidGenerator.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import java.util.Random; 18 | 19 | import codeu.chat.util.Uuid; 20 | 21 | // Create a new random uuid. Uuids from this generator are random 22 | // but are not guaranteed to be unique. Checking uniqueness is left 23 | // to the caller. 24 | public final class RandomUuidGenerator implements Uuid.Generator { 25 | 26 | private final Uuid commonRoot; 27 | private final Random random; 28 | 29 | public RandomUuidGenerator(Uuid root, long seed) { 30 | this.commonRoot = root; 31 | this.random = new Random(seed); 32 | } 33 | 34 | @Override 35 | public Uuid make() { 36 | return new Uuid(commonRoot, random.nextInt()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/codeu/chat/util/store/LinkIterable.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.store; 16 | 17 | import java.util.Comparator; 18 | import java.util.Iterator; 19 | 20 | final class LinkIterable implements Iterable { 21 | 22 | private final Comparator comparator; 23 | private final StoreLink first; 24 | private final StoreLink last; 25 | 26 | public LinkIterable(Comparator comparator, StoreLink first, StoreLink last) { 27 | this.comparator = comparator; 28 | this.first = first; 29 | this.last = last; 30 | } 31 | 32 | @Override 33 | public Iterator iterator() { 34 | return new LinkIterator(comparator, first, last); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/codeu/chat/client/ClientContext.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.client; 16 | 17 | import codeu.chat.client.ClientConversation; 18 | import codeu.chat.client.ClientMessage; 19 | import codeu.chat.client.ClientUser; 20 | import codeu.chat.client.Controller; 21 | import codeu.chat.client.View; 22 | 23 | public final class ClientContext { 24 | 25 | public final ClientUser user; 26 | public final ClientConversation conversation; 27 | public final ClientMessage message; 28 | 29 | public ClientContext(Controller controller, View view) { 30 | user = new ClientUser(controller, view); 31 | conversation = new ClientConversation(controller, view, user); 32 | message = new ClientMessage(controller, view, user, conversation); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/codeu/chat/common/LinearUuidGenerator.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import codeu.chat.util.Uuid; 18 | 19 | public final class LinearUuidGenerator implements Uuid.Generator { 20 | 21 | private final Uuid commonRoot; 22 | private final int start; 23 | private final int end; 24 | 25 | private int current; 26 | 27 | public LinearUuidGenerator(Uuid root, int start, int end) { 28 | this.commonRoot = root; 29 | this.start = start; 30 | this.end = end; 31 | this.current = start; 32 | } 33 | 34 | @Override 35 | public Uuid make() { 36 | return new Uuid(commonRoot, next()); 37 | } 38 | 39 | private int next() { 40 | if (current == end) { 41 | throw new IllegalStateException("Uuid overflow"); 42 | } else { 43 | current++; 44 | return current; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/codeu/chat/TestRunner.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat; 16 | 17 | import org.junit.runner.JUnitCore; 18 | import org.junit.runner.Result; 19 | import org.junit.runner.notification.Failure; 20 | 21 | public final class TestRunner { 22 | public static void main(String[] args) { 23 | final Result result = 24 | JUnitCore.runClasses( 25 | codeu.chat.common.SecretTest.class, 26 | codeu.chat.relay.ServerTest.class, 27 | codeu.chat.server.BasicControllerTest.class, 28 | codeu.chat.server.RawControllerTest.class, 29 | codeu.chat.util.TimeTest.class, 30 | codeu.chat.util.UuidTest.class, 31 | codeu.chat.util.store.StoreTest.class 32 | ); 33 | for (final Failure failure : result.getFailures()) { 34 | System.out.println(failure.toString()); 35 | } 36 | System.out.println(result.wasSuccessful()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/codeu/chat/util/store/LinkIterator.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.store; 16 | 17 | import java.util.Comparator; 18 | import java.util.Iterator; 19 | 20 | final class LinkIterator implements Iterator { 21 | 22 | private final Comparator comparator; 23 | private final StoreLink last; 24 | 25 | private StoreLink current; 26 | 27 | public LinkIterator(Comparator comparator, StoreLink first, StoreLink last) { 28 | this.comparator = comparator; 29 | this.last = last; 30 | this.current = first; 31 | } 32 | 33 | @Override 34 | public boolean hasNext() { 35 | return current != null && comparator.compare(current.key, last.key) <= 0; 36 | } 37 | 38 | @Override 39 | public VALUE next() { 40 | final VALUE value = current.value; 41 | current = current.next; 42 | return value; 43 | } 44 | 45 | @Override 46 | public void remove() { 47 | // do nothing 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/codeu/chat/common/SinglesView.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import codeu.chat.common.Conversation; 18 | import codeu.chat.common.Message; 19 | import codeu.chat.common.User; 20 | import codeu.chat.util.Uuid; 21 | 22 | // SINGLES VIEW 23 | // 24 | // A view as part of the Model-View-Controller pattern. This view is 25 | // responsible for allowing single value reading from the model. 26 | public interface SinglesView { 27 | 28 | // FIND USER 29 | // 30 | // Find the user whose id matches the given id. If no user's id matches 31 | // the given id, null with be returned. 32 | User findUser(Uuid id); 33 | 34 | // FIND CONVERSATION 35 | // 36 | // Find the conversation whose id matches the given id. If no conversation's 37 | // matches the given id, null will be returned. 38 | Conversation findConversation(Uuid id); 39 | 40 | // FIND MESSAGE 41 | // 42 | // Find the message whose id matches the given id. if no message's id 43 | // matches the given id, null will be returned. 44 | Message findMessage(Uuid id); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/codeu/chat/common/RawController.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import codeu.chat.util.Time; 18 | import codeu.chat.util.Uuid; 19 | 20 | // RAW CONTROLLER 21 | // 22 | // A controller that grants a large amount of control over how data is inserted 23 | // into the model. If there is a conflict in data, the call will be rejected and 24 | // a null value returned. 25 | public interface RawController { 26 | 27 | // NEW MESSAGE 28 | // 29 | // Add a new message to the model with a specific id. If the id is already 30 | // in use, the call will fail and null will be returned. 31 | Message newMessage(Uuid id, Uuid author, Uuid conversation, String body, Time creationTime); 32 | 33 | // NEW USER 34 | // 35 | // Add a new user to the model with a specific id. If the id is already in 36 | // use, the call will fail and null will be returned. 37 | User newUser(Uuid id, String name, Time creationTime); 38 | 39 | // NEW CONVERSATION 40 | // 41 | // Add a new conversation to the model with a specific if. If the id is 42 | // already in use, the call will fail and null will be returned. 43 | Conversation newConversation(Uuid id, String title, Uuid owner, Time creationTime); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/codeu/chat/common/User.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | 21 | import codeu.chat.util.Serializer; 22 | import codeu.chat.util.Serializers; 23 | import codeu.chat.util.Time; 24 | import codeu.chat.util.Uuid; 25 | 26 | public final class User { 27 | 28 | public static final Serializer SERIALIZER = new Serializer() { 29 | 30 | @Override 31 | public void write(OutputStream out, User value) throws IOException { 32 | 33 | Uuid.SERIALIZER.write(out, value.id); 34 | Serializers.STRING.write(out, value.name); 35 | Time.SERIALIZER.write(out, value.creation); 36 | 37 | } 38 | 39 | @Override 40 | public User read(InputStream in) throws IOException { 41 | 42 | return new User( 43 | Uuid.SERIALIZER.read(in), 44 | Serializers.STRING.read(in), 45 | Time.SERIALIZER.read(in) 46 | ); 47 | 48 | } 49 | }; 50 | 51 | public final Uuid id; 52 | public final String name; 53 | public final Time creation; 54 | 55 | public User(Uuid id, String name, Time creation) { 56 | 57 | this.id = id; 58 | this.name = name; 59 | this.creation = creation; 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/codeu/chat/common/BasicView.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | import java.util.Collection; 18 | 19 | import codeu.chat.common.Conversation; 20 | import codeu.chat.common.ConversationSummary; 21 | import codeu.chat.common.Message; 22 | import codeu.chat.common.User; 23 | import codeu.chat.util.Uuid; 24 | 25 | // BASIC VIEW 26 | // 27 | // The view component in the Model-View-Controller pattern. This component 28 | // is used to read information from the model where the model is the current 29 | // state of the server. Data returned from the view should be treated as 30 | // read only data as manipulating any data returned from the view may 31 | // have no effect on the server's state. 32 | 33 | public interface BasicView { 34 | 35 | // GET USERS 36 | // 37 | // Return all users whose id is found in the given collection. 38 | Collection getUsers(Collection ids); 39 | 40 | // GET ALL CONVERSATIONS 41 | // 42 | // Return a summary of each converation. 43 | Collection getAllConversations(); 44 | 45 | // GET CONVERSATIONS 46 | // 47 | // Return all conversations whose id is found in the given collection. 48 | Collection getConversations(Collection ids); 49 | 50 | // GET MESSAGES 51 | // 52 | // Return all messages whose id is found in the given collection. 53 | Collection getMessages(Collection ids); 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/codeu/chat/common/NetworkCode.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.common; 16 | 17 | public final class NetworkCode { 18 | 19 | public static final int 20 | NO_MESSAGE = 0, 21 | GET_USERS_BY_ID_REQUEST = 1, 22 | GET_USERS_BY_ID_RESPONSE = 2, 23 | GET_ALL_CONVERSATIONS_REQUEST = 3, 24 | GET_ALL_CONVERSATIONS_RESPONSE = 4, 25 | GET_CONVERSATIONS_BY_ID_RESPONSE = 5, 26 | GET_CONVERSATIONS_BY_ID_REQUEST = 6, 27 | GET_MESSAGES_BY_ID_REQUEST = 7, 28 | GET_MESSAGES_BY_ID_RESPONSE = 8, 29 | NEW_MESSAGE_REQUEST = 9, 30 | NEW_MESSAGE_RESPONSE = 10, 31 | NEW_USER_REQUEST = 11, 32 | NEW_USER_RESPONSE = 12, 33 | NEW_CONVERSATION_REQUEST = 13, 34 | NEW_CONVERSATION_RESPONSE = 14, 35 | GET_CONVERSATIONS_BY_TIME_REQUEST = 15, 36 | GET_CONVERSATIONS_BY_TIME_RESPONSE = 16, 37 | GET_CONVERSATIONS_BY_TITLE_REQUEST = 17, 38 | GET_CONVERSATIONS_BY_TITLE_RESPONSE = 18, 39 | GET_MESSAGES_BY_TIME_REQUEST = 19, 40 | GET_MESSAGES_BY_TIME_RESPONSE = 20, 41 | GET_MESSAGES_BY_RANGE_REQUEST = 21, 42 | GET_MESSAGES_BY_RANGE_RESPONSE = 22, 43 | GET_USER_GENERATION_REQUEST = 23, 44 | GET_USER_GENERATION_RESPONSE = 24, 45 | GET_USERS_EXCLUDING_REQUEST = 25, 46 | GET_USERS_EXCLUDING_RESPONSE = 26, 47 | RELAY_READ_REQUEST = 27, 48 | RELAY_READ_RESPONSE = 28, 49 | RELAY_WRITE_REQUEST = 29, 50 | RELAY_WRITE_RESPONSE = 30; 51 | } 52 | -------------------------------------------------------------------------------- /src/codeu/chat/util/connections/ClientConnectionSource.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util.connections; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | import java.net.Socket; 21 | 22 | // CLIENT CONNECTION SOURCE 23 | // 24 | // Implements the ConnectionSource interface for clients who know what 25 | // port on the host they will be connecting to.Calls to "connect" will 26 | // block until a connection is established or timeout. 27 | public final class ClientConnectionSource implements ConnectionSource { 28 | 29 | private final String host; 30 | private final int port; 31 | 32 | public ClientConnectionSource(String host, int port) { 33 | this.host = host; 34 | this.port = port; 35 | } 36 | 37 | @Override 38 | public Connection connect() throws IOException { 39 | return fromSocket(new Socket(host, port)); 40 | } 41 | 42 | @Override 43 | public void close() throws IOException { } 44 | 45 | private static Connection fromSocket(final Socket socket) throws IOException { 46 | 47 | return new Connection() { 48 | 49 | @Override 50 | public InputStream in() throws IOException { 51 | return socket.getInputStream(); 52 | } 53 | 54 | @Override 55 | public OutputStream out() throws IOException { 56 | return socket.getOutputStream(); 57 | } 58 | 59 | @Override 60 | public void close() throws IOException { 61 | socket.close(); 62 | } 63 | }; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/codeu/chat/util/Time.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package codeu.chat.util; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.io.OutputStream; 20 | import java.text.SimpleDateFormat; 21 | import java.util.Date; 22 | 23 | public final class Time implements Comparable