diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-08-27 17:16:18 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-08-27 17:16:18 +0000 |
commit | a942ffadee0fe9fd385decdf818ad6baae8c99b3 (patch) | |
tree | d500fbdba7379631b0591a19417c7c3f3df29194 /src | |
parent | 6be9a1ed1b7847c795cb700e3e0bd87824fc0573 (diff) | |
download | gnunet-java-a942ffadee0fe9fd385decdf818ad6baae8c99b3.tar.gz gnunet-java-a942ffadee0fe9fd385decdf818ad6baae8c99b3.zip |
- adapted source tree structure to gradle/maven conventions
- added gradle wrapper
- fixes to adapt to GNUnet changes (new time unit, ...)
- helper process in util
- started implementing testbed
- skeleton for voting tools
- use new mq api
- implemented some more transport api
- mesh
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/gnunet/consensus/ConcludeCallback.java (renamed from src/org/gnunet/consensus/ConcludeCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/ConcludeDoneMessage.java (renamed from src/org/gnunet/consensus/ConcludeDoneMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/ConcludeMessage.java (renamed from src/org/gnunet/consensus/ConcludeMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/Consensus.java (renamed from src/org/gnunet/consensus/Consensus.java) | 14 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/ConsensusElement.java (renamed from src/org/gnunet/consensus/ConsensusElement.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/InsertDoneCallback.java (renamed from src/org/gnunet/consensus/InsertDoneCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/InsertElementMessage.java (renamed from src/org/gnunet/consensus/InsertElementMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/NewElementCallback.java (renamed from src/org/gnunet/consensus/NewElementCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/consensus/NewElementMessage.java (renamed from src/org/gnunet/consensus/NewElementMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Construct.java (renamed from src/org/gnunet/construct/Construct.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/DoubleValue.java (renamed from src/org/gnunet/construct/DoubleValue.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/FillWith.java (renamed from src/org/gnunet/construct/FillWith.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/FixedSizeArray.java (renamed from src/org/gnunet/construct/FixedSizeArray.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/FixedSizeIntegerArray.java (renamed from src/org/gnunet/construct/FixedSizeIntegerArray.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/FrameSize.java (renamed from src/org/gnunet/construct/FrameSize.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Int16.java (renamed from src/org/gnunet/construct/Int16.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Int32.java (renamed from src/org/gnunet/construct/Int32.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Int64.java (renamed from src/org/gnunet/construct/Int64.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Int8.java (renamed from src/org/gnunet/construct/Int8.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/IntegerFill.java (renamed from src/org/gnunet/construct/IntegerFill.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Message.java (renamed from src/org/gnunet/construct/Message.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/MessageIdAnnotationProcessor.java (renamed from src/org/gnunet/construct/MessageIdAnnotationProcessor.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/MessageLoader.java (renamed from src/org/gnunet/construct/MessageLoader.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/MessageUnion.java (renamed from src/org/gnunet/construct/MessageUnion.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/MsgMap.txt (renamed from src/org/gnunet/construct/MsgMap.txt) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/NestedMessage.java (renamed from src/org/gnunet/construct/NestedMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/ProtocolViolationException.java (renamed from src/org/gnunet/construct/ProtocolViolationException.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/ReflectUtil.java (renamed from src/org/gnunet/construct/ReflectUtil.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/UInt16.java (renamed from src/org/gnunet/construct/UInt16.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/UInt32.java (renamed from src/org/gnunet/construct/UInt32.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/UInt64.java (renamed from src/org/gnunet/construct/UInt64.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/UInt8.java (renamed from src/org/gnunet/construct/UInt8.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/Union.java (renamed from src/org/gnunet/construct/Union.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/UnionCase.java (renamed from src/org/gnunet/construct/UnionCase.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/VariableSizeArray.java (renamed from src/org/gnunet/construct/VariableSizeArray.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/VariableSizeIntegerArray.java (renamed from src/org/gnunet/construct/VariableSizeIntegerArray.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/ZeroTerminatedString.java (renamed from src/org/gnunet/construct/ZeroTerminatedString.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/package-info.java (renamed from src/org/gnunet/construct/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/DoubleParser.java (renamed from src/org/gnunet/construct/parsers/DoubleParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/FillParser.java (renamed from src/org/gnunet/construct/parsers/FillParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/FixedSizeArrayParser.java (renamed from src/org/gnunet/construct/parsers/FixedSizeArrayParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java (renamed from src/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/IntegerFillParser.java (renamed from src/org/gnunet/construct/parsers/IntegerFillParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/IntegerParser.java (renamed from src/org/gnunet/construct/parsers/IntegerParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/IntegerUtil.java (renamed from src/org/gnunet/construct/parsers/IntegerUtil.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/NestedParser.java (renamed from src/org/gnunet/construct/parsers/NestedParser.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/Parser.java (renamed from src/org/gnunet/construct/parsers/Parser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/SequenceParser.java (renamed from src/org/gnunet/construct/parsers/SequenceParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/StringParser.java (renamed from src/org/gnunet/construct/parsers/StringParser.java) | 1 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/UnionParser.java (renamed from src/org/gnunet/construct/parsers/UnionParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/VariableSizeArrayParser.java (renamed from src/org/gnunet/construct/parsers/VariableSizeArrayParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java (renamed from src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/construct/parsers/package-info.java (renamed from src/org/gnunet/construct/parsers/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/ConnectHandler.java (renamed from src/org/gnunet/core/ConnectHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/ConnectNotifyMessage.java (renamed from src/org/gnunet/core/ConnectNotifyMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/Core.java (renamed from src/org/gnunet/core/Core.java) | 246 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/DisconnectHandler.java (renamed from src/org/gnunet/core/DisconnectHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/DisconnectNotifyMessage.java (renamed from src/org/gnunet/core/DisconnectNotifyMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/HeaderNotify.java (renamed from src/org/gnunet/core/HeaderNotify.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/InitCallback.java (renamed from src/org/gnunet/core/InitCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/InitMessage.java (renamed from src/org/gnunet/core/InitMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/InitReplyMessage.java (renamed from src/org/gnunet/core/InitReplyMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/MessageNotify.java (renamed from src/org/gnunet/core/MessageNotify.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/NotifyInboundTrafficMessage.java (renamed from src/org/gnunet/core/NotifyInboundTrafficMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/NotifyOutboundTrafficMessage.java (renamed from src/org/gnunet/core/NotifyOutboundTrafficMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/RequestIdentification.java | 35 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/SendMessage.java (renamed from src/org/gnunet/core/SendMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/SendMessageReady.java (renamed from src/org/gnunet/core/SendMessageReady.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/SendMessageRequest.java (renamed from src/org/gnunet/core/SendMessageRequest.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/core/package-info.java (renamed from src/org/gnunet/core/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/BlockType.java (renamed from src/org/gnunet/dht/BlockType.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ClientGetMessage.java (renamed from src/org/gnunet/dht/ClientGetMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ClientGetStopMessage.java (renamed from src/org/gnunet/dht/ClientGetStopMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ClientPutConfirmationMessage.java (renamed from src/org/gnunet/dht/ClientPutConfirmationMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ClientPutMessage.java (renamed from src/org/gnunet/dht/ClientPutMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ClientResultMessage.java (renamed from src/org/gnunet/dht/ClientResultMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/DistributedHashTable.java (renamed from src/org/gnunet/dht/DistributedHashTable.java) | 122 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorGetHandler.java (renamed from src/org/gnunet/dht/MonitorGetHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorGetMessage.java (renamed from src/org/gnunet/dht/MonitorGetMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorGetRespMessage.java (renamed from src/org/gnunet/dht/MonitorGetRespMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorGetResponseHandler.java (renamed from src/org/gnunet/dht/MonitorGetResponseHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorPutHandler.java (renamed from src/org/gnunet/dht/MonitorPutHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorPutMessage.java (renamed from src/org/gnunet/dht/MonitorPutMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/MonitorStartStop.java (renamed from src/org/gnunet/dht/MonitorStartStop.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/ResultCallback.java (renamed from src/org/gnunet/dht/ResultCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/RouteOption.java (renamed from src/org/gnunet/dht/RouteOption.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/dht/package-info.java (renamed from src/org/gnunet/dht/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/hello/HelloMessage.java (renamed from src/org/gnunet/hello/HelloMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/hello/package-info.java (renamed from src/org/gnunet/hello/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/ClientConnectMessage.java (renamed from src/org/gnunet/mesh/ClientConnectMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/ConnectHandler.java (renamed from src/org/gnunet/mesh/ConnectHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/DataMessage.java (renamed from src/org/gnunet/mesh/DataMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/DisconnectHandler.java (renamed from src/org/gnunet/mesh/DisconnectHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/InboundTunnelHandler.java (renamed from src/org/gnunet/mesh/InboundTunnelHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/LocalAckMessage.java (renamed from src/org/gnunet/mesh/LocalAckMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/Mesh.java (renamed from src/org/gnunet/mesh/Mesh.java) | 60 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/MeshRunabout.java (renamed from src/org/gnunet/mesh/MeshRunabout.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/TunnelCreateMessage.java (renamed from src/org/gnunet/mesh/TunnelCreateMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java (renamed from src/org/gnunet/mesh/TunnelDestroyMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/TunnelEndHandler.java (renamed from src/org/gnunet/mesh/TunnelEndHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/TunnelNotificationMessage.java (renamed from src/org/gnunet/mesh/TunnelNotificationMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mesh/package-info.java (renamed from src/org/gnunet/mesh/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mq/Envelope.java (renamed from src/org/gnunet/mq/Envelope.java) | 6 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mq/MessageQueue.java | 84 | ||||
-rw-r--r-- | src/main/java/org/gnunet/mq/NotifySentHandler.java (renamed from src/org/gnunet/mq/NotifySentHandler.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/nse/NetworkSizeEstimation.java (renamed from src/org/gnunet/nse/NetworkSizeEstimation.java) | 6 | ||||
-rw-r--r-- | src/main/java/org/gnunet/nse/StartMessage.java (renamed from src/org/gnunet/nse/StartMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/nse/UpdateMessage.java (renamed from src/org/gnunet/nse/UpdateMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/nse/package-info.java (renamed from src/org/gnunet/nse/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/InfoEnd.java (renamed from src/org/gnunet/peerinfo/InfoEnd.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/InfoMessage.java (renamed from src/org/gnunet/peerinfo/InfoMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/ListAllPeersMessage.java (renamed from src/org/gnunet/peerinfo/ListAllPeersMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/ListPeerMessage.java (renamed from src/org/gnunet/peerinfo/ListPeerMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/PeerInfo.java | 159 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/PeerProcessor.java (renamed from src/org/gnunet/peerinfo/PeerProcessor.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java (renamed from src/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/peerinfo/package-info.java (renamed from src/org/gnunet/peerinfo/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/requests/MatchingRequestContainer.java | 51 | ||||
-rw-r--r-- | src/main/java/org/gnunet/requests/RequestContainer.java | 59 | ||||
-rw-r--r-- | src/main/java/org/gnunet/requests/SequentialRequestContainer.java | 86 | ||||
-rw-r--r-- | src/main/java/org/gnunet/requests/package-info.java (renamed from src/org/gnunet/requests/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/GetMessage.java (renamed from src/org/gnunet/statistics/GetMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/GetRequest.java | 31 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/GetResponseEndMessage.java (renamed from src/org/gnunet/statistics/GetResponseEndMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/GetResponseMessage.java (renamed from src/org/gnunet/statistics/GetResponseMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/SetMessage.java (renamed from src/org/gnunet/statistics/SetMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/SetRequest.java | 48 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/Statistics.java | 310 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/StatisticsReceiver.java (renamed from src/org/gnunet/statistics/StatisticsReceiver.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/StatisticsWatcher.java | 13 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/WatchMessage.java (renamed from src/org/gnunet/statistics/WatchMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/WatchRequest.java | 31 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/WatchResponseMessage.java (renamed from src/org/gnunet/statistics/WatchResponseMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/statistics/package-info.java (renamed from src/org/gnunet/statistics/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/Controller.java | 84 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/ControllerEventCallback.java | 21 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/ControllerInitMessage.java | 30 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/ControllerProc.java | 89 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/ControllerStatusCallback.java | 9 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/HelperInitMessage.java | 37 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/HelperReplyMessage.java | 17 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/Host.java | 51 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/Operation.java | 11 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/OperationCompletionCallback.java | 11 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/Peer.java | 57 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/PeerChurnCallback.java | 7 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testbed/PeerCreateCallback.java | 7 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testing/TestingFixture.java | 21 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testing/TestingServer.java (renamed from src/org/gnunet/testing/TestingServer.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testing/TestingSetup.java (renamed from src/org/gnunet/testing/TestingSetup.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testing/TestingSubsystem.java (renamed from src/org/gnunet/testing/TestingSubsystem.java) | 16 | ||||
-rw-r--r-- | src/main/java/org/gnunet/testing/package-info.java (renamed from src/org/gnunet/testing/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/AddressIterateMessage.java | 34 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/BlacklistCallback.java | 12 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/BlacklistInitMessage.java | 9 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/HelloUpdateCallback.java (renamed from src/org/gnunet/transport/HelloUpdateCallback.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/PeerIterateCallback.java | 12 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/RequestConnectMessage.java (renamed from src/org/gnunet/transport/RequestConnectMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/StartMessage.java (renamed from src/org/gnunet/transport/StartMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/Transport.java (renamed from src/org/gnunet/transport/Transport.java) | 82 | ||||
-rw-r--r-- | src/main/java/org/gnunet/transport/TryConnectCallback.java (renamed from src/org/gnunet/transport/TryConnectCallback.java) | 3 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/ATSInformation.java (renamed from src/org/gnunet/util/ATSInformation.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/AbsoluteTime.java (renamed from src/org/gnunet/util/AbsoluteTime.java) | 4 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/AbsoluteTimeMessage.java (renamed from src/org/gnunet/util/AbsoluteTimeMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Cancelable.java (renamed from src/org/gnunet/util/Cancelable.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Client.java (renamed from src/org/gnunet/util/Client.java) | 85 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Configuration.java (renamed from src/org/gnunet/util/Configuration.java) | 11 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Connection.java (renamed from src/org/gnunet/util/Connection.java) | 9 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Continuation.java (renamed from src/org/gnunet/util/Continuation.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/GnunetMessage.java (renamed from src/org/gnunet/util/GnunetMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/HashCode.java (renamed from src/org/gnunet/util/HashCode.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Helper.java | 208 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/MessageReceiver.java (renamed from src/org/gnunet/util/MessageReceiver.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/MessageTransmitter.java (renamed from src/org/gnunet/util/MessageTransmitter.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/PeerIdentity.java (renamed from src/org/gnunet/util/PeerIdentity.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Program.java (renamed from src/org/gnunet/util/Program.java) | 8 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/RelativeTime.java (renamed from src/org/gnunet/util/RelativeTime.java) | 8 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/RelativeTimeMessage.java (renamed from src/org/gnunet/util/RelativeTimeMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Resolver.java (renamed from src/org/gnunet/util/Resolver.java) | 4 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/RunaboutMessageReceiver.java (renamed from src/org/gnunet/util/RunaboutMessageReceiver.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/RunaboutUtil.java (renamed from src/org/gnunet/util/RunaboutUtil.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Scheduler.java (renamed from src/org/gnunet/util/Scheduler.java) | 128 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Server.java (renamed from src/org/gnunet/util/Server.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Service.java (renamed from src/org/gnunet/util/Service.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/Strings.java (renamed from src/org/gnunet/util/Strings.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/TestMessage.java (renamed from src/org/gnunet/util/TestMessage.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/UnknownMessageBody.java (renamed from src/org/gnunet/util/UnknownMessageBody.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/getopt/Argument.java (renamed from src/org/gnunet/util/getopt/Argument.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/getopt/ArgumentAction.java (renamed from src/org/gnunet/util/getopt/ArgumentAction.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/getopt/Parser.java (renamed from src/org/gnunet/util/getopt/Parser.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/getopt/package-info.java (renamed from src/org/gnunet/util/getopt/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/util/package-info.java (renamed from src/org/gnunet/util/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/CertificateAuthorityService.java | 7 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/ElectionCallTool.java | 50 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/ElectionSpecification.java | 5 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/TallyAuthorityService.java | 5 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/VotingTool.java | 49 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/Authority.java (renamed from src/org/gnunet/voting/Authority.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/Ballot.java (renamed from src/org/gnunet/voting/Ballot.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/BogusAuthority.java (renamed from src/org/gnunet/voting/BogusAuthority.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/CallForVoters.java (renamed from src/org/gnunet/voting/CallForVoters.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/CryptoUtil.java (renamed from src/org/gnunet/voting/CryptoUtil.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/Cyphertext.java (renamed from src/org/gnunet/voting/Cyphertext.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/ElectionSupervisor.java (renamed from src/org/gnunet/voting/ElectionSupervisor.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/GroupPublicKey.java (renamed from src/org/gnunet/voting/GroupPublicKey.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/TallyKeyShare.java (renamed from src/org/gnunet/voting/TallyKeyShare.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/TransmitShareVerification.java (renamed from src/org/gnunet/voting/TransmitShareVerification.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/Voter.java (renamed from src/org/gnunet/voting/Voter.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/VotingParameters.java (renamed from src/org/gnunet/voting/VotingParameters.java) | 3 | ||||
-rw-r--r-- | src/main/java/org/gnunet/voting/simulation/VotingSimulation.java (renamed from src/org/gnunet/voting/VotingSimulation.java) | 2 | ||||
-rw-r--r-- | src/main/java/org/grothoff/Runabout.java (renamed from src/org/grothoff/Runabout.java) | 0 | ||||
-rw-r--r-- | src/main/java/org/grothoff/package-info.java (renamed from src/org/grothoff/package-info.java) | 0 | ||||
-rw-r--r-- | src/main/resources/org/gnunet/construct/MsgMap.txt | 46 | ||||
-rw-r--r-- | src/org/gnunet/mq/ClientMessageQueue.java | 44 | ||||
-rw-r--r-- | src/org/gnunet/mq/MessageQueue.java | 41 | ||||
-rw-r--r-- | src/org/gnunet/peerinfo/PeerInfo.java | 123 | ||||
-rw-r--r-- | src/org/gnunet/requests/FixedMessageRequest.java | 23 | ||||
-rw-r--r-- | src/org/gnunet/requests/Request.java | 82 | ||||
-rw-r--r-- | src/org/gnunet/requests/RequestQueue.java | 294 | ||||
-rw-r--r-- | src/org/gnunet/statistics/Statistics.java | 408 | ||||
-rw-r--r-- | src/org/gnunet/transport/BlacklistCallback.java | 9 | ||||
-rw-r--r-- | src/org/gnunet/transport/PeerIterateCallback.java | 9 | ||||
-rw-r--r-- | src/org/gnunet/util/HelperProcess.java | 21 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/ByteFillMessage.java | 32 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/ConstructTest.java | 82 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/DoubleTest.java | 35 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/FillParserTest.java | 37 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/FixedSizeTest.java | 52 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/FrameSizeTest.java | 50 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/IntMessage.java | 25 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/OptionalUnionTest.java | 72 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/PrivateMemberMessage.java | 13 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/QueryMessage.java | 31 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/SendMessageTest.java | 34 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/StringMessage.java | 32 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/StringTest.java | 50 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/StringTuple.java | 37 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/VariableSizeArrayTest.java | 33 | ||||
-rw-r--r-- | src/test/java/org/gnunet/construct/VariableSizeMessage.java | 17 | ||||
-rw-r--r-- | src/test/java/org/gnunet/core/CoreTest.java | 111 | ||||
-rw-r--r-- | src/test/java/org/gnunet/dht/DHTTest.java | 171 | ||||
-rw-r--r-- | src/test/java/org/gnunet/mesh/MeshTest.java | 47 | ||||
-rw-r--r-- | src/test/java/org/gnunet/nse/NSETest.java | 56 | ||||
-rw-r--r-- | src/test/java/org/gnunet/peerinfo/PeerInfoTest.java | 41 | ||||
-rw-r--r-- | src/test/java/org/gnunet/statistics/StatisticsTest.java | 180 | ||||
-rw-r--r-- | src/test/java/org/gnunet/testing/TestingSetupTest.java | 54 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/Assertion.java | 37 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/AssertionList.java | 30 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/ClientServerTest.java | 251 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/FilePipeExample.java | 41 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/ResolverTest.java | 92 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/ServerExample.java | 79 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/StringsTest.java | 20 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/TimeTest.java | 29 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/Wrapper.java | 43 | ||||
-rw-r--r-- | src/test/java/org/gnunet/util/getopt/GetoptTest.java | 286 | ||||
-rw-r--r-- | src/test/java/org/grothoff/RunaboutBenchmark.java | 82 |
257 files changed, 4545 insertions, 1437 deletions
diff --git a/src/org/gnunet/consensus/ConcludeCallback.java b/src/main/java/org/gnunet/consensus/ConcludeCallback.java index 7660c49..7660c49 100644 --- a/src/org/gnunet/consensus/ConcludeCallback.java +++ b/src/main/java/org/gnunet/consensus/ConcludeCallback.java | |||
diff --git a/src/org/gnunet/consensus/ConcludeDoneMessage.java b/src/main/java/org/gnunet/consensus/ConcludeDoneMessage.java index 51757aa..51757aa 100644 --- a/src/org/gnunet/consensus/ConcludeDoneMessage.java +++ b/src/main/java/org/gnunet/consensus/ConcludeDoneMessage.java | |||
diff --git a/src/org/gnunet/consensus/ConcludeMessage.java b/src/main/java/org/gnunet/consensus/ConcludeMessage.java index 7b43928..7b43928 100644 --- a/src/org/gnunet/consensus/ConcludeMessage.java +++ b/src/main/java/org/gnunet/consensus/ConcludeMessage.java | |||
diff --git a/src/org/gnunet/consensus/Consensus.java b/src/main/java/org/gnunet/consensus/Consensus.java index 2047454..322599c 100644 --- a/src/org/gnunet/consensus/Consensus.java +++ b/src/main/java/org/gnunet/consensus/Consensus.java | |||
@@ -1,6 +1,5 @@ | |||
1 | package org.gnunet.consensus; | 1 | package org.gnunet.consensus; |
2 | 2 | ||
3 | import org.gnunet.mq.ClientMessageQueue; | ||
4 | import org.gnunet.mq.Envelope; | 3 | import org.gnunet.mq.Envelope; |
5 | import org.gnunet.mq.MessageQueue; | 4 | import org.gnunet.mq.MessageQueue; |
6 | import org.gnunet.mq.NotifySentHandler; | 5 | import org.gnunet.mq.NotifySentHandler; |
@@ -30,11 +29,6 @@ public class Consensus { | |||
30 | private Client client; | 29 | private Client client; |
31 | 30 | ||
32 | /** | 31 | /** |
33 | * Message queue for 'client'. | ||
34 | */ | ||
35 | private MessageQueue client_mq; | ||
36 | |||
37 | /** | ||
38 | * Called when conclude has finished. | 32 | * Called when conclude has finished. |
39 | */ | 33 | */ |
40 | private ConcludeCallback concludeCallback; | 34 | private ConcludeCallback concludeCallback; |
@@ -81,7 +75,7 @@ public class Consensus { | |||
81 | public Consensus(Configuration cfg, int num_peers, PeerIdentity[] peers, HashCode sessionId, | 75 | public Consensus(Configuration cfg, int num_peers, PeerIdentity[] peers, HashCode sessionId, |
82 | NewElementCallback newElementCallback) { | 76 | NewElementCallback newElementCallback) { |
83 | client = new Client("consensus", cfg); | 77 | client = new Client("consensus", cfg); |
84 | client_mq = new ClientMessageQueue(client, new ConsensusMessageReceiver()); | 78 | client.installReceiver(new ConsensusMessageReceiver()); |
85 | this.newElementCallback = newElementCallback; | 79 | this.newElementCallback = newElementCallback; |
86 | } | 80 | } |
87 | 81 | ||
@@ -102,7 +96,7 @@ public class Consensus { | |||
102 | idc.onInsertDone(); | 96 | idc.onInsertDone(); |
103 | } | 97 | } |
104 | }); | 98 | }); |
105 | client_mq.send(ev); | 99 | client.send(ev); |
106 | } | 100 | } |
107 | 101 | ||
108 | /** | 102 | /** |
@@ -119,7 +113,7 @@ public class Consensus { | |||
119 | throw new AssertionError("called conclude twice"); | 113 | throw new AssertionError("called conclude twice"); |
120 | this.concludeCallback = concludeCallback; | 114 | this.concludeCallback = concludeCallback; |
121 | ConcludeMessage m = new ConcludeMessage(); | 115 | ConcludeMessage m = new ConcludeMessage(); |
122 | client_mq.send(m); | 116 | client.send(m); |
123 | } | 117 | } |
124 | 118 | ||
125 | /** | 119 | /** |
@@ -128,8 +122,6 @@ public class Consensus { | |||
128 | * it, no longer call any of the callbacks. | 122 | * it, no longer call any of the callbacks. |
129 | */ | 123 | */ |
130 | public void destroy() { | 124 | public void destroy() { |
131 | client_mq.destroy(); | ||
132 | client_mq = null; | ||
133 | client.disconnect(); | 125 | client.disconnect(); |
134 | client = null; | 126 | client = null; |
135 | } | 127 | } |
diff --git a/src/org/gnunet/consensus/ConsensusElement.java b/src/main/java/org/gnunet/consensus/ConsensusElement.java index 846e72a..846e72a 100644 --- a/src/org/gnunet/consensus/ConsensusElement.java +++ b/src/main/java/org/gnunet/consensus/ConsensusElement.java | |||
diff --git a/src/org/gnunet/consensus/InsertDoneCallback.java b/src/main/java/org/gnunet/consensus/InsertDoneCallback.java index f0e86ca..f0e86ca 100644 --- a/src/org/gnunet/consensus/InsertDoneCallback.java +++ b/src/main/java/org/gnunet/consensus/InsertDoneCallback.java | |||
diff --git a/src/org/gnunet/consensus/InsertElementMessage.java b/src/main/java/org/gnunet/consensus/InsertElementMessage.java index fd0ff67..fd0ff67 100644 --- a/src/org/gnunet/consensus/InsertElementMessage.java +++ b/src/main/java/org/gnunet/consensus/InsertElementMessage.java | |||
diff --git a/src/org/gnunet/consensus/NewElementCallback.java b/src/main/java/org/gnunet/consensus/NewElementCallback.java index 4b07a71..4b07a71 100644 --- a/src/org/gnunet/consensus/NewElementCallback.java +++ b/src/main/java/org/gnunet/consensus/NewElementCallback.java | |||
diff --git a/src/org/gnunet/consensus/NewElementMessage.java b/src/main/java/org/gnunet/consensus/NewElementMessage.java index deb3634..deb3634 100644 --- a/src/org/gnunet/consensus/NewElementMessage.java +++ b/src/main/java/org/gnunet/consensus/NewElementMessage.java | |||
diff --git a/src/org/gnunet/construct/Construct.java b/src/main/java/org/gnunet/construct/Construct.java index a74a5c2..a74a5c2 100644 --- a/src/org/gnunet/construct/Construct.java +++ b/src/main/java/org/gnunet/construct/Construct.java | |||
diff --git a/src/org/gnunet/construct/DoubleValue.java b/src/main/java/org/gnunet/construct/DoubleValue.java index 0856fe8..0856fe8 100644 --- a/src/org/gnunet/construct/DoubleValue.java +++ b/src/main/java/org/gnunet/construct/DoubleValue.java | |||
diff --git a/src/org/gnunet/construct/FillWith.java b/src/main/java/org/gnunet/construct/FillWith.java index f95aa3d..f95aa3d 100644 --- a/src/org/gnunet/construct/FillWith.java +++ b/src/main/java/org/gnunet/construct/FillWith.java | |||
diff --git a/src/org/gnunet/construct/FixedSizeArray.java b/src/main/java/org/gnunet/construct/FixedSizeArray.java index 699ae68..699ae68 100644 --- a/src/org/gnunet/construct/FixedSizeArray.java +++ b/src/main/java/org/gnunet/construct/FixedSizeArray.java | |||
diff --git a/src/org/gnunet/construct/FixedSizeIntegerArray.java b/src/main/java/org/gnunet/construct/FixedSizeIntegerArray.java index b2418b8..b2418b8 100644 --- a/src/org/gnunet/construct/FixedSizeIntegerArray.java +++ b/src/main/java/org/gnunet/construct/FixedSizeIntegerArray.java | |||
diff --git a/src/org/gnunet/construct/FrameSize.java b/src/main/java/org/gnunet/construct/FrameSize.java index 0533d0b..0533d0b 100644 --- a/src/org/gnunet/construct/FrameSize.java +++ b/src/main/java/org/gnunet/construct/FrameSize.java | |||
diff --git a/src/org/gnunet/construct/Int16.java b/src/main/java/org/gnunet/construct/Int16.java index 677324b..677324b 100644 --- a/src/org/gnunet/construct/Int16.java +++ b/src/main/java/org/gnunet/construct/Int16.java | |||
diff --git a/src/org/gnunet/construct/Int32.java b/src/main/java/org/gnunet/construct/Int32.java index da993ca..da993ca 100644 --- a/src/org/gnunet/construct/Int32.java +++ b/src/main/java/org/gnunet/construct/Int32.java | |||
diff --git a/src/org/gnunet/construct/Int64.java b/src/main/java/org/gnunet/construct/Int64.java index be5d940..be5d940 100644 --- a/src/org/gnunet/construct/Int64.java +++ b/src/main/java/org/gnunet/construct/Int64.java | |||
diff --git a/src/org/gnunet/construct/Int8.java b/src/main/java/org/gnunet/construct/Int8.java index a0aaa14..a0aaa14 100644 --- a/src/org/gnunet/construct/Int8.java +++ b/src/main/java/org/gnunet/construct/Int8.java | |||
diff --git a/src/org/gnunet/construct/IntegerFill.java b/src/main/java/org/gnunet/construct/IntegerFill.java index 4564c83..4564c83 100644 --- a/src/org/gnunet/construct/IntegerFill.java +++ b/src/main/java/org/gnunet/construct/IntegerFill.java | |||
diff --git a/src/org/gnunet/construct/Message.java b/src/main/java/org/gnunet/construct/Message.java index d0068b5..d0068b5 100644 --- a/src/org/gnunet/construct/Message.java +++ b/src/main/java/org/gnunet/construct/Message.java | |||
diff --git a/src/org/gnunet/construct/MessageIdAnnotationProcessor.java b/src/main/java/org/gnunet/construct/MessageIdAnnotationProcessor.java index 8a17ba9..ab80be3 100644 --- a/src/org/gnunet/construct/MessageIdAnnotationProcessor.java +++ b/src/main/java/org/gnunet/construct/MessageIdAnnotationProcessor.java | |||
@@ -47,8 +47,6 @@ import java.util.*; | |||
47 | @SupportedAnnotationTypes("org.gnunet.construct.UnionCase") | 47 | @SupportedAnnotationTypes("org.gnunet.construct.UnionCase") |
48 | @SupportedSourceVersion(SourceVersion.RELEASE_6) | 48 | @SupportedSourceVersion(SourceVersion.RELEASE_6) |
49 | public class MessageIdAnnotationProcessor extends AbstractProcessor { | 49 | public class MessageIdAnnotationProcessor extends AbstractProcessor { |
50 | // a mapping from (union, id) to member | ||
51 | //private final Map<String, Map<Integer, String>> unionmap = new HashMap<String, Map<Integer, String>>(100); | ||
52 | private final Table<String, Integer, String> idToMember = HashBasedTable.create(); | 50 | private final Table<String, Integer, String> idToMember = HashBasedTable.create(); |
53 | 51 | ||
54 | @Override | 52 | @Override |
diff --git a/src/org/gnunet/construct/MessageLoader.java b/src/main/java/org/gnunet/construct/MessageLoader.java index 71e2719..71e2719 100644 --- a/src/org/gnunet/construct/MessageLoader.java +++ b/src/main/java/org/gnunet/construct/MessageLoader.java | |||
diff --git a/src/org/gnunet/construct/MessageUnion.java b/src/main/java/org/gnunet/construct/MessageUnion.java index 0481df2..0481df2 100644 --- a/src/org/gnunet/construct/MessageUnion.java +++ b/src/main/java/org/gnunet/construct/MessageUnion.java | |||
diff --git a/src/org/gnunet/construct/MsgMap.txt b/src/main/java/org/gnunet/construct/MsgMap.txt index 7f3d2f4..30a4656 100644 --- a/src/org/gnunet/construct/MsgMap.txt +++ b/src/main/java/org/gnunet/construct/MsgMap.txt | |||
@@ -43,4 +43,4 @@ org.gnunet.util.GnunetMessage$Body|521=org.gnunet.consensus.InsertElementMessage | |||
43 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage | 43 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage |
44 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage | 44 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage |
45 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage | 45 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage |
46 | # generated 2013/08/13 20:15:12 | 46 | # generated 2013/08/22 21:14:59 |
diff --git a/src/org/gnunet/construct/NestedMessage.java b/src/main/java/org/gnunet/construct/NestedMessage.java index fe65323..fe65323 100644 --- a/src/org/gnunet/construct/NestedMessage.java +++ b/src/main/java/org/gnunet/construct/NestedMessage.java | |||
diff --git a/src/org/gnunet/construct/ProtocolViolationException.java b/src/main/java/org/gnunet/construct/ProtocolViolationException.java index 9de46d4..9de46d4 100644 --- a/src/org/gnunet/construct/ProtocolViolationException.java +++ b/src/main/java/org/gnunet/construct/ProtocolViolationException.java | |||
diff --git a/src/org/gnunet/construct/ReflectUtil.java b/src/main/java/org/gnunet/construct/ReflectUtil.java index 1115641..1115641 100644 --- a/src/org/gnunet/construct/ReflectUtil.java +++ b/src/main/java/org/gnunet/construct/ReflectUtil.java | |||
diff --git a/src/org/gnunet/construct/UInt16.java b/src/main/java/org/gnunet/construct/UInt16.java index 18c24b2..18c24b2 100644 --- a/src/org/gnunet/construct/UInt16.java +++ b/src/main/java/org/gnunet/construct/UInt16.java | |||
diff --git a/src/org/gnunet/construct/UInt32.java b/src/main/java/org/gnunet/construct/UInt32.java index ab4a278..ab4a278 100644 --- a/src/org/gnunet/construct/UInt32.java +++ b/src/main/java/org/gnunet/construct/UInt32.java | |||
diff --git a/src/org/gnunet/construct/UInt64.java b/src/main/java/org/gnunet/construct/UInt64.java index d45cf69..d45cf69 100644 --- a/src/org/gnunet/construct/UInt64.java +++ b/src/main/java/org/gnunet/construct/UInt64.java | |||
diff --git a/src/org/gnunet/construct/UInt8.java b/src/main/java/org/gnunet/construct/UInt8.java index 58b8335..58b8335 100644 --- a/src/org/gnunet/construct/UInt8.java +++ b/src/main/java/org/gnunet/construct/UInt8.java | |||
diff --git a/src/org/gnunet/construct/Union.java b/src/main/java/org/gnunet/construct/Union.java index d35512e..d35512e 100644 --- a/src/org/gnunet/construct/Union.java +++ b/src/main/java/org/gnunet/construct/Union.java | |||
diff --git a/src/org/gnunet/construct/UnionCase.java b/src/main/java/org/gnunet/construct/UnionCase.java index 7622dd4..7622dd4 100644 --- a/src/org/gnunet/construct/UnionCase.java +++ b/src/main/java/org/gnunet/construct/UnionCase.java | |||
diff --git a/src/org/gnunet/construct/VariableSizeArray.java b/src/main/java/org/gnunet/construct/VariableSizeArray.java index 64daece..64daece 100644 --- a/src/org/gnunet/construct/VariableSizeArray.java +++ b/src/main/java/org/gnunet/construct/VariableSizeArray.java | |||
diff --git a/src/org/gnunet/construct/VariableSizeIntegerArray.java b/src/main/java/org/gnunet/construct/VariableSizeIntegerArray.java index a3a9f28..a3a9f28 100644 --- a/src/org/gnunet/construct/VariableSizeIntegerArray.java +++ b/src/main/java/org/gnunet/construct/VariableSizeIntegerArray.java | |||
diff --git a/src/org/gnunet/construct/ZeroTerminatedString.java b/src/main/java/org/gnunet/construct/ZeroTerminatedString.java index 2d70d71..2d70d71 100644 --- a/src/org/gnunet/construct/ZeroTerminatedString.java +++ b/src/main/java/org/gnunet/construct/ZeroTerminatedString.java | |||
diff --git a/src/org/gnunet/construct/package-info.java b/src/main/java/org/gnunet/construct/package-info.java index add5d40..add5d40 100644 --- a/src/org/gnunet/construct/package-info.java +++ b/src/main/java/org/gnunet/construct/package-info.java | |||
diff --git a/src/org/gnunet/construct/parsers/DoubleParser.java b/src/main/java/org/gnunet/construct/parsers/DoubleParser.java index c6cc835..c6cc835 100644 --- a/src/org/gnunet/construct/parsers/DoubleParser.java +++ b/src/main/java/org/gnunet/construct/parsers/DoubleParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/FillParser.java b/src/main/java/org/gnunet/construct/parsers/FillParser.java index 156e120..156e120 100644 --- a/src/org/gnunet/construct/parsers/FillParser.java +++ b/src/main/java/org/gnunet/construct/parsers/FillParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/FixedSizeArrayParser.java b/src/main/java/org/gnunet/construct/parsers/FixedSizeArrayParser.java index 3dbc720..3dbc720 100644 --- a/src/org/gnunet/construct/parsers/FixedSizeArrayParser.java +++ b/src/main/java/org/gnunet/construct/parsers/FixedSizeArrayParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java b/src/main/java/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java index ae633b0..ae633b0 100644 --- a/src/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java +++ b/src/main/java/org/gnunet/construct/parsers/FixedSizeIntegerArrayParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/IntegerFillParser.java b/src/main/java/org/gnunet/construct/parsers/IntegerFillParser.java index 7027afc..7027afc 100644 --- a/src/org/gnunet/construct/parsers/IntegerFillParser.java +++ b/src/main/java/org/gnunet/construct/parsers/IntegerFillParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/IntegerParser.java b/src/main/java/org/gnunet/construct/parsers/IntegerParser.java index 4f71aa5..4f71aa5 100644 --- a/src/org/gnunet/construct/parsers/IntegerParser.java +++ b/src/main/java/org/gnunet/construct/parsers/IntegerParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/IntegerUtil.java b/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java index 83391de..83391de 100644 --- a/src/org/gnunet/construct/parsers/IntegerUtil.java +++ b/src/main/java/org/gnunet/construct/parsers/IntegerUtil.java | |||
diff --git a/src/org/gnunet/construct/parsers/NestedParser.java b/src/main/java/org/gnunet/construct/parsers/NestedParser.java index 733bf9e..76aa397 100644 --- a/src/org/gnunet/construct/parsers/NestedParser.java +++ b/src/main/java/org/gnunet/construct/parsers/NestedParser.java | |||
@@ -50,7 +50,7 @@ public class NestedParser implements Parser { | |||
50 | if (inner == null) { | 50 | if (inner == null) { |
51 | if (optional) | 51 | if (optional) |
52 | return 0; | 52 | return 0; |
53 | throw new AssertionError(); | 53 | throw new AssertionError(String.format("empty non-optional nested message in field '%s'", targetField)); |
54 | } | 54 | } |
55 | return nestedParser.getSize(inner); | 55 | return nestedParser.getSize(inner); |
56 | } | 56 | } |
diff --git a/src/org/gnunet/construct/parsers/Parser.java b/src/main/java/org/gnunet/construct/parsers/Parser.java index 3eb02a6..3eb02a6 100644 --- a/src/org/gnunet/construct/parsers/Parser.java +++ b/src/main/java/org/gnunet/construct/parsers/Parser.java | |||
diff --git a/src/org/gnunet/construct/parsers/SequenceParser.java b/src/main/java/org/gnunet/construct/parsers/SequenceParser.java index 4128e1b..4128e1b 100644 --- a/src/org/gnunet/construct/parsers/SequenceParser.java +++ b/src/main/java/org/gnunet/construct/parsers/SequenceParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/StringParser.java b/src/main/java/org/gnunet/construct/parsers/StringParser.java index 8df171e..46d98ae 100644 --- a/src/org/gnunet/construct/parsers/StringParser.java +++ b/src/main/java/org/gnunet/construct/parsers/StringParser.java | |||
@@ -81,7 +81,6 @@ public class StringParser implements Parser { | |||
81 | 81 | ||
82 | int length = 0; | 82 | int length = 0; |
83 | 83 | ||
84 | |||
85 | while (srcBuf.get(srcBuf.position() + length) != 0) { | 84 | while (srcBuf.get(srcBuf.position() + length) != 0) { |
86 | length++; | 85 | length++; |
87 | } | 86 | } |
diff --git a/src/org/gnunet/construct/parsers/UnionParser.java b/src/main/java/org/gnunet/construct/parsers/UnionParser.java index e9fcc92..e9fcc92 100644 --- a/src/org/gnunet/construct/parsers/UnionParser.java +++ b/src/main/java/org/gnunet/construct/parsers/UnionParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java b/src/main/java/org/gnunet/construct/parsers/VariableSizeArrayParser.java index fd55925..fd55925 100644 --- a/src/org/gnunet/construct/parsers/VariableSizeArrayParser.java +++ b/src/main/java/org/gnunet/construct/parsers/VariableSizeArrayParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java b/src/main/java/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java index b3f134d..b3f134d 100644 --- a/src/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java +++ b/src/main/java/org/gnunet/construct/parsers/VariableSizeIntegerArrayParser.java | |||
diff --git a/src/org/gnunet/construct/parsers/package-info.java b/src/main/java/org/gnunet/construct/parsers/package-info.java index 5da3a94..5da3a94 100644 --- a/src/org/gnunet/construct/parsers/package-info.java +++ b/src/main/java/org/gnunet/construct/parsers/package-info.java | |||
diff --git a/src/org/gnunet/core/ConnectHandler.java b/src/main/java/org/gnunet/core/ConnectHandler.java index a36d798..a36d798 100644 --- a/src/org/gnunet/core/ConnectHandler.java +++ b/src/main/java/org/gnunet/core/ConnectHandler.java | |||
diff --git a/src/org/gnunet/core/ConnectNotifyMessage.java b/src/main/java/org/gnunet/core/ConnectNotifyMessage.java index 4eafe02..4eafe02 100644 --- a/src/org/gnunet/core/ConnectNotifyMessage.java +++ b/src/main/java/org/gnunet/core/ConnectNotifyMessage.java | |||
diff --git a/src/org/gnunet/core/Core.java b/src/main/java/org/gnunet/core/Core.java index 8fffe08..2895e41 100644 --- a/src/org/gnunet/core/Core.java +++ b/src/main/java/org/gnunet/core/Core.java | |||
@@ -20,17 +20,18 @@ | |||
20 | 20 | ||
21 | package org.gnunet.core; | 21 | package org.gnunet.core; |
22 | 22 | ||
23 | import com.google.common.collect.Maps; | ||
23 | import org.gnunet.construct.Construct; | 24 | import org.gnunet.construct.Construct; |
24 | import org.gnunet.construct.MessageLoader; | 25 | import org.gnunet.construct.MessageLoader; |
25 | import org.gnunet.requests.Request; | 26 | import org.gnunet.mq.Envelope; |
26 | import org.gnunet.requests.RequestQueue; | 27 | import org.gnunet.requests.MatchingRequestContainer; |
28 | import org.gnunet.requests.RequestContainer; | ||
27 | import org.gnunet.util.*; | 29 | import org.gnunet.util.*; |
28 | import org.grothoff.Runabout; | 30 | import org.grothoff.Runabout; |
29 | import org.slf4j.Logger; | 31 | import org.slf4j.Logger; |
30 | import org.slf4j.LoggerFactory; | 32 | import org.slf4j.LoggerFactory; |
31 | 33 | ||
32 | import java.util.HashMap; | 34 | import java.util.HashMap; |
33 | import java.util.Map; | ||
34 | 35 | ||
35 | 36 | ||
36 | /** | 37 | /** |
@@ -39,158 +40,111 @@ import java.util.Map; | |||
39 | * Sends messages to connected peers. | 40 | * Sends messages to connected peers. |
40 | */ | 41 | */ |
41 | public class Core { | 42 | public class Core { |
43 | /** | ||
44 | * Logger for org.gnunet.Core. | ||
45 | */ | ||
42 | private static final Logger logger = LoggerFactory | 46 | private static final Logger logger = LoggerFactory |
43 | .getLogger(Core.class); | 47 | .getLogger(Core.class); |
44 | 48 | ||
49 | /** | ||
50 | * Client for connecting to the core service | ||
51 | */ | ||
45 | private final Client client; | 52 | private final Client client; |
46 | 53 | ||
47 | private final RequestQueue requestQueue; | ||
48 | |||
49 | /* | 54 | /* |
50 | * set to null once connected for the first time | 55 | * set to null once connected for the first time |
51 | */ | 56 | */ |
52 | private InitCallback initCallback; | 57 | private InitCallback initCallback; |
53 | 58 | ||
54 | /* | 59 | /* |
55 | * Callbacks for traffic notifications. null if not used. | 60 | * Callback for traffic notifications. null if not interested. |
56 | */ | 61 | */ |
57 | private HeaderNotify notifyOutboundHeaders; | 62 | private HeaderNotify notifyOutboundHeaders; |
63 | |||
64 | /* | ||
65 | * Callback for traffic notifications. null if not interested. | ||
66 | */ | ||
58 | private HeaderNotify notifyInboundHeaders; | 67 | private HeaderNotify notifyInboundHeaders; |
68 | |||
69 | /* | ||
70 | * Callback for traffic notifications. null if not interested. | ||
71 | */ | ||
59 | private MessageNotify notifyOutboundMessages; | 72 | private MessageNotify notifyOutboundMessages; |
60 | private MessageNotify notifyInboundMessages; | ||
61 | 73 | ||
74 | /* | ||
75 | * Callback for traffic notifications. null if not interested. | ||
76 | */ | ||
77 | private MessageNotify notifyInboundMessages; | ||
62 | 78 | ||
63 | /* | 79 | /* |
64 | * Callbacks for connect/disconnect events | 80 | * Callbacks for connect events |
65 | */ | 81 | */ |
66 | private ConnectHandler connectHandler; | 82 | private ConnectHandler connectHandler; |
67 | private DisconnectHandler disconnectHandler; | ||
68 | |||
69 | 83 | ||
70 | // per default we are interested in all messages => specific interest set is empty | ||
71 | private int[] interested = new int[0]; | ||
72 | /** | 84 | /** |
73 | * Handler for the messages we are interested in. | 85 | * Callback for disconnect events. |
74 | */ | 86 | */ |
75 | private Runabout messageHandler; | 87 | private DisconnectHandler disconnectHandler; |
76 | |||
77 | 88 | ||
78 | /** | 89 | /** |
79 | * All currently connected peers with information about them attached | 90 | * Messages we are interested in. |
91 | * Per default we are interested in all messages => specific interest set is empty. | ||
80 | */ | 92 | */ |
81 | HashMap<PeerIdentity, ConnectedPeerInfo> connectedPeers = new HashMap<PeerIdentity, ConnectedPeerInfo>(10); | 93 | private int[] interested = new int[0]; |
82 | |||
83 | 94 | ||
84 | /** | 95 | /** |
85 | * Information about the requests on connected peers. | 96 | * Handler for the messages we are interested in. |
86 | */ | 97 | */ |
87 | private static class ConnectedPeerInfo { | 98 | private Runabout messageHandler; |
88 | public Map<Integer, NotifyTransmitReadyRequest> requestsToPeer = new HashMap<Integer, | ||
89 | NotifyTransmitReadyRequest>(1); | ||
90 | public int nextSmrId; | ||
91 | } | ||
92 | |||
93 | 99 | ||
94 | /** | 100 | /** |
95 | * Request to transmit a message, sent after the corresponding NotifyTransmitRequest has been | 101 | * Peers that we were notified about being connected to them. |
96 | * approved by core. | 102 | * Every connected peer is mapped to a generator for unique request IDs. |
97 | */ | 103 | */ |
98 | private static class TransmitRequest extends Request { | 104 | private HashMap<PeerIdentity, Integer> connectedPeers = Maps.newHashMap(); |
99 | public NotifyTransmitReadyRequest origin; | ||
100 | public GnunetMessage message; | ||
101 | |||
102 | @Override | ||
103 | public AbsoluteTime getDeadline() { | ||
104 | return origin.getDeadline(); | ||
105 | } | ||
106 | |||
107 | @Override | ||
108 | public void transmit(Connection.MessageSink sink) { | ||
109 | SendMessage sm = new SendMessage(); | ||
110 | sm.cork = 0; | ||
111 | sm.deadline = origin.getDeadline().asMessage(); | ||
112 | sm.peer = origin.target; | ||
113 | sm.priority = origin.priority; | ||
114 | sm.payloadMessage = message; | ||
115 | |||
116 | sink.send(sm); | ||
117 | } | ||
118 | |||
119 | @Override | ||
120 | public void onCancel(boolean alreadyTransmitted) { | ||
121 | throw new AssertionError("TransmitRequest cannot be canceled"); | ||
122 | } | ||
123 | } | ||
124 | 105 | ||
125 | /** | 106 | /** |
126 | * Sent after we get a SendMessageReady | 107 | * Request container for notify transmit requests. |
127 | */ | 108 | */ |
128 | private static class NotifyTransmitReadyRequest extends Request { | 109 | private MatchingRequestContainer<RequestIdentification, NotifyTransmitReadyRequest> ntr_requests; |
129 | public int size; | ||
130 | public int priority; | ||
131 | public PeerIdentity target; | ||
132 | public MessageTransmitter transmitter; | ||
133 | public int smrId; | ||
134 | public Cancelable transmitRequestCancel; | ||
135 | |||
136 | |||
137 | @Override | ||
138 | public void transmit(Connection.MessageSink sink) { | ||
139 | SendMessageRequest smr = new SendMessageRequest(); | ||
140 | smr.queueSize = 0; | ||
141 | smr.priority = priority; | ||
142 | smr.deadline = deadline.asMessage(); | ||
143 | smr.smrId = smrId; | ||
144 | smr.peer = target; | ||
145 | |||
146 | sink.send(smr); | ||
147 | } | ||
148 | |||
149 | @Override | ||
150 | public void onCancel(boolean alreadyTransmitted) { | ||
151 | // only thing we have to do is cancel the following transmitRequest, if any! | ||
152 | if (transmitRequestCancel != null) { | ||
153 | transmitRequestCancel.cancel(); | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | private class InitRequest extends Request { | ||
159 | @Override | ||
160 | public AbsoluteTime getDeadline() { | ||
161 | return AbsoluteTime.FOREVER; | ||
162 | } | ||
163 | |||
164 | @Override | ||
165 | public void transmit(Connection.MessageSink sink) { | ||
166 | InitMessage initMessage = new InitMessage(); | ||
167 | |||
168 | initMessage.interested = interested; | ||
169 | initMessage.options = 0; | ||
170 | |||
171 | for (int i : interested) { | ||
172 | logger.info("we are interested in " + i); | ||
173 | } | ||
174 | 110 | ||
175 | sink.send(initMessage); | 111 | public static class NotifyTransmitReadyRequest extends RequestContainer.Request { |
112 | private final int size; | ||
113 | final public PeerIdentity target; | ||
114 | final public long priority; | ||
115 | public int smrId; | ||
116 | final public MessageTransmitter transmitter; | ||
117 | final public AbsoluteTime deadline; | ||
118 | |||
119 | public NotifyTransmitReadyRequest(int priority, int size, PeerIdentity target, RelativeTime timeout, MessageTransmitter transmitter) { | ||
120 | this.deadline = timeout.toAbsolute(); | ||
121 | this.priority = priority; | ||
122 | this.size = size; | ||
123 | this.target = target; | ||
124 | this.transmitter = transmitter; | ||
176 | } | 125 | } |
177 | 126 | ||
178 | @Override | 127 | @Override |
179 | public void onCancel(boolean alreadyTransmitted) { | 128 | public Envelope assembleRequest() { |
180 | throw new AssertionError("init request can't be canceled"); | 129 | SendMessageRequest m = new SendMessageRequest(); |
130 | m.peer = target; | ||
131 | m.smrId = smrId; | ||
132 | m.priority = priority; | ||
133 | m.size = size; | ||
134 | m.deadline = deadline.asMessage(); | ||
135 | return new Envelope(m); | ||
181 | } | 136 | } |
182 | 137 | ||
183 | @Override | 138 | public void cancel() { |
184 | public boolean onReconnect() { | 139 | // do nothing |
185 | // keep the init message on reconnect. | ||
186 | return true; | ||
187 | } | 140 | } |
188 | } | 141 | } |
189 | 142 | ||
190 | public class CoreReceiver extends RunaboutMessageReceiver { | 143 | |
144 | public final class CoreReceiver extends RunaboutMessageReceiver { | ||
191 | public void visit(InitReplyMessage m) { | 145 | public void visit(InitReplyMessage m) { |
192 | PeerIdentity myIdentity = m.myIdentity; | 146 | PeerIdentity myIdentity = m.myIdentity; |
193 | connectedPeers.put(myIdentity, new ConnectedPeerInfo()); | 147 | connectedPeers.put(myIdentity, 1); |
194 | 148 | ||
195 | if (initCallback != null) { | 149 | if (initCallback != null) { |
196 | initCallback.onInit(m.myIdentity); | 150 | initCallback.onInit(m.myIdentity); |
@@ -243,12 +197,14 @@ public class Core { | |||
243 | } | 197 | } |
244 | 198 | ||
245 | public void visit(SendMessageReady m) { | 199 | public void visit(SendMessageReady m) { |
246 | ConnectedPeerInfo cpi = connectedPeers.get(m.peer); | 200 | RequestIdentification rid = new RequestIdentification(m.smrId, m.peer); |
247 | NotifyTransmitReadyRequest req = cpi.requestsToPeer.get(m.smrId); | 201 | NotifyTransmitReadyRequest req = ntr_requests.getRequest(rid); |
248 | |||
249 | final TransmitRequest transmitRequest = new TransmitRequest(); | ||
250 | transmitRequest.origin = req; | ||
251 | 202 | ||
203 | final SendMessage sm = new SendMessage(); | ||
204 | sm.cork = 0; | ||
205 | sm.peer = req.target; | ||
206 | sm.priority = req.priority; | ||
207 | sm.deadline = req.deadline.asMessage(); | ||
252 | 208 | ||
253 | req.transmitter.transmit(new Connection.MessageSink() { | 209 | req.transmitter.transmit(new Connection.MessageSink() { |
254 | boolean sent; | 210 | boolean sent; |
@@ -257,12 +213,16 @@ public class Core { | |||
257 | if (sent) { | 213 | if (sent) { |
258 | throw new AssertionError("sending multiple messages not supported"); | 214 | throw new AssertionError("sending multiple messages not supported"); |
259 | } | 215 | } |
260 | transmitRequest.message = GnunetMessage.fromBody(m); | 216 | sm.payloadMessage = GnunetMessage.fromBody(m); |
261 | sent = true; | 217 | sent = true; |
262 | } | 218 | } |
263 | }); | 219 | }); |
264 | 220 | ||
265 | req.transmitRequestCancel = requestQueue.add(transmitRequest); | 221 | |
222 | if (sm.payloadMessage == null) | ||
223 | throw new AssertionError(); | ||
224 | |||
225 | client.send(sm); | ||
266 | } | 226 | } |
267 | 227 | ||
268 | @Override | 228 | @Override |
@@ -273,8 +233,8 @@ public class Core { | |||
273 | @Override | 233 | @Override |
274 | public void handleError() { | 234 | public void handleError() { |
275 | if (disconnectHandler != null) { | 235 | if (disconnectHandler != null) { |
276 | for (Map.Entry<PeerIdentity, ConnectedPeerInfo> e : connectedPeers.entrySet()) { | 236 | for (PeerIdentity e : connectedPeers.keySet()) { |
277 | disconnectHandler.onDisconnect(e.getKey()); | 237 | disconnectHandler.onDisconnect(e); |
278 | } | 238 | } |
279 | } | 239 | } |
280 | connectedPeers.clear(); | 240 | connectedPeers.clear(); |
@@ -283,12 +243,26 @@ public class Core { | |||
283 | 243 | ||
284 | public Core(Configuration cfg) { | 244 | public Core(Configuration cfg) { |
285 | client = new Client("core", cfg); | 245 | client = new Client("core", cfg); |
286 | requestQueue = new RequestQueue(client, new CoreReceiver()); | 246 | client.installReceiver(new CoreReceiver()); |
247 | ntr_requests = new MatchingRequestContainer<RequestIdentification, NotifyTransmitReadyRequest>(client); | ||
287 | } | 248 | } |
288 | 249 | ||
250 | /** | ||
251 | * Send to the service which messages are we interested in. | ||
252 | * | ||
253 | * @param initCallback called after the init message has been sent | ||
254 | */ | ||
289 | public void init(InitCallback initCallback) { | 255 | public void init(InitCallback initCallback) { |
290 | this.initCallback = initCallback; | 256 | this.initCallback = initCallback; |
291 | requestQueue.sendNext(new InitRequest()); | 257 | InitMessage initMessage = new InitMessage(); |
258 | |||
259 | initMessage.interested = interested; | ||
260 | initMessage.options = 0; | ||
261 | |||
262 | for (int i : interested) { | ||
263 | logger.debug("we are interested in " + i); | ||
264 | } | ||
265 | client.sendPrefered(initMessage); | ||
292 | } | 266 | } |
293 | 267 | ||
294 | /** | 268 | /** |
@@ -306,27 +280,21 @@ public class Core { | |||
306 | */ | 280 | */ |
307 | public Cancelable notifyTransmitReady(int priority, RelativeTime maxdelay, | 281 | public Cancelable notifyTransmitReady(int priority, RelativeTime maxdelay, |
308 | PeerIdentity target, int size, final MessageTransmitter transmitter) { | 282 | PeerIdentity target, int size, final MessageTransmitter transmitter) { |
309 | NotifyTransmitReadyRequest notifyRequest = new NotifyTransmitReadyRequest(); | 283 | if (!connectedPeers.containsKey(target)) { |
310 | notifyRequest.priority = priority; | ||
311 | notifyRequest.size = size; | ||
312 | notifyRequest.target = target; | ||
313 | notifyRequest.transmitter = transmitter; | ||
314 | notifyRequest.setDeadline(maxdelay.toAbsolute()); | ||
315 | |||
316 | ConnectedPeerInfo cpi = connectedPeers.get(target); | ||
317 | if (cpi == null) { | ||
318 | throw new AssertionError("notifyTransmitReady called for unconnected peer"); | 284 | throw new AssertionError("notifyTransmitReady called for unconnected peer"); |
319 | } | 285 | } |
320 | 286 | int id = connectedPeers.get(target); | |
321 | notifyRequest.smrId = cpi.nextSmrId; | 287 | connectedPeers.put(target, id+1); |
322 | cpi.nextSmrId++; | 288 | NotifyTransmitReadyRequest notifyRequest = new NotifyTransmitReadyRequest(priority, size, target, maxdelay, transmitter); |
323 | 289 | notifyRequest.smrId = id; | |
324 | cpi.requestsToPeer.put(notifyRequest.smrId, notifyRequest); | 290 | RequestIdentification rid = new RequestIdentification(notifyRequest.smrId, target); |
325 | 291 | return ntr_requests.addRequest(rid, notifyRequest); | |
326 | return requestQueue.add(notifyRequest); | ||
327 | } | 292 | } |
328 | 293 | ||
329 | 294 | /** | |
295 | * Observe outgoing message headers from core. | ||
296 | * @param h callback | ||
297 | */ | ||
330 | public void observeOutboundHeaders(HeaderNotify h) { | 298 | public void observeOutboundHeaders(HeaderNotify h) { |
331 | this.notifyOutboundHeaders = h; | 299 | this.notifyOutboundHeaders = h; |
332 | } | 300 | } |
@@ -353,7 +321,8 @@ public class Core { | |||
353 | 321 | ||
354 | /** | 322 | /** |
355 | * Handle all incoming messages with the specified runabout. | 323 | * Handle all incoming messages with the specified runabout. |
356 | * Has to be called before init. | 324 | * Has to be called before init, as the service has to know which messages we |
325 | * are interested in. | ||
357 | */ | 326 | */ |
358 | public void setMessageHandler(Runabout runabout) { | 327 | public void setMessageHandler(Runabout runabout) { |
359 | if (messageHandler != null) { | 328 | if (messageHandler != null) { |
@@ -373,7 +342,6 @@ public class Core { | |||
373 | * requests have been explicitly cancelled. | 342 | * requests have been explicitly cancelled. |
374 | */ | 343 | */ |
375 | public void disconnect() { | 344 | public void disconnect() { |
376 | requestQueue.destroy(); | ||
377 | client.disconnect(); | 345 | client.disconnect(); |
378 | } | 346 | } |
379 | } | 347 | } |
diff --git a/src/org/gnunet/core/DisconnectHandler.java b/src/main/java/org/gnunet/core/DisconnectHandler.java index c7ca407..c7ca407 100644 --- a/src/org/gnunet/core/DisconnectHandler.java +++ b/src/main/java/org/gnunet/core/DisconnectHandler.java | |||
diff --git a/src/org/gnunet/core/DisconnectNotifyMessage.java b/src/main/java/org/gnunet/core/DisconnectNotifyMessage.java index e4c3209..e4c3209 100644 --- a/src/org/gnunet/core/DisconnectNotifyMessage.java +++ b/src/main/java/org/gnunet/core/DisconnectNotifyMessage.java | |||
diff --git a/src/org/gnunet/core/HeaderNotify.java b/src/main/java/org/gnunet/core/HeaderNotify.java index 4f536e3..4f536e3 100644 --- a/src/org/gnunet/core/HeaderNotify.java +++ b/src/main/java/org/gnunet/core/HeaderNotify.java | |||
diff --git a/src/org/gnunet/core/InitCallback.java b/src/main/java/org/gnunet/core/InitCallback.java index 889f8cf..889f8cf 100644 --- a/src/org/gnunet/core/InitCallback.java +++ b/src/main/java/org/gnunet/core/InitCallback.java | |||
diff --git a/src/org/gnunet/core/InitMessage.java b/src/main/java/org/gnunet/core/InitMessage.java index 5546088..5546088 100644 --- a/src/org/gnunet/core/InitMessage.java +++ b/src/main/java/org/gnunet/core/InitMessage.java | |||
diff --git a/src/org/gnunet/core/InitReplyMessage.java b/src/main/java/org/gnunet/core/InitReplyMessage.java index 02e8eef..02e8eef 100644 --- a/src/org/gnunet/core/InitReplyMessage.java +++ b/src/main/java/org/gnunet/core/InitReplyMessage.java | |||
diff --git a/src/org/gnunet/core/MessageNotify.java b/src/main/java/org/gnunet/core/MessageNotify.java index b14ce29..b14ce29 100644 --- a/src/org/gnunet/core/MessageNotify.java +++ b/src/main/java/org/gnunet/core/MessageNotify.java | |||
diff --git a/src/org/gnunet/core/NotifyInboundTrafficMessage.java b/src/main/java/org/gnunet/core/NotifyInboundTrafficMessage.java index 2bdd428..2bdd428 100644 --- a/src/org/gnunet/core/NotifyInboundTrafficMessage.java +++ b/src/main/java/org/gnunet/core/NotifyInboundTrafficMessage.java | |||
diff --git a/src/org/gnunet/core/NotifyOutboundTrafficMessage.java b/src/main/java/org/gnunet/core/NotifyOutboundTrafficMessage.java index 900f8be..900f8be 100644 --- a/src/org/gnunet/core/NotifyOutboundTrafficMessage.java +++ b/src/main/java/org/gnunet/core/NotifyOutboundTrafficMessage.java | |||
diff --git a/src/main/java/org/gnunet/core/RequestIdentification.java b/src/main/java/org/gnunet/core/RequestIdentification.java new file mode 100644 index 0000000..4f6a734 --- /dev/null +++ b/src/main/java/org/gnunet/core/RequestIdentification.java | |||
@@ -0,0 +1,35 @@ | |||
1 | package org.gnunet.core; | ||
2 | |||
3 | import org.gnunet.peerinfo.PeerInfo; | ||
4 | import org.gnunet.util.PeerIdentity; | ||
5 | |||
6 | |||
7 | final class RequestIdentification { | ||
8 | public final int requestIdentifier; | ||
9 | public final PeerIdentity peerIdentity; | ||
10 | |||
11 | public RequestIdentification(int requestIdentifier, PeerIdentity peerIdentity) { | ||
12 | this.requestIdentifier = requestIdentifier; | ||
13 | this.peerIdentity = peerIdentity; | ||
14 | } | ||
15 | |||
16 | @Override | ||
17 | public boolean equals(Object o) { | ||
18 | if (this == o) return true; | ||
19 | if (o == null || getClass() != o.getClass()) return false; | ||
20 | |||
21 | RequestIdentification that = (RequestIdentification) o; | ||
22 | |||
23 | if (requestIdentifier != that.requestIdentifier) return false; | ||
24 | if (!peerIdentity.equals(that.peerIdentity)) return false; | ||
25 | |||
26 | return true; | ||
27 | } | ||
28 | |||
29 | @Override | ||
30 | public int hashCode() { | ||
31 | int result = requestIdentifier; | ||
32 | result = 31 * result + peerIdentity.hashCode(); | ||
33 | return result; | ||
34 | } | ||
35 | } | ||
diff --git a/src/org/gnunet/core/SendMessage.java b/src/main/java/org/gnunet/core/SendMessage.java index e4c6215..e4c6215 100644 --- a/src/org/gnunet/core/SendMessage.java +++ b/src/main/java/org/gnunet/core/SendMessage.java | |||
diff --git a/src/org/gnunet/core/SendMessageReady.java b/src/main/java/org/gnunet/core/SendMessageReady.java index aa5bf44..aa5bf44 100644 --- a/src/org/gnunet/core/SendMessageReady.java +++ b/src/main/java/org/gnunet/core/SendMessageReady.java | |||
diff --git a/src/org/gnunet/core/SendMessageRequest.java b/src/main/java/org/gnunet/core/SendMessageRequest.java index 17087d3..7a95127 100644 --- a/src/org/gnunet/core/SendMessageRequest.java +++ b/src/main/java/org/gnunet/core/SendMessageRequest.java | |||
@@ -57,7 +57,7 @@ public class SendMessageRequest implements GnunetMessage.Body { | |||
57 | * How large is the client's message queue for this peer? | 57 | * How large is the client's message queue for this peer? |
58 | */ | 58 | */ |
59 | @UInt32 | 59 | @UInt32 |
60 | public long queueSize; | 60 | public byte reserved; |
61 | 61 | ||
62 | /** | 62 | /** |
63 | * How large is the message? | 63 | * How large is the message? |
diff --git a/src/org/gnunet/core/package-info.java b/src/main/java/org/gnunet/core/package-info.java index 64e5d59..64e5d59 100644 --- a/src/org/gnunet/core/package-info.java +++ b/src/main/java/org/gnunet/core/package-info.java | |||
diff --git a/src/org/gnunet/dht/BlockType.java b/src/main/java/org/gnunet/dht/BlockType.java index cf00d38..cf00d38 100644 --- a/src/org/gnunet/dht/BlockType.java +++ b/src/main/java/org/gnunet/dht/BlockType.java | |||
diff --git a/src/org/gnunet/dht/ClientGetMessage.java b/src/main/java/org/gnunet/dht/ClientGetMessage.java index cd317fb..cd317fb 100644 --- a/src/org/gnunet/dht/ClientGetMessage.java +++ b/src/main/java/org/gnunet/dht/ClientGetMessage.java | |||
diff --git a/src/org/gnunet/dht/ClientGetStopMessage.java b/src/main/java/org/gnunet/dht/ClientGetStopMessage.java index 4cdee12..4cdee12 100644 --- a/src/org/gnunet/dht/ClientGetStopMessage.java +++ b/src/main/java/org/gnunet/dht/ClientGetStopMessage.java | |||
diff --git a/src/org/gnunet/dht/ClientPutConfirmationMessage.java b/src/main/java/org/gnunet/dht/ClientPutConfirmationMessage.java index 45bbe60..45bbe60 100644 --- a/src/org/gnunet/dht/ClientPutConfirmationMessage.java +++ b/src/main/java/org/gnunet/dht/ClientPutConfirmationMessage.java | |||
diff --git a/src/org/gnunet/dht/ClientPutMessage.java b/src/main/java/org/gnunet/dht/ClientPutMessage.java index 4b63e92..4b63e92 100644 --- a/src/org/gnunet/dht/ClientPutMessage.java +++ b/src/main/java/org/gnunet/dht/ClientPutMessage.java | |||
diff --git a/src/org/gnunet/dht/ClientResultMessage.java b/src/main/java/org/gnunet/dht/ClientResultMessage.java index fab614f..fab614f 100644 --- a/src/org/gnunet/dht/ClientResultMessage.java +++ b/src/main/java/org/gnunet/dht/ClientResultMessage.java | |||
diff --git a/src/org/gnunet/dht/DistributedHashTable.java b/src/main/java/org/gnunet/dht/DistributedHashTable.java index 3f42492..0a561bf 100644 --- a/src/org/gnunet/dht/DistributedHashTable.java +++ b/src/main/java/org/gnunet/dht/DistributedHashTable.java | |||
@@ -21,10 +21,10 @@ | |||
21 | package org.gnunet.dht; | 21 | package org.gnunet.dht; |
22 | 22 | ||
23 | import com.google.common.base.Charsets; | 23 | import com.google.common.base.Charsets; |
24 | import com.google.common.collect.Lists; | 24 | import org.gnunet.mq.Envelope; |
25 | import com.google.common.collect.Maps; | 25 | import org.gnunet.requests.MatchingRequestContainer; |
26 | import org.gnunet.requests.Request; | 26 | import org.gnunet.requests.RequestContainer; |
27 | import org.gnunet.requests.RequestQueue; | 27 | import org.gnunet.requests.SequentialRequestContainer; |
28 | import org.gnunet.util.*; | 28 | import org.gnunet.util.*; |
29 | import org.gnunet.util.getopt.Argument; | 29 | import org.gnunet.util.getopt.Argument; |
30 | import org.gnunet.util.getopt.ArgumentAction; | 30 | import org.gnunet.util.getopt.ArgumentAction; |
@@ -43,36 +43,18 @@ public class DistributedHashTable { | |||
43 | private static final Logger logger = LoggerFactory | 43 | private static final Logger logger = LoggerFactory |
44 | .getLogger(DistributedHashTable.class); | 44 | .getLogger(DistributedHashTable.class); |
45 | 45 | ||
46 | |||
47 | private Client client; | 46 | private Client client; |
48 | 47 | ||
49 | private RequestQueue requestQueue; | ||
50 | |||
51 | /** | 48 | /** |
52 | * next UID used on get/monitor requests, incremented after each use. | 49 | * next UID used on get/monitor requests, incremented after each use. |
53 | */ | 50 | */ |
54 | private long nextUID = 1; | 51 | private long nextUID = 1; |
55 | 52 | ||
56 | private Map<Long, PutRequest> putRequests = Maps.newTreeMap(); | 53 | private MatchingRequestContainer<Long, PutRequest> putRequests; |
57 | private Map<Long, GetRequest> getRequests = Maps.newTreeMap(); | 54 | private MatchingRequestContainer<Long, GetRequest> getRequests; |
58 | 55 | private SequentialRequestContainer<MonitorRequest> monitorRequests; | |
59 | /** | ||
60 | * monitor requests are kind of special, responses from the dht don't include the UID! | ||
61 | */ | ||
62 | private List<MonitorRequest> monitorRequests = Lists.newLinkedList(); | ||
63 | |||
64 | |||
65 | /** | ||
66 | * Create a connection with the DHT service. | ||
67 | * | ||
68 | * @param cfg the configuration to use | ||
69 | */ | ||
70 | public DistributedHashTable(Configuration cfg) { | ||
71 | client = new Client("dht", cfg); | ||
72 | requestQueue = new RequestQueue(client, new DHTMessageReceiver()); | ||
73 | } | ||
74 | 56 | ||
75 | private class PutRequest extends Request { | 57 | private class PutRequest extends RequestContainer.Request { |
76 | public byte[] data; | 58 | public byte[] data; |
77 | public HashCode key; | 59 | public HashCode key; |
78 | public int replicationLevel; | 60 | public int replicationLevel; |
@@ -83,11 +65,10 @@ public class DistributedHashTable { | |||
83 | 65 | ||
84 | public PutRequest() { | 66 | public PutRequest() { |
85 | this.uid = nextUID++; | 67 | this.uid = nextUID++; |
86 | putRequests.put(uid, this); | ||
87 | } | 68 | } |
88 | 69 | ||
89 | @Override | 70 | @Override |
90 | public void transmit(Connection.MessageSink sink) { | 71 | public Envelope assembleRequest() { |
91 | final ClientPutMessage cpm = new ClientPutMessage(); | 72 | final ClientPutMessage cpm = new ClientPutMessage(); |
92 | cpm.data = data; | 73 | cpm.data = data; |
93 | cpm.hash = key; | 74 | cpm.hash = key; |
@@ -95,16 +76,15 @@ public class DistributedHashTable { | |||
95 | cpm.expiration = expiration.asMessage(); | 76 | cpm.expiration = expiration.asMessage(); |
96 | cpm.type = type; | 77 | cpm.type = type; |
97 | cpm.uid = uid; | 78 | cpm.uid = uid; |
98 | sink.send(cpm); | 79 | return new Envelope(cpm); |
99 | } | 80 | } |
100 | 81 | ||
101 | public boolean onDestroy() { | 82 | public void cancel() { |
102 | return true; // keep! | ||
103 | } | 83 | } |
104 | } | 84 | } |
105 | 85 | ||
106 | 86 | ||
107 | private class GetRequest extends Request { | 87 | private class GetRequest extends RequestContainer.Request { |
108 | public long uid; | 88 | public long uid; |
109 | public HashCode key; | 89 | public HashCode key; |
110 | public ResultCallback cb; | 90 | public ResultCallback cb; |
@@ -114,23 +94,24 @@ public class DistributedHashTable { | |||
114 | 94 | ||
115 | public GetRequest() { | 95 | public GetRequest() { |
116 | uid = DistributedHashTable.this.nextUID++; | 96 | uid = DistributedHashTable.this.nextUID++; |
117 | getRequests.put(uid, this); | ||
118 | } | 97 | } |
119 | 98 | ||
120 | @Override | 99 | @Override |
121 | public void transmit(Connection.MessageSink sink) { | 100 | public Envelope assembleRequest() { |
122 | ClientGetMessage gm = new ClientGetMessage(); | 101 | ClientGetMessage gm = new ClientGetMessage(); |
123 | gm.desiredReplicationLevel = replication; | 102 | gm.desiredReplicationLevel = replication; |
124 | gm.type = type; | 103 | gm.type = type; |
125 | gm.xquery = xquery == null ? new byte[0] : xquery; | 104 | gm.xquery = xquery == null ? new byte[0] : xquery; |
126 | gm.key = key; | 105 | gm.key = key; |
127 | gm.uniqueId = uid; | 106 | gm.uniqueId = uid; |
107 | return new Envelope(gm); | ||
108 | } | ||
109 | public void cancel() { | ||
128 | 110 | ||
129 | sink.send(gm); | ||
130 | } | 111 | } |
131 | } | 112 | } |
132 | 113 | ||
133 | private class MonitorRequest extends Request { | 114 | private class MonitorRequest extends RequestContainer.Request { |
134 | public int blockType; | 115 | public int blockType; |
135 | public HashCode key; | 116 | public HashCode key; |
136 | public MonitorGetHandler getHandler; | 117 | public MonitorGetHandler getHandler; |
@@ -138,16 +119,7 @@ public class DistributedHashTable { | |||
138 | public MonitorPutHandler putHandler; | 119 | public MonitorPutHandler putHandler; |
139 | 120 | ||
140 | @Override | 121 | @Override |
141 | public AbsoluteTime getDeadline() { | 122 | public Envelope assembleRequest() { |
142 | return AbsoluteTime.FOREVER; | ||
143 | } | ||
144 | |||
145 | public MonitorRequest() { | ||
146 | monitorRequests.add(this); | ||
147 | } | ||
148 | |||
149 | @Override | ||
150 | public void transmit(Connection.MessageSink sink) { | ||
151 | MonitorStartStop mss = new MonitorStartStop(); | 123 | MonitorStartStop mss = new MonitorStartStop(); |
152 | if (key != null) { | 124 | if (key != null) { |
153 | mss.filter_key = 1; | 125 | mss.filter_key = 1; |
@@ -165,25 +137,24 @@ public class DistributedHashTable { | |||
165 | mss.put = 1; | 137 | mss.put = 1; |
166 | } | 138 | } |
167 | mss.type = blockType; | 139 | mss.type = blockType; |
168 | 140 | return new Envelope(mss); | |
169 | sink.send(mss); | ||
170 | } | 141 | } |
171 | 142 | ||
172 | @Override | 143 | public void cancel() { |
173 | public void onCancel(boolean alreadyTransmitted) { | 144 | // todo: use priority requests |
174 | if (alreadyTransmitted) { | 145 | MonitorRequest cancelRequest = new MonitorRequest(); |
175 | MonitorRequest cancelRequest = new MonitorRequest(); | 146 | cancelRequest.getHandler = null; |
176 | cancelRequest.getHandler = null; | 147 | cancelRequest.getResponseHandler = null; |
177 | cancelRequest.getResponseHandler = null; | 148 | cancelRequest.putHandler = null; |
178 | cancelRequest.putHandler = null; | 149 | monitorRequests.addRequest(cancelRequest); |
179 | requestQueue.add(new MonitorRequest()); | 150 | |
180 | } | 151 | monitorRequests.addRequest(cancelRequest); |
181 | } | 152 | } |
182 | } | 153 | } |
183 | 154 | ||
184 | public class DHTMessageReceiver extends RunaboutMessageReceiver { | 155 | private class DHTMessageReceiver extends RunaboutMessageReceiver { |
185 | public void visit(ClientPutConfirmationMessage pcm) { | 156 | public void visit(ClientPutConfirmationMessage pcm) { |
186 | PutRequest thePutRequest = putRequests.get(pcm.uid); | 157 | PutRequest thePutRequest = putRequests.getRequest(pcm.uid); |
187 | if (thePutRequest == null) { | 158 | if (thePutRequest == null) { |
188 | logger.warn("request UID not found"); | 159 | logger.warn("request UID not found"); |
189 | return; | 160 | return; |
@@ -194,7 +165,7 @@ public class DistributedHashTable { | |||
194 | } | 165 | } |
195 | 166 | ||
196 | public void visit(ClientResultMessage rm) { | 167 | public void visit(ClientResultMessage rm) { |
197 | GetRequest theGetRequest = getRequests.get(rm.uid); | 168 | GetRequest theGetRequest = getRequests.getRequest(rm.uid); |
198 | if (theGetRequest == null) { | 169 | if (theGetRequest == null) { |
199 | logger.warn("request UID not found"); | 170 | logger.warn("request UID not found"); |
200 | return; | 171 | return; |
@@ -205,7 +176,7 @@ public class DistributedHashTable { | |||
205 | } | 176 | } |
206 | 177 | ||
207 | public void visit(MonitorGetMessage monitorGetMessage) { | 178 | public void visit(MonitorGetMessage monitorGetMessage) { |
208 | for (MonitorRequest monitorRequest : monitorRequests) { | 179 | for (MonitorRequest monitorRequest : monitorRequests.iter()) { |
209 | boolean type_ok = (monitorGetMessage.type == BlockType.ANY.val) | 180 | boolean type_ok = (monitorGetMessage.type == BlockType.ANY.val) |
210 | || (monitorGetMessage.type == monitorRequest.blockType); | 181 | || (monitorGetMessage.type == monitorRequest.blockType); |
211 | boolean key_ok = monitorGetMessage.key.isAllZero() | 182 | boolean key_ok = monitorGetMessage.key.isAllZero() |
@@ -220,7 +191,7 @@ public class DistributedHashTable { | |||
220 | } | 191 | } |
221 | 192 | ||
222 | public void visit(MonitorGetRespMessage monitorGetRespMessage) { | 193 | public void visit(MonitorGetRespMessage monitorGetRespMessage) { |
223 | for (MonitorRequest monitorRequest : monitorRequests) { | 194 | for (MonitorRequest monitorRequest : monitorRequests.iter()) { |
224 | boolean type_ok = (monitorGetRespMessage.type == BlockType.ANY.val) | 195 | boolean type_ok = (monitorGetRespMessage.type == BlockType.ANY.val) |
225 | || (monitorGetRespMessage.type == monitorRequest.blockType); | 196 | || (monitorGetRespMessage.type == monitorRequest.blockType); |
226 | boolean key_ok = monitorGetRespMessage.key.isAllZero() | 197 | boolean key_ok = monitorGetRespMessage.key.isAllZero() |
@@ -240,7 +211,7 @@ public class DistributedHashTable { | |||
240 | } | 211 | } |
241 | 212 | ||
242 | public void visit(MonitorPutMessage monitorPutMessage) { | 213 | public void visit(MonitorPutMessage monitorPutMessage) { |
243 | for (MonitorRequest monitorRequest : monitorRequests) { | 214 | for (MonitorRequest monitorRequest : monitorRequests.iter()) { |
244 | boolean type_ok = (monitorPutMessage.type == BlockType.ANY.val) | 215 | boolean type_ok = (monitorPutMessage.type == BlockType.ANY.val) |
245 | || (monitorPutMessage.type == monitorRequest.blockType); | 216 | || (monitorPutMessage.type == monitorRequest.blockType); |
246 | boolean key_ok = monitorPutMessage.key.isAllZero() | 217 | boolean key_ok = monitorPutMessage.key.isAllZero() |
@@ -256,10 +227,23 @@ public class DistributedHashTable { | |||
256 | 227 | ||
257 | @Override | 228 | @Override |
258 | public void handleError() { | 229 | public void handleError() { |
259 | requestQueue.reconnect(); | ||
260 | } | 230 | } |
261 | } | 231 | } |
262 | 232 | ||
233 | |||
234 | /** | ||
235 | * Create a connection with the DHT service. | ||
236 | * | ||
237 | * @param cfg the configuration to use | ||
238 | */ | ||
239 | public DistributedHashTable(Configuration cfg) { | ||
240 | client = new Client("dht", cfg); | ||
241 | client.installReceiver(new DHTMessageReceiver()); | ||
242 | putRequests = new MatchingRequestContainer<Long, PutRequest>(client); | ||
243 | getRequests = new MatchingRequestContainer<Long, GetRequest>(client); | ||
244 | monitorRequests = new SequentialRequestContainer<MonitorRequest>(client); | ||
245 | } | ||
246 | |||
263 | /** | 247 | /** |
264 | * Put data into the dht. | 248 | * Put data into the dht. |
265 | * | 249 | * |
@@ -279,12 +263,11 @@ public class DistributedHashTable { | |||
279 | pr.key = key; | 263 | pr.key = key; |
280 | pr.data = data; | 264 | pr.data = data; |
281 | pr.replicationLevel = replicationLevel; | 265 | pr.replicationLevel = replicationLevel; |
282 | pr.setDeadline(timeout.toAbsolute()); | ||
283 | pr.expiration = expiration; | 266 | pr.expiration = expiration; |
284 | pr.type = type; | 267 | pr.type = type; |
285 | pr.cont = cont; | 268 | pr.cont = cont; |
286 | 269 | ||
287 | requestQueue.add(pr); | 270 | putRequests.addRequest(pr.uid, pr); |
288 | } | 271 | } |
289 | 272 | ||
290 | 273 | ||
@@ -310,9 +293,8 @@ public class DistributedHashTable { | |||
310 | getRequest.type = type; | 293 | getRequest.type = type; |
311 | getRequest.replication = type; | 294 | getRequest.replication = type; |
312 | getRequest.xquery = xquery; | 295 | getRequest.xquery = xquery; |
313 | getRequest.setDeadline(timeout.toAbsolute()); | ||
314 | 296 | ||
315 | return requestQueue.add(getRequest); | 297 | return getRequests.addRequest(getRequest.uid, getRequest); |
316 | } | 298 | } |
317 | 299 | ||
318 | public Cancelable startMonitor(int blockType, HashCode key, MonitorGetHandler getHandler, | 300 | public Cancelable startMonitor(int blockType, HashCode key, MonitorGetHandler getHandler, |
@@ -325,8 +307,7 @@ public class DistributedHashTable { | |||
325 | monitorRequest.getResponseHandler = getResponseHandler; | 307 | monitorRequest.getResponseHandler = getResponseHandler; |
326 | monitorRequest.putHandler = putHandler; | 308 | monitorRequest.putHandler = putHandler; |
327 | 309 | ||
328 | return requestQueue.add(monitorRequest); | 310 | return monitorRequests.addRequest(monitorRequest); |
329 | |||
330 | } | 311 | } |
331 | 312 | ||
332 | 313 | ||
@@ -335,7 +316,6 @@ public class DistributedHashTable { | |||
335 | */ | 316 | */ |
336 | public void destroy() { | 317 | public void destroy() { |
337 | // there's nothing to sync, just destroy! | 318 | // there's nothing to sync, just destroy! |
338 | requestQueue.destroy(); | ||
339 | client.disconnect(); | 319 | client.disconnect(); |
340 | } | 320 | } |
341 | 321 | ||
diff --git a/src/org/gnunet/dht/MonitorGetHandler.java b/src/main/java/org/gnunet/dht/MonitorGetHandler.java index c7dad7c..c7dad7c 100644 --- a/src/org/gnunet/dht/MonitorGetHandler.java +++ b/src/main/java/org/gnunet/dht/MonitorGetHandler.java | |||
diff --git a/src/org/gnunet/dht/MonitorGetMessage.java b/src/main/java/org/gnunet/dht/MonitorGetMessage.java index e96ec96..e96ec96 100644 --- a/src/org/gnunet/dht/MonitorGetMessage.java +++ b/src/main/java/org/gnunet/dht/MonitorGetMessage.java | |||
diff --git a/src/org/gnunet/dht/MonitorGetRespMessage.java b/src/main/java/org/gnunet/dht/MonitorGetRespMessage.java index 3bf145b..3bf145b 100644 --- a/src/org/gnunet/dht/MonitorGetRespMessage.java +++ b/src/main/java/org/gnunet/dht/MonitorGetRespMessage.java | |||
diff --git a/src/org/gnunet/dht/MonitorGetResponseHandler.java b/src/main/java/org/gnunet/dht/MonitorGetResponseHandler.java index ff03cce..ff03cce 100644 --- a/src/org/gnunet/dht/MonitorGetResponseHandler.java +++ b/src/main/java/org/gnunet/dht/MonitorGetResponseHandler.java | |||
diff --git a/src/org/gnunet/dht/MonitorPutHandler.java b/src/main/java/org/gnunet/dht/MonitorPutHandler.java index 0abb79b..0abb79b 100644 --- a/src/org/gnunet/dht/MonitorPutHandler.java +++ b/src/main/java/org/gnunet/dht/MonitorPutHandler.java | |||
diff --git a/src/org/gnunet/dht/MonitorPutMessage.java b/src/main/java/org/gnunet/dht/MonitorPutMessage.java index 103c05b..103c05b 100644 --- a/src/org/gnunet/dht/MonitorPutMessage.java +++ b/src/main/java/org/gnunet/dht/MonitorPutMessage.java | |||
diff --git a/src/org/gnunet/dht/MonitorStartStop.java b/src/main/java/org/gnunet/dht/MonitorStartStop.java index b3f3268..b3f3268 100644 --- a/src/org/gnunet/dht/MonitorStartStop.java +++ b/src/main/java/org/gnunet/dht/MonitorStartStop.java | |||
diff --git a/src/org/gnunet/dht/ResultCallback.java b/src/main/java/org/gnunet/dht/ResultCallback.java index fa5c7ef..fa5c7ef 100644 --- a/src/org/gnunet/dht/ResultCallback.java +++ b/src/main/java/org/gnunet/dht/ResultCallback.java | |||
diff --git a/src/org/gnunet/dht/RouteOption.java b/src/main/java/org/gnunet/dht/RouteOption.java index b74b528..b74b528 100644 --- a/src/org/gnunet/dht/RouteOption.java +++ b/src/main/java/org/gnunet/dht/RouteOption.java | |||
diff --git a/src/org/gnunet/dht/package-info.java b/src/main/java/org/gnunet/dht/package-info.java index 3d8c6ed..3d8c6ed 100644 --- a/src/org/gnunet/dht/package-info.java +++ b/src/main/java/org/gnunet/dht/package-info.java | |||
diff --git a/src/org/gnunet/hello/HelloMessage.java b/src/main/java/org/gnunet/hello/HelloMessage.java index 0d90912..0d90912 100644 --- a/src/org/gnunet/hello/HelloMessage.java +++ b/src/main/java/org/gnunet/hello/HelloMessage.java | |||
diff --git a/src/org/gnunet/hello/package-info.java b/src/main/java/org/gnunet/hello/package-info.java index 78a5193..78a5193 100644 --- a/src/org/gnunet/hello/package-info.java +++ b/src/main/java/org/gnunet/hello/package-info.java | |||
diff --git a/src/org/gnunet/mesh/ClientConnectMessage.java b/src/main/java/org/gnunet/mesh/ClientConnectMessage.java index 1a56ebb..1a56ebb 100644 --- a/src/org/gnunet/mesh/ClientConnectMessage.java +++ b/src/main/java/org/gnunet/mesh/ClientConnectMessage.java | |||
diff --git a/src/org/gnunet/mesh/ConnectHandler.java b/src/main/java/org/gnunet/mesh/ConnectHandler.java index 021e8f0..021e8f0 100644 --- a/src/org/gnunet/mesh/ConnectHandler.java +++ b/src/main/java/org/gnunet/mesh/ConnectHandler.java | |||
diff --git a/src/org/gnunet/mesh/DataMessage.java b/src/main/java/org/gnunet/mesh/DataMessage.java index 92546c8..92546c8 100644 --- a/src/org/gnunet/mesh/DataMessage.java +++ b/src/main/java/org/gnunet/mesh/DataMessage.java | |||
diff --git a/src/org/gnunet/mesh/DisconnectHandler.java b/src/main/java/org/gnunet/mesh/DisconnectHandler.java index 8fd6428..8fd6428 100644 --- a/src/org/gnunet/mesh/DisconnectHandler.java +++ b/src/main/java/org/gnunet/mesh/DisconnectHandler.java | |||
diff --git a/src/org/gnunet/mesh/InboundTunnelHandler.java b/src/main/java/org/gnunet/mesh/InboundTunnelHandler.java index ebcf225..ebcf225 100644 --- a/src/org/gnunet/mesh/InboundTunnelHandler.java +++ b/src/main/java/org/gnunet/mesh/InboundTunnelHandler.java | |||
diff --git a/src/org/gnunet/mesh/LocalAckMessage.java b/src/main/java/org/gnunet/mesh/LocalAckMessage.java index 6a09411..6a09411 100644 --- a/src/org/gnunet/mesh/LocalAckMessage.java +++ b/src/main/java/org/gnunet/mesh/LocalAckMessage.java | |||
diff --git a/src/org/gnunet/mesh/Mesh.java b/src/main/java/org/gnunet/mesh/Mesh.java index b272b0b..a6bf602 100644 --- a/src/org/gnunet/mesh/Mesh.java +++ b/src/main/java/org/gnunet/mesh/Mesh.java | |||
@@ -21,11 +21,9 @@ | |||
21 | package org.gnunet.mesh; | 21 | package org.gnunet.mesh; |
22 | 22 | ||
23 | import org.gnunet.construct.Construct; | 23 | import org.gnunet.construct.Construct; |
24 | import org.gnunet.mq.ClientMessageQueue; | ||
25 | import org.gnunet.mq.Envelope; | 24 | import org.gnunet.mq.Envelope; |
26 | import org.gnunet.mq.MessageQueue; | 25 | import org.gnunet.mq.MessageQueue; |
27 | import org.gnunet.requests.Request; | 26 | import org.gnunet.mq.NotifySentHandler; |
28 | import org.gnunet.requests.RequestQueue; | ||
29 | import org.gnunet.util.*; | 27 | import org.gnunet.util.*; |
30 | import org.slf4j.Logger; | 28 | import org.slf4j.Logger; |
31 | import org.slf4j.LoggerFactory; | 29 | import org.slf4j.LoggerFactory; |
@@ -75,11 +73,6 @@ public class Mesh { | |||
75 | private final Client client; | 73 | private final Client client; |
76 | 74 | ||
77 | /** | 75 | /** |
78 | * Message queue for the client. | ||
79 | */ | ||
80 | private final ClientMessageQueue client_mq; | ||
81 | |||
82 | /** | ||
83 | * Called whenever a tunnel was destroyed. | 76 | * Called whenever a tunnel was destroyed. |
84 | */ | 77 | */ |
85 | private TunnelEndHandler tunnelEndHandler; | 78 | private TunnelEndHandler tunnelEndHandler; |
@@ -103,13 +96,13 @@ public class Mesh { | |||
103 | /** | 96 | /** |
104 | * Mapping from the tunnel's ID to the tunnel object. | 97 | * Mapping from the tunnel's ID to the tunnel object. |
105 | */ | 98 | */ |
106 | private Map<Integer,Tunnel> tunnelMap = new HashMap<>(); | 99 | private Map<Integer,Tunnel> tunnelMap = new HashMap<Integer,Tunnel>(); |
107 | 100 | ||
108 | /** | 101 | /** |
109 | * Counter for generating fresh tunnel ID's | 102 | * Counter for generating fresh tunnel ID's |
110 | * when creating new tunnels. | 103 | * when creating new tunnels. |
111 | */ | 104 | */ |
112 | int next_tid = 1; | 105 | private int next_tid = 1; |
113 | 106 | ||
114 | /** | 107 | /** |
115 | * A tunnel to a remote peer. | 108 | * A tunnel to a remote peer. |
@@ -122,7 +115,12 @@ public class Mesh { | |||
122 | public final int port; | 115 | public final int port; |
123 | protected int tunnelId; | 116 | protected int tunnelId; |
124 | private boolean receive_done_expected = false; | 117 | private boolean receive_done_expected = false; |
125 | int ack_count = 0; | 118 | int ack_count = 1; |
119 | |||
120 | /** | ||
121 | * Canceler for the currently submitted envelope. | ||
122 | */ | ||
123 | public Cancelable envelopeCanceler; | ||
126 | 124 | ||
127 | /** | 125 | /** |
128 | * Create a new tunnel (we're initiator and will be allowed to add/remove peers | 126 | * Create a new tunnel (we're initiator and will be allowed to add/remove peers |
@@ -142,7 +140,7 @@ public class Mesh { | |||
142 | tcm.opt = opt; | 140 | tcm.opt = opt; |
143 | tcm.port = port; | 141 | tcm.port = port; |
144 | tcm.tunnel_id = tunnelId; | 142 | tcm.tunnel_id = tunnelId; |
145 | client_mq.send(tcm); | 143 | client.send(tcm); |
146 | } | 144 | } |
147 | 145 | ||
148 | /** | 146 | /** |
@@ -155,7 +153,7 @@ public class Mesh { | |||
155 | * @param nobuffer | 153 | * @param nobuffer |
156 | * @param reliable | 154 | * @param reliable |
157 | */ | 155 | */ |
158 | private Tunnel(PeerIdentity peer, int tunnelId, int port, boolean nobuffer, boolean reliable) { | 156 | public Tunnel(PeerIdentity peer, int tunnelId, int port, boolean nobuffer, boolean reliable) { |
159 | int my_opt = 0; | 157 | int my_opt = 0; |
160 | if (reliable) | 158 | if (reliable) |
161 | my_opt |= OPTION_RELIABLE; | 159 | my_opt |= OPTION_RELIABLE; |
@@ -175,27 +173,42 @@ public class Mesh { | |||
175 | throw new AssertionError("unexpected call to receiveDone"); | 173 | throw new AssertionError("unexpected call to receiveDone"); |
176 | LocalAckMessage am = new LocalAckMessage(); | 174 | LocalAckMessage am = new LocalAckMessage(); |
177 | am.tid = tunnelId; | 175 | am.tid = tunnelId; |
178 | client_mq.send(am); | 176 | client.send(am); |
179 | receive_done_expected = false; | 177 | receive_done_expected = false; |
180 | } | 178 | } |
181 | 179 | ||
182 | public void destroy() { | 180 | public void destroy() { |
183 | TunnelDestroyMessage m = new TunnelDestroyMessage(); | 181 | TunnelDestroyMessage m = new TunnelDestroyMessage(); |
184 | m.tunnel_id = tunnelId; | 182 | m.tunnel_id = tunnelId; |
185 | client_mq.send(m); | 183 | client.send(m); |
186 | } | 184 | } |
187 | 185 | ||
188 | @Override | 186 | @Override |
189 | protected void sendImmediate(Envelope ev) { | 187 | protected void submit(Envelope ev) { |
190 | if (ack_count <= 0) | 188 | if (ack_count <= 0) |
191 | throw new AssertionError(); | 189 | throw new AssertionError(); |
192 | DataMessage m = new DataMessage(); | 190 | DataMessage m = new DataMessage(); |
193 | m.payload = Construct.toBinary(GnunetMessage.fromBody(ev.message)); | 191 | m.payload = Construct.toBinary(GnunetMessage.fromBody(ev.message)); |
194 | Envelope mesh_ev = new Envelope(m); | 192 | Envelope mesh_ev = new Envelope(m); |
195 | client_mq.send(mesh_ev); | 193 | mesh_ev.notifySent(new NotifySentHandler() { |
194 | @Override | ||
195 | public void onSent() { | ||
196 | envelopeCanceler = null; | ||
197 | } | ||
198 | }); | ||
199 | client.send(mesh_ev); | ||
200 | envelopeCanceler = mesh_ev; | ||
196 | ack_count -= 1; | 201 | ack_count -= 1; |
197 | } | 202 | } |
198 | 203 | ||
204 | @Override | ||
205 | protected void retract() { | ||
206 | if (envelopeCanceler == null) | ||
207 | throw new AssertionError(); | ||
208 | envelopeCanceler.cancel(); | ||
209 | envelopeCanceler = null; | ||
210 | } | ||
211 | |||
199 | public T getContext() { | 212 | public T getContext() { |
200 | return context; | 213 | return context; |
201 | } | 214 | } |
@@ -253,7 +266,7 @@ public class Mesh { | |||
253 | client.reconnect(); | 266 | client.reconnect(); |
254 | ClientConnectMessage ccm = new ClientConnectMessage(); | 267 | ClientConnectMessage ccm = new ClientConnectMessage(); |
255 | ccm.apps_list = ports; | 268 | ccm.apps_list = ports; |
256 | client_mq.send(ccm); | 269 | client.send(ccm); |
257 | } | 270 | } |
258 | } | 271 | } |
259 | 272 | ||
@@ -274,10 +287,14 @@ public class Mesh { | |||
274 | this.inboundTunnelHandler = inboundTunnelHandler; | 287 | this.inboundTunnelHandler = inboundTunnelHandler; |
275 | 288 | ||
276 | client = new Client("mesh", cfg); | 289 | client = new Client("mesh", cfg); |
277 | client_mq = new ClientMessageQueue(client, new MeshMessageReceiver()); | 290 | client.installReceiver(new MeshMessageReceiver()); |
278 | ClientConnectMessage ccm = new ClientConnectMessage(); | 291 | ClientConnectMessage ccm = new ClientConnectMessage(); |
279 | ccm.apps_list = ports; | 292 | ccm.apps_list = ports; |
280 | client_mq.send(ccm); | 293 | client.send(ccm); |
294 | } | ||
295 | |||
296 | public <T> Tunnel<T> createTunnel(PeerIdentity peer, int port, boolean nobuffer, boolean reliable, T initialContext) { | ||
297 | return new Tunnel<T>(peer, port, nobuffer, reliable, initialContext); | ||
281 | } | 298 | } |
282 | 299 | ||
283 | /** | 300 | /** |
@@ -286,8 +303,7 @@ public class Mesh { | |||
286 | * All tunnel disconnect callbacks will be called on any still connected peers, notifying | 303 | * All tunnel disconnect callbacks will be called on any still connected peers, notifying |
287 | * about their disconnection. | 304 | * about their disconnection. |
288 | */ | 305 | */ |
289 | public void disconnect() { | 306 | public void destroy() { |
290 | client_mq.destroy(); | ||
291 | client.disconnect(); | 307 | client.disconnect(); |
292 | } | 308 | } |
293 | } | 309 | } |
diff --git a/src/org/gnunet/mesh/MeshRunabout.java b/src/main/java/org/gnunet/mesh/MeshRunabout.java index ea4248c..ea4248c 100644 --- a/src/org/gnunet/mesh/MeshRunabout.java +++ b/src/main/java/org/gnunet/mesh/MeshRunabout.java | |||
diff --git a/src/org/gnunet/mesh/TunnelCreateMessage.java b/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java index eaa4d6c..eaa4d6c 100644 --- a/src/org/gnunet/mesh/TunnelCreateMessage.java +++ b/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java | |||
diff --git a/src/org/gnunet/mesh/TunnelDestroyMessage.java b/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java index bce60bb..bce60bb 100644 --- a/src/org/gnunet/mesh/TunnelDestroyMessage.java +++ b/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java | |||
diff --git a/src/org/gnunet/mesh/TunnelEndHandler.java b/src/main/java/org/gnunet/mesh/TunnelEndHandler.java index e56fdd4..e56fdd4 100644 --- a/src/org/gnunet/mesh/TunnelEndHandler.java +++ b/src/main/java/org/gnunet/mesh/TunnelEndHandler.java | |||
diff --git a/src/org/gnunet/mesh/TunnelNotificationMessage.java b/src/main/java/org/gnunet/mesh/TunnelNotificationMessage.java index 8846088..8846088 100644 --- a/src/org/gnunet/mesh/TunnelNotificationMessage.java +++ b/src/main/java/org/gnunet/mesh/TunnelNotificationMessage.java | |||
diff --git a/src/org/gnunet/mesh/package-info.java b/src/main/java/org/gnunet/mesh/package-info.java index fb5a8a4..fb5a8a4 100644 --- a/src/org/gnunet/mesh/package-info.java +++ b/src/main/java/org/gnunet/mesh/package-info.java | |||
diff --git a/src/org/gnunet/mq/Envelope.java b/src/main/java/org/gnunet/mq/Envelope.java index 09c0c2c..fa19225 100644 --- a/src/org/gnunet/mq/Envelope.java +++ b/src/main/java/org/gnunet/mq/Envelope.java | |||
@@ -1,11 +1,12 @@ | |||
1 | package org.gnunet.mq; | 1 | package org.gnunet.mq; |
2 | 2 | ||
3 | import org.gnunet.util.Cancelable; | ||
3 | import org.gnunet.util.GnunetMessage; | 4 | import org.gnunet.util.GnunetMessage; |
4 | 5 | ||
5 | /** | 6 | /** |
6 | * Container for a message to be sent by a message queue. | 7 | * Container for a message to be sent by a message queue. |
7 | */ | 8 | */ |
8 | public class Envelope { | 9 | public class Envelope implements Cancelable { |
9 | public final GnunetMessage.Body message; | 10 | public final GnunetMessage.Body message; |
10 | private MessageQueue parent_queue; | 11 | private MessageQueue parent_queue; |
11 | private NotifySentHandler notify_sent_handler; | 12 | private NotifySentHandler notify_sent_handler; |
@@ -24,7 +25,8 @@ public class Envelope { | |||
24 | } | 25 | } |
25 | 26 | ||
26 | public void cancel() { | 27 | public void cancel() { |
27 | // TODO | 28 | if (parent_queue == null) |
29 | throw new AssertionError("can not cancel an unqueued message"); | ||
28 | } | 30 | } |
29 | 31 | ||
30 | /* pkg-private */ void invokeSentNotification() { | 32 | /* pkg-private */ void invokeSentNotification() { |
diff --git a/src/main/java/org/gnunet/mq/MessageQueue.java b/src/main/java/org/gnunet/mq/MessageQueue.java new file mode 100644 index 0000000..4df3ae4 --- /dev/null +++ b/src/main/java/org/gnunet/mq/MessageQueue.java | |||
@@ -0,0 +1,84 @@ | |||
1 | package org.gnunet.mq; | ||
2 | |||
3 | |||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | |||
6 | import java.util.LinkedList; | ||
7 | |||
8 | /** | ||
9 | * General-purpose message queue | ||
10 | */ | ||
11 | public abstract class MessageQueue { | ||
12 | private LinkedList<Envelope> queued_envelopes = new LinkedList<Envelope>(); | ||
13 | private LinkedList<Envelope> prefered_queued_envelopes = new LinkedList<Envelope>(); | ||
14 | protected Envelope current_envelope; | ||
15 | |||
16 | protected abstract void submit(Envelope ev); | ||
17 | |||
18 | protected abstract void retract(); | ||
19 | |||
20 | public void send(GnunetMessage.Body body) { | ||
21 | send(new Envelope(body)); | ||
22 | } | ||
23 | |||
24 | public void sendPrefered(GnunetMessage.Body body) { | ||
25 | sendPrefered(new Envelope(body)); | ||
26 | } | ||
27 | |||
28 | private Envelope pollNextEnvelope() { | ||
29 | if (!prefered_queued_envelopes.isEmpty()) | ||
30 | return prefered_queued_envelopes.removeFirst(); | ||
31 | if (!queued_envelopes.isEmpty()) | ||
32 | return queued_envelopes.removeFirst(); | ||
33 | return null; | ||
34 | } | ||
35 | |||
36 | public void send(Envelope ev) { | ||
37 | if (null == current_envelope) { | ||
38 | current_envelope = ev; | ||
39 | submit(current_envelope); | ||
40 | } else { | ||
41 | queued_envelopes.addLast(ev); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | public void sendPrefered(Envelope ev) { | ||
46 | if (null == current_envelope) { | ||
47 | current_envelope = ev; | ||
48 | submit(current_envelope); | ||
49 | } else { | ||
50 | prefered_queued_envelopes.addLast(ev); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | protected void reportMessageSent() { | ||
55 | if (null == current_envelope) | ||
56 | throw new AssertionError(); | ||
57 | current_envelope.invokeSentNotification(); | ||
58 | next(); | ||
59 | } | ||
60 | |||
61 | private void next() { | ||
62 | current_envelope = pollNextEnvelope(); | ||
63 | if (current_envelope == null) | ||
64 | return; | ||
65 | submit(current_envelope); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Cancel sending an envelope. The envelope must be queued in this message queue. | ||
70 | * | ||
71 | * @param ev the envelope to cancel | ||
72 | */ | ||
73 | /* pkg-private */ void cancelEnvelope(Envelope ev) { | ||
74 | if (null == current_envelope) | ||
75 | throw new AssertionError(); | ||
76 | if (ev == current_envelope) { | ||
77 | retract(); | ||
78 | next(); | ||
79 | } else { | ||
80 | queued_envelopes.remove(ev); | ||
81 | prefered_queued_envelopes.remove(ev); | ||
82 | } | ||
83 | } | ||
84 | } | ||
diff --git a/src/org/gnunet/mq/NotifySentHandler.java b/src/main/java/org/gnunet/mq/NotifySentHandler.java index 7ec13b2..7ec13b2 100644 --- a/src/org/gnunet/mq/NotifySentHandler.java +++ b/src/main/java/org/gnunet/mq/NotifySentHandler.java | |||
diff --git a/src/org/gnunet/nse/NetworkSizeEstimation.java b/src/main/java/org/gnunet/nse/NetworkSizeEstimation.java index 366ab80..5fc7453 100644 --- a/src/org/gnunet/nse/NetworkSizeEstimation.java +++ b/src/main/java/org/gnunet/nse/NetworkSizeEstimation.java | |||
@@ -21,7 +21,6 @@ | |||
21 | package org.gnunet.nse; | 21 | package org.gnunet.nse; |
22 | 22 | ||
23 | 23 | ||
24 | import org.gnunet.construct.ProtocolViolationException; | ||
25 | import org.gnunet.util.*; | 24 | import org.gnunet.util.*; |
26 | import org.gnunet.util.getopt.Argument; | 25 | import org.gnunet.util.getopt.Argument; |
27 | import org.gnunet.util.getopt.ArgumentAction; | 26 | import org.gnunet.util.getopt.ArgumentAction; |
@@ -53,7 +52,7 @@ public class NetworkSizeEstimation { | |||
53 | } | 52 | } |
54 | 53 | ||
55 | if (!disconnected) { | 54 | if (!disconnected) { |
56 | client.receive(RelativeTime.FOREVER, this); | 55 | client.receiveOne(RelativeTime.FOREVER, this); |
57 | } | 56 | } |
58 | } | 57 | } |
59 | 58 | ||
@@ -70,7 +69,7 @@ public class NetworkSizeEstimation { | |||
70 | public void transmit(Connection.MessageSink sink) { | 69 | public void transmit(Connection.MessageSink sink) { |
71 | StartMessage m = new StartMessage(); | 70 | StartMessage m = new StartMessage(); |
72 | sink.send(m); | 71 | sink.send(m); |
73 | client.receive(RelativeTime.FOREVER, new NSE_Receiver()); | 72 | client.receiveOne(RelativeTime.FOREVER, new NSE_Receiver()); |
74 | } | 73 | } |
75 | 74 | ||
76 | @Override | 75 | @Override |
@@ -139,7 +138,6 @@ public class NetworkSizeEstimation { | |||
139 | */ | 138 | */ |
140 | public void disconnect() { | 139 | public void disconnect() { |
141 | disconnected = true; | 140 | disconnected = true; |
142 | |||
143 | } | 141 | } |
144 | 142 | ||
145 | public static void main(String[] args) { | 143 | public static void main(String[] args) { |
diff --git a/src/org/gnunet/nse/StartMessage.java b/src/main/java/org/gnunet/nse/StartMessage.java index 9f5b79e..9f5b79e 100644 --- a/src/org/gnunet/nse/StartMessage.java +++ b/src/main/java/org/gnunet/nse/StartMessage.java | |||
diff --git a/src/org/gnunet/nse/UpdateMessage.java b/src/main/java/org/gnunet/nse/UpdateMessage.java index 5c64a54..5c64a54 100644 --- a/src/org/gnunet/nse/UpdateMessage.java +++ b/src/main/java/org/gnunet/nse/UpdateMessage.java | |||
diff --git a/src/org/gnunet/nse/package-info.java b/src/main/java/org/gnunet/nse/package-info.java index 3bbc064..3bbc064 100644 --- a/src/org/gnunet/nse/package-info.java +++ b/src/main/java/org/gnunet/nse/package-info.java | |||
diff --git a/src/org/gnunet/peerinfo/InfoEnd.java b/src/main/java/org/gnunet/peerinfo/InfoEnd.java index b59e194..b59e194 100644 --- a/src/org/gnunet/peerinfo/InfoEnd.java +++ b/src/main/java/org/gnunet/peerinfo/InfoEnd.java | |||
diff --git a/src/org/gnunet/peerinfo/InfoMessage.java b/src/main/java/org/gnunet/peerinfo/InfoMessage.java index d8d7daa..d8d7daa 100644 --- a/src/org/gnunet/peerinfo/InfoMessage.java +++ b/src/main/java/org/gnunet/peerinfo/InfoMessage.java | |||
diff --git a/src/org/gnunet/peerinfo/ListAllPeersMessage.java b/src/main/java/org/gnunet/peerinfo/ListAllPeersMessage.java index e384c93..e384c93 100644 --- a/src/org/gnunet/peerinfo/ListAllPeersMessage.java +++ b/src/main/java/org/gnunet/peerinfo/ListAllPeersMessage.java | |||
diff --git a/src/org/gnunet/peerinfo/ListPeerMessage.java b/src/main/java/org/gnunet/peerinfo/ListPeerMessage.java index 56e6b5d..56e6b5d 100644 --- a/src/org/gnunet/peerinfo/ListPeerMessage.java +++ b/src/main/java/org/gnunet/peerinfo/ListPeerMessage.java | |||
diff --git a/src/main/java/org/gnunet/peerinfo/PeerInfo.java b/src/main/java/org/gnunet/peerinfo/PeerInfo.java new file mode 100644 index 0000000..46d35bf --- /dev/null +++ b/src/main/java/org/gnunet/peerinfo/PeerInfo.java | |||
@@ -0,0 +1,159 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.peerinfo; | ||
22 | |||
23 | import org.gnunet.hello.HelloMessage; | ||
24 | import org.gnunet.mq.Envelope; | ||
25 | import org.gnunet.mq.MessageQueue; | ||
26 | import org.gnunet.requests.RequestContainer; | ||
27 | import org.gnunet.requests.SequentialRequestContainer; | ||
28 | import org.gnunet.util.*; | ||
29 | import org.slf4j.Logger; | ||
30 | import org.slf4j.LoggerFactory; | ||
31 | |||
32 | /** | ||
33 | * Interface to the service that maintains all known hosts. | ||
34 | * | ||
35 | * @author Florian Dold | ||
36 | */ | ||
37 | public class PeerInfo { | ||
38 | private static final Logger logger = LoggerFactory | ||
39 | .getLogger(AbsoluteTime.class); | ||
40 | |||
41 | /** | ||
42 | * Client that connects to the peerinfo service. | ||
43 | */ | ||
44 | private final Client client; | ||
45 | |||
46 | /** | ||
47 | * All currently active iterate requests. | ||
48 | */ | ||
49 | private SequentialRequestContainer<PeerIterateRequest> iterate_requests; | ||
50 | |||
51 | private class PeerIterateRequest extends RequestContainer.Request { | ||
52 | public PeerIdentity peer; | ||
53 | public PeerProcessor peerProcessor; | ||
54 | public boolean friend_only; | ||
55 | public boolean canceled; | ||
56 | |||
57 | public PeerIterateRequest(PeerIdentity peer, boolean friend_only, PeerProcessor peerProcessor) { | ||
58 | this.peer = peer; | ||
59 | this.peerProcessor = peerProcessor; | ||
60 | this.friend_only = friend_only; | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public Envelope assembleRequest() { | ||
65 | if (peer == null) { | ||
66 | ListAllPeersMessage m = new ListAllPeersMessage(); | ||
67 | m.include_friend_only = friend_only ? 1 : 0; | ||
68 | return new Envelope(m); | ||
69 | } else { | ||
70 | ListPeerMessage m = new ListPeerMessage(); | ||
71 | m.peer = peer; | ||
72 | return new Envelope(m); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | public void cancel() { | ||
77 | canceled = true; | ||
78 | } | ||
79 | } | ||
80 | |||
81 | private class PeerInfoMessageReceiver extends RunaboutMessageReceiver { | ||
82 | public void visit(InfoEnd infoEnd) { | ||
83 | PeerIterateRequest r = iterate_requests.getRequest(); | ||
84 | if (!r.canceled) | ||
85 | r.peerProcessor.onEnd(); | ||
86 | iterate_requests.next(); | ||
87 | } | ||
88 | public void visit(InfoMessage infoMessage) { | ||
89 | PeerIterateRequest r = iterate_requests.getRequest(); | ||
90 | if (!r.canceled) | ||
91 | r.peerProcessor.onPeer(infoMessage.peerIdentity, infoMessage.hello); | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public void handleError() { | ||
96 | client.reconnect(); | ||
97 | iterate_requests.restart(); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | |||
102 | public PeerInfo(Configuration cfg) { | ||
103 | client = new Client("peerinfo", cfg); | ||
104 | client.installReceiver(new PeerInfoMessageReceiver()); | ||
105 | iterate_requests = new SequentialRequestContainer<PeerIterateRequest>(client); | ||
106 | // Make sure that new requests are only sent once the old request has finished. | ||
107 | // Otherwise, the peerinfo service would send the answers interleaved. | ||
108 | iterate_requests.setOverlap(false); | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * Iterates over the HELLOs of all peers. | ||
113 | * | ||
114 | * @param timeout | ||
115 | * @param processor | ||
116 | * @return | ||
117 | */ | ||
118 | public Cancelable iterate(RelativeTime timeout, boolean friend_only, PeerProcessor processor) { | ||
119 | return iterate(timeout, null, friend_only, processor); | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Iterates over the HELLOs of the given peer. | ||
124 | * Can be called with peer=null to iterate over all peers. | ||
125 | * @param timeout | ||
126 | * @param peer | ||
127 | * @param processor | ||
128 | * @return | ||
129 | */ | ||
130 | public Cancelable iterate(RelativeTime timeout, PeerIdentity peer, boolean friend_only, PeerProcessor processor) { | ||
131 | PeerIterateRequest r = new PeerIterateRequest(peer, friend_only, processor); | ||
132 | return iterate_requests.addRequest(r); | ||
133 | } | ||
134 | |||
135 | public void disconnect() { | ||
136 | client.disconnect(); | ||
137 | } | ||
138 | |||
139 | public static void main(String... args) { | ||
140 | new Program(args) { | ||
141 | @Override | ||
142 | public void run() { | ||
143 | final PeerInfo peerInfo = new PeerInfo(getConfiguration()); | ||
144 | peerInfo.iterate(RelativeTime.FOREVER, false, new PeerProcessor() { | ||
145 | @Override | ||
146 | public void onPeer(PeerIdentity peerIdentity, HelloMessage hello) { | ||
147 | System.out.println("peer " + peerIdentity.toString()); | ||
148 | } | ||
149 | |||
150 | @Override | ||
151 | public void onEnd() { | ||
152 | System.out.println("got peer end"); | ||
153 | peerInfo.disconnect(); | ||
154 | } | ||
155 | }); | ||
156 | } | ||
157 | }.start(); | ||
158 | } | ||
159 | } | ||
diff --git a/src/org/gnunet/peerinfo/PeerProcessor.java b/src/main/java/org/gnunet/peerinfo/PeerProcessor.java index b096c37..b096c37 100644 --- a/src/org/gnunet/peerinfo/PeerProcessor.java +++ b/src/main/java/org/gnunet/peerinfo/PeerProcessor.java | |||
diff --git a/src/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java b/src/main/java/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java index 86985f3..86985f3 100644 --- a/src/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java +++ b/src/main/java/org/gnunet/peerinfo/RsaPublicKeyBinaryEncoded.java | |||
diff --git a/src/org/gnunet/peerinfo/package-info.java b/src/main/java/org/gnunet/peerinfo/package-info.java index 19cebdb..19cebdb 100644 --- a/src/org/gnunet/peerinfo/package-info.java +++ b/src/main/java/org/gnunet/peerinfo/package-info.java | |||
diff --git a/src/main/java/org/gnunet/requests/MatchingRequestContainer.java b/src/main/java/org/gnunet/requests/MatchingRequestContainer.java new file mode 100644 index 0000000..656d162 --- /dev/null +++ b/src/main/java/org/gnunet/requests/MatchingRequestContainer.java | |||
@@ -0,0 +1,51 @@ | |||
1 | package org.gnunet.requests; | ||
2 | |||
3 | import com.google.common.collect.Maps; | ||
4 | import org.gnunet.mq.MessageQueue; | ||
5 | import org.gnunet.util.Cancelable; | ||
6 | |||
7 | import java.util.Map; | ||
8 | |||
9 | |||
10 | public class MatchingRequestContainer<K, T extends RequestContainer.Request> extends RequestContainer { | ||
11 | private Map<K,T> requests = Maps.newHashMap(); | ||
12 | private final MessageQueue mq; | ||
13 | |||
14 | public MatchingRequestContainer(MessageQueue mq) { | ||
15 | this.mq = mq; | ||
16 | } | ||
17 | |||
18 | public Cancelable addRequest(K key, final T request) { | ||
19 | if (requests.containsKey(key)) | ||
20 | throw new AssertionError("key already present in request container"); | ||
21 | requests.put(key, request); | ||
22 | mq.send(request.assembleRequest()); | ||
23 | return new Cancelable() { | ||
24 | @Override | ||
25 | public void cancel() { | ||
26 | setRequestCancelled(request); | ||
27 | if (isRequestTransmitting(request)) { | ||
28 | cancelRequestTransmission(request); | ||
29 | } else { | ||
30 | request.cancel(); | ||
31 | } | ||
32 | } | ||
33 | }; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public void restart() { | ||
38 | Map<K,T> requestsOld = requests; | ||
39 | requests = Maps.newHashMap(); | ||
40 | for (Map.Entry<K,T> e : requestsOld.entrySet()) { | ||
41 | if (!isRequestCancelled(e.getValue())) { | ||
42 | setRequestTransmitting(e.getValue(), false); | ||
43 | addRequest(e.getKey(), e.getValue()); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | |||
48 | public T getRequest(K key) { | ||
49 | return requests.get(key); | ||
50 | } | ||
51 | } | ||
diff --git a/src/main/java/org/gnunet/requests/RequestContainer.java b/src/main/java/org/gnunet/requests/RequestContainer.java new file mode 100644 index 0000000..c7627f0 --- /dev/null +++ b/src/main/java/org/gnunet/requests/RequestContainer.java | |||
@@ -0,0 +1,59 @@ | |||
1 | package org.gnunet.requests; | ||
2 | |||
3 | |||
4 | import org.gnunet.mq.Envelope; | ||
5 | import org.gnunet.util.Cancelable; | ||
6 | |||
7 | public abstract class RequestContainer { | ||
8 | protected boolean overlap = true; | ||
9 | |||
10 | public abstract static class Request { | ||
11 | private boolean transmitting; | ||
12 | private boolean canceled; | ||
13 | private Cancelable cancelRequest; | ||
14 | public abstract Envelope assembleRequest(); | ||
15 | public void cancel() { | ||
16 | throw new AssertionError("request of type " + this.getClass() + " can not be canceled (not implemented)"); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | /** | ||
21 | * Re-send all requests in the queue that have not been canceled. | ||
22 | */ | ||
23 | public abstract void restart(); | ||
24 | |||
25 | /** | ||
26 | * Allow or disallow requests to be send while other requests in the queue have not been completed. | ||
27 | * | ||
28 | * @param overlap true to allow overlapped requests, false to disallow them | ||
29 | */ | ||
30 | public void setOverlap(boolean overlap) { | ||
31 | this.overlap = overlap; | ||
32 | } | ||
33 | |||
34 | protected boolean isRequestTransmitting(Request r) { | ||
35 | return r.transmitting; | ||
36 | } | ||
37 | |||
38 | protected void setRequestTransmitting(Request r, boolean transmitting) { | ||
39 | r.transmitting = transmitting; | ||
40 | } | ||
41 | |||
42 | protected void setRequestTransmissionCancel(Request request, Cancelable cancel) { | ||
43 | request.cancelRequest = cancel; | ||
44 | } | ||
45 | |||
46 | protected void cancelRequestTransmission(Request r) { | ||
47 | r.cancelRequest.cancel(); | ||
48 | r.cancelRequest = null; | ||
49 | } | ||
50 | |||
51 | protected void setRequestCancelled(Request r) { | ||
52 | r.canceled = true; | ||
53 | } | ||
54 | |||
55 | protected boolean isRequestCancelled(Request r) { | ||
56 | return r.canceled; | ||
57 | } | ||
58 | |||
59 | } | ||
diff --git a/src/main/java/org/gnunet/requests/SequentialRequestContainer.java b/src/main/java/org/gnunet/requests/SequentialRequestContainer.java new file mode 100644 index 0000000..037055f --- /dev/null +++ b/src/main/java/org/gnunet/requests/SequentialRequestContainer.java | |||
@@ -0,0 +1,86 @@ | |||
1 | package org.gnunet.requests; | ||
2 | |||
3 | import org.gnunet.mq.Envelope; | ||
4 | import org.gnunet.mq.MessageQueue; | ||
5 | import org.gnunet.mq.NotifySentHandler; | ||
6 | import org.gnunet.util.Cancelable; | ||
7 | |||
8 | import java.util.LinkedList; | ||
9 | |||
10 | /** | ||
11 | * Container for requests that are responded to in sequential order. | ||
12 | */ | ||
13 | public class SequentialRequestContainer<T extends RequestContainer.Request> extends RequestContainer { | ||
14 | private LinkedList<T> requests = new LinkedList<T>(); | ||
15 | private MessageQueue mq; | ||
16 | |||
17 | int requestsActive = 0; | ||
18 | |||
19 | public SequentialRequestContainer(MessageQueue mq) { | ||
20 | this.mq = mq; | ||
21 | } | ||
22 | |||
23 | public T getRequest() { | ||
24 | return requests.getFirst(); | ||
25 | } | ||
26 | |||
27 | public void next() { | ||
28 | if (requestsActive == 0 || requests.isEmpty()) | ||
29 | throw new AssertionError(); | ||
30 | requestsActive--; | ||
31 | requests.removeFirst(); | ||
32 | if (requestsActive == 0 && !requests.isEmpty()) { | ||
33 | Request r = requests.getFirst(); | ||
34 | setRequestTransmitting(r, true); | ||
35 | Envelope ev = r.assembleRequest(); | ||
36 | setRequestTransmissionCancel(r, ev); | ||
37 | mq.send(r.assembleRequest()); | ||
38 | requestsActive++; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | public Cancelable addRequest(final T request) { | ||
43 | requests.addLast(request); | ||
44 | if (overlap || requestsActive == 0) { | ||
45 | requestsActive++; | ||
46 | setRequestTransmitting(request, true); | ||
47 | Envelope ev = request.assembleRequest(); | ||
48 | ev.notifySent(new NotifySentHandler() { | ||
49 | @Override | ||
50 | public void onSent() { | ||
51 | setRequestTransmitting(request, false); | ||
52 | } | ||
53 | }); | ||
54 | setRequestTransmissionCancel(request, ev); | ||
55 | mq.send(request.assembleRequest()); | ||
56 | } | ||
57 | return new Cancelable() { | ||
58 | @Override | ||
59 | public void cancel() { | ||
60 | setRequestCancelled(request); | ||
61 | if (isRequestTransmitting(request)) { | ||
62 | cancelRequestTransmission(request); | ||
63 | } else { | ||
64 | request.cancel(); | ||
65 | } | ||
66 | } | ||
67 | }; | ||
68 | } | ||
69 | |||
70 | @Override | ||
71 | public void restart() { | ||
72 | LinkedList<T> requestsOld = requests; | ||
73 | requests = new LinkedList<T>(); | ||
74 | for (T r : requestsOld) { | ||
75 | if (!isRequestCancelled(r)) { | ||
76 | setRequestTransmitting(r, false); | ||
77 | addRequest(r); | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | |||
83 | public Iterable<T> iter() { | ||
84 | return requests; | ||
85 | } | ||
86 | } | ||
diff --git a/src/org/gnunet/requests/package-info.java b/src/main/java/org/gnunet/requests/package-info.java index 892a606..892a606 100644 --- a/src/org/gnunet/requests/package-info.java +++ b/src/main/java/org/gnunet/requests/package-info.java | |||
diff --git a/src/org/gnunet/statistics/GetMessage.java b/src/main/java/org/gnunet/statistics/GetMessage.java index eb44fbd..eb44fbd 100644 --- a/src/org/gnunet/statistics/GetMessage.java +++ b/src/main/java/org/gnunet/statistics/GetMessage.java | |||
diff --git a/src/main/java/org/gnunet/statistics/GetRequest.java b/src/main/java/org/gnunet/statistics/GetRequest.java new file mode 100644 index 0000000..8f421b1 --- /dev/null +++ b/src/main/java/org/gnunet/statistics/GetRequest.java | |||
@@ -0,0 +1,31 @@ | |||
1 | package org.gnunet.statistics; | ||
2 | |||
3 | import org.gnunet.mq.Envelope; | ||
4 | import org.gnunet.mq.MessageQueue; | ||
5 | import org.gnunet.requests.RequestContainer; | ||
6 | |||
7 | /** | ||
8 | */ | ||
9 | public class GetRequest extends RequestContainer.Request { | ||
10 | private final String subsystem; | ||
11 | private final String name; | ||
12 | public final StatisticsReceiver receiver; | ||
13 | private RequestContainer parent; | ||
14 | |||
15 | public GetRequest(String subsystem, String name, StatisticsReceiver receiver) { | ||
16 | this.subsystem = subsystem; | ||
17 | this.name = name; | ||
18 | this.receiver = receiver; | ||
19 | } | ||
20 | |||
21 | @Override | ||
22 | public Envelope assembleRequest() { | ||
23 | GetMessage m = new GetMessage(); | ||
24 | m.subsystemName = subsystem; | ||
25 | m.statisticsName = name; | ||
26 | return new Envelope(m); | ||
27 | } | ||
28 | |||
29 | public void cancel() { | ||
30 | } | ||
31 | } | ||
diff --git a/src/org/gnunet/statistics/GetResponseEndMessage.java b/src/main/java/org/gnunet/statistics/GetResponseEndMessage.java index eebe16e..eebe16e 100644 --- a/src/org/gnunet/statistics/GetResponseEndMessage.java +++ b/src/main/java/org/gnunet/statistics/GetResponseEndMessage.java | |||
diff --git a/src/org/gnunet/statistics/GetResponseMessage.java b/src/main/java/org/gnunet/statistics/GetResponseMessage.java index 2a722e6..2a722e6 100644 --- a/src/org/gnunet/statistics/GetResponseMessage.java +++ b/src/main/java/org/gnunet/statistics/GetResponseMessage.java | |||
diff --git a/src/org/gnunet/statistics/SetMessage.java b/src/main/java/org/gnunet/statistics/SetMessage.java index d75e3bf..d75e3bf 100644 --- a/src/org/gnunet/statistics/SetMessage.java +++ b/src/main/java/org/gnunet/statistics/SetMessage.java | |||
diff --git a/src/main/java/org/gnunet/statistics/SetRequest.java b/src/main/java/org/gnunet/statistics/SetRequest.java new file mode 100644 index 0000000..4aa4a82 --- /dev/null +++ b/src/main/java/org/gnunet/statistics/SetRequest.java | |||
@@ -0,0 +1,48 @@ | |||
1 | package org.gnunet.statistics; | ||
2 | |||
3 | import org.gnunet.mq.Envelope; | ||
4 | import org.gnunet.mq.MessageQueue; | ||
5 | import org.gnunet.requests.RequestContainer; | ||
6 | import org.gnunet.util.RelativeTime; | ||
7 | |||
8 | |||
9 | public class SetRequest extends RequestContainer.Request { | ||
10 | /** | ||
11 | * Time after we give up on setting values in statistics | ||
12 | */ | ||
13 | private static final RelativeTime SET_TIMEOUT = RelativeTime.SECOND.multiply(10); | ||
14 | |||
15 | private final static int SETFLAG_RELATIVE = 1; | ||
16 | private final static int SETFLAG_PERSIST = 2; | ||
17 | private final String subsystem; | ||
18 | private final String name; | ||
19 | private final boolean persist; | ||
20 | private final long value; | ||
21 | private final boolean relative; | ||
22 | |||
23 | public SetRequest(String subsystem, String name, long value, boolean relative, boolean persist) { | ||
24 | this.subsystem = subsystem; | ||
25 | this.name = name; | ||
26 | this.persist = persist; | ||
27 | this.value = value; | ||
28 | this.relative = relative; | ||
29 | |||
30 | } | ||
31 | |||
32 | @Override | ||
33 | public Envelope assembleRequest() { | ||
34 | SetMessage m = new SetMessage(); | ||
35 | m.statisticName = name; | ||
36 | m.subsystemName = subsystem; | ||
37 | m.value = value; | ||
38 | if (relative) | ||
39 | m.flags |= SETFLAG_RELATIVE; | ||
40 | if (persist) | ||
41 | m.flags |= SETFLAG_PERSIST; | ||
42 | return new Envelope(m); | ||
43 | } | ||
44 | |||
45 | public void cancel() { | ||
46 | //To change body of implemented methods use File | Settings | File Templates. | ||
47 | } | ||
48 | } | ||
diff --git a/src/main/java/org/gnunet/statistics/Statistics.java b/src/main/java/org/gnunet/statistics/Statistics.java new file mode 100644 index 0000000..416db0c --- /dev/null +++ b/src/main/java/org/gnunet/statistics/Statistics.java | |||
@@ -0,0 +1,310 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * The stuff below does nothing whatsoever, first milestone of | ||
23 | * this project is to implement the StatisticsService api | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | package org.gnunet.statistics; | ||
28 | |||
29 | import org.gnunet.requests.MatchingRequestContainer; | ||
30 | import org.gnunet.requests.SequentialRequestContainer; | ||
31 | import org.gnunet.util.*; | ||
32 | import org.gnunet.util.getopt.Argument; | ||
33 | import org.gnunet.util.getopt.ArgumentAction; | ||
34 | import org.slf4j.Logger; | ||
35 | import org.slf4j.LoggerFactory; | ||
36 | |||
37 | /** | ||
38 | * API for the GNUnet statistics service. | ||
39 | * <p/> | ||
40 | * Set, get and monitor statistics values, represented as unsigned 64bit integer. | ||
41 | * Note that {@literal long}, java's largest primitive type, can only store signed 64bit integers. | ||
42 | * With absolute operation, its negative values are interpreted as large numbers by the statistics api. | ||
43 | */ | ||
44 | public class Statistics { | ||
45 | private static final Logger logger = LoggerFactory | ||
46 | .getLogger(Statistics.class); | ||
47 | |||
48 | /** | ||
49 | * Client connecting us to the statistics service. | ||
50 | */ | ||
51 | private final Client client; | ||
52 | |||
53 | SequentialRequestContainer<SetRequest> set_requests; | ||
54 | SequentialRequestContainer<GetRequest> get_requests; | ||
55 | MatchingRequestContainer<Long,WatchRequest> watch_requests; | ||
56 | |||
57 | private boolean destroy_requested; | ||
58 | |||
59 | private long wid = 0; | ||
60 | |||
61 | public class StatisticsMessageReceiver extends RunaboutMessageReceiver { | ||
62 | public void visit(GetResponseMessage m) { | ||
63 | GetRequest r = get_requests.getRequest(); | ||
64 | if (r != null) | ||
65 | r.receiver.onReceive(m.subsystemName, m.statisticName, m.value); | ||
66 | } | ||
67 | |||
68 | public void visit(GetResponseEndMessage m) { | ||
69 | GetRequest r = get_requests.getRequest(); | ||
70 | if (r != null) | ||
71 | r.receiver.onDone(); | ||
72 | //noinspection ConstantConditions | ||
73 | if (get_requests != null) | ||
74 | get_requests.next(); | ||
75 | } | ||
76 | |||
77 | public void visit(TestMessage m) { | ||
78 | System.out.println("got back TEST message"); | ||
79 | client.disconnect(); | ||
80 | } | ||
81 | |||
82 | public void visit(WatchResponseMessage wrm) { | ||
83 | WatchRequest r = watch_requests.getRequest((long) wrm.wid); | ||
84 | if (r != null) { | ||
85 | r.watcher.onReceive(r.subsystem, r.name, wrm.value); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public void handleError() { | ||
91 | if (!destroy_requested) { | ||
92 | client.reconnect(); | ||
93 | get_requests.restart(); | ||
94 | set_requests.restart(); | ||
95 | watch_requests.restart(); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
100 | public Statistics(Configuration cfg) { | ||
101 | client = new Client("statistics", cfg); | ||
102 | client.installReceiver(new StatisticsMessageReceiver()); | ||
103 | get_requests = new SequentialRequestContainer<GetRequest>(client); | ||
104 | set_requests = new SequentialRequestContainer<SetRequest>(client); | ||
105 | watch_requests = new MatchingRequestContainer<Long, WatchRequest>(client); | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Retrieve values from statistics. | ||
110 | * Only one instance of this request may be active simultaneously. | ||
111 | * Upon cancellation | ||
112 | * | ||
113 | * @param timeout time after we give up and call receiver.onTimeout | ||
114 | * @param subsystem the subsystem of interest | ||
115 | * @param name name of the statistics value belongs to | ||
116 | * @param receiver callback | ||
117 | * @return handle to cancel the request | ||
118 | */ | ||
119 | public Cancelable get(RelativeTime timeout, final String subsystem, final String name, | ||
120 | final StatisticsReceiver receiver) { | ||
121 | if (destroy_requested) | ||
122 | throw new AssertionError("already destroyed"); | ||
123 | return get_requests.addRequest(new GetRequest(subsystem, name, receiver)); | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Sets a statistics value asynchronously. | ||
128 | * | ||
129 | * @param name name of the entry | ||
130 | * @param value desired value | ||
131 | * @param persist keep value even if the statistics service restarts | ||
132 | * @return a handle to cancel the request | ||
133 | */ | ||
134 | public Cancelable set(final String subsystem, final String name, final long value, boolean persist) { | ||
135 | if (destroy_requested) | ||
136 | throw new AssertionError("already destroyed"); | ||
137 | return set_requests.addRequest(new SetRequest(subsystem, name, value, false, persist)); | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * Changes a statistics value asynchronously. | ||
142 | * | ||
143 | * @param name name of the entry | ||
144 | * @param delta relative difference to the old value | ||
145 | * @param persist keep value even if the statistics service restarts | ||
146 | * @return a handle to cancel the request | ||
147 | */ | ||
148 | public Cancelable update(final String subsystem, final String name, final long delta, boolean persist) { | ||
149 | if (destroy_requested) | ||
150 | throw new AssertionError("already destroyed"); | ||
151 | return set_requests.addRequest(new SetRequest(subsystem, name, delta, true, persist)); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Receive updates about changing statistics values. | ||
156 | * | ||
157 | * @param subsystem the subsystem to watch | ||
158 | * @param name the value to watch | ||
159 | * @param watcher the object that receives the updates | ||
160 | * @return a handle to cancel the request | ||
161 | */ | ||
162 | public Cancelable watch(final String subsystem, final String name, StatisticsWatcher watcher) { | ||
163 | if (destroy_requested) | ||
164 | throw new AssertionError("already destroyed"); | ||
165 | WatchRequest r = new WatchRequest(subsystem, name, watcher); | ||
166 | return watch_requests.addRequest(wid++, r); | ||
167 | } | ||
168 | |||
169 | /** | ||
170 | * Destroy handle to the statistics service. Always finishes writing pending values. | ||
171 | */ | ||
172 | public void destroy() { | ||
173 | destroy_requested = true; | ||
174 | client.send(new TestMessage()); | ||
175 | // wait until the service responds | ||
176 | // TODO: or timeout | ||
177 | System.out.println("destroying statistics"); | ||
178 | } | ||
179 | |||
180 | |||
181 | /** | ||
182 | * Statistics command line utility entry point | ||
183 | * | ||
184 | * @param args command line arguments | ||
185 | */ | ||
186 | public static void main(String[] args) { | ||
187 | new Program(args) { | ||
188 | @Argument( | ||
189 | shortname = "x", | ||
190 | longname = "set", | ||
191 | action = ArgumentAction.SET, | ||
192 | description = "watch a value") | ||
193 | boolean set; | ||
194 | @Argument( | ||
195 | shortname = "w", | ||
196 | longname = "watch", | ||
197 | action = ArgumentAction.SET, | ||
198 | description = "set a value") | ||
199 | boolean watch; | ||
200 | @Argument( | ||
201 | shortname = "n", | ||
202 | longname = "name", | ||
203 | action = ArgumentAction.STORE_STRING, | ||
204 | argumentName = "NAME", | ||
205 | description = "statistics name") | ||
206 | String statisticsName = ""; | ||
207 | @Argument( | ||
208 | shortname = "s", | ||
209 | longname = "subsystem", | ||
210 | argumentName = "SUBSYS", | ||
211 | action = ArgumentAction.STORE_STRING, | ||
212 | description = "subsystem name") | ||
213 | String subsystemName = ""; | ||
214 | @Argument( | ||
215 | shortname = "p", | ||
216 | longname = "persistent", | ||
217 | action = ArgumentAction.SET, | ||
218 | description = "set value persistently (used with -x)") | ||
219 | boolean persistent = false; | ||
220 | @Argument( | ||
221 | shortname = "r", | ||
222 | longname = "relative", | ||
223 | action = ArgumentAction.SET, | ||
224 | description = "set value relative to old value (used with -x)") | ||
225 | boolean relative = false; | ||
226 | |||
227 | @Override | ||
228 | protected String makeHelpText() { | ||
229 | return "Get, set and watch GNUnet's statistics."; | ||
230 | } | ||
231 | |||
232 | public void run() { | ||
233 | final Statistics statistics = new Statistics(cfg); | ||
234 | |||
235 | if (set && watch) { | ||
236 | System.err.println("--watch/-w and --set/-s cannot be used together"); | ||
237 | return; | ||
238 | } | ||
239 | |||
240 | if (set) { | ||
241 | if (subsystemName.isEmpty() || statisticsName.isEmpty()) { | ||
242 | System.err.println("both subsystem and name must be given for --set/-x"); | ||
243 | return; | ||
244 | } | ||
245 | if (unprocessedArgs.length != 1) { | ||
246 | System.err.println("must specify exactly one value to set"); | ||
247 | return; | ||
248 | } | ||
249 | long value; | ||
250 | try { | ||
251 | value = Long.parseLong(unprocessedArgs[0]); | ||
252 | } catch (NumberFormatException e) { | ||
253 | System.err.println("invalid value (not a long)"); | ||
254 | return; | ||
255 | } | ||
256 | if (relative) | ||
257 | statistics.update(subsystemName, statisticsName, value, persistent); | ||
258 | else | ||
259 | statistics.set(subsystemName, statisticsName, value, persistent); | ||
260 | statistics.destroy(); | ||
261 | return; | ||
262 | } | ||
263 | |||
264 | if (unprocessedArgs.length != 0) { | ||
265 | System.err.println("dumping statistics does not take any positional parameters"); | ||
266 | return; | ||
267 | } | ||
268 | |||
269 | if (watch) { | ||
270 | if (subsystemName.isEmpty() || statisticsName.isEmpty()) { | ||
271 | System.err.println("both subsystem and name must be given for --watch/-w"); | ||
272 | return; | ||
273 | } | ||
274 | statistics.watch(subsystemName, statisticsName, | ||
275 | new StatisticsWatcher() { | ||
276 | @Override | ||
277 | public void onReceive(String subsystem, String name, long value) { | ||
278 | System.out.println(subsystem + "(" + name + ") = " + value); | ||
279 | } | ||
280 | |||
281 | @Override | ||
282 | public void onTimeout() { | ||
283 | System.err.println("timeout"); | ||
284 | } | ||
285 | } | ||
286 | ); | ||
287 | } else { | ||
288 | statistics.get(RelativeTime.SECOND, subsystemName, statisticsName, | ||
289 | new StatisticsReceiver() { | ||
290 | @Override | ||
291 | public void onReceive(String subsystem, String name, long value) { | ||
292 | System.out.println(subsystem + "(" + name + ") = " + value); | ||
293 | } | ||
294 | |||
295 | @Override | ||
296 | public void onTimeout() { | ||
297 | System.err.println("timeout"); | ||
298 | } | ||
299 | |||
300 | @Override | ||
301 | public void onDone() { | ||
302 | statistics.destroy(); | ||
303 | } | ||
304 | } | ||
305 | ); | ||
306 | } | ||
307 | } | ||
308 | }.start(); | ||
309 | } | ||
310 | } | ||
diff --git a/src/org/gnunet/statistics/StatisticsReceiver.java b/src/main/java/org/gnunet/statistics/StatisticsReceiver.java index 87fbdd6..0c25acc 100644 --- a/src/org/gnunet/statistics/StatisticsReceiver.java +++ b/src/main/java/org/gnunet/statistics/StatisticsReceiver.java | |||
@@ -24,4 +24,6 @@ package org.gnunet.statistics; | |||
24 | 24 | ||
25 | public interface StatisticsReceiver { | 25 | public interface StatisticsReceiver { |
26 | public void onReceive(String subsystem, String name, long value); | 26 | public void onReceive(String subsystem, String name, long value); |
27 | public void onTimeout(); | ||
28 | public void onDone(); | ||
27 | } | 29 | } |
diff --git a/src/main/java/org/gnunet/statistics/StatisticsWatcher.java b/src/main/java/org/gnunet/statistics/StatisticsWatcher.java new file mode 100644 index 0000000..d3fb645 --- /dev/null +++ b/src/main/java/org/gnunet/statistics/StatisticsWatcher.java | |||
@@ -0,0 +1,13 @@ | |||
1 | package org.gnunet.statistics; | ||
2 | |||
3 | /** | ||
4 | * Created with IntelliJ IDEA. | ||
5 | * User: dold | ||
6 | * Date: 8/24/13 | ||
7 | * Time: 5:56 PM | ||
8 | * To change this template use File | Settings | File Templates. | ||
9 | */ | ||
10 | public interface StatisticsWatcher { | ||
11 | public void onReceive(String subsystem, String name, long value); | ||
12 | public void onTimeout(); | ||
13 | } | ||
diff --git a/src/org/gnunet/statistics/WatchMessage.java b/src/main/java/org/gnunet/statistics/WatchMessage.java index 814f263..814f263 100644 --- a/src/org/gnunet/statistics/WatchMessage.java +++ b/src/main/java/org/gnunet/statistics/WatchMessage.java | |||
diff --git a/src/main/java/org/gnunet/statistics/WatchRequest.java b/src/main/java/org/gnunet/statistics/WatchRequest.java new file mode 100644 index 0000000..49e1615 --- /dev/null +++ b/src/main/java/org/gnunet/statistics/WatchRequest.java | |||
@@ -0,0 +1,31 @@ | |||
1 | package org.gnunet.statistics; | ||
2 | |||
3 | import org.gnunet.mq.Envelope; | ||
4 | import org.gnunet.mq.MessageQueue; | ||
5 | import org.gnunet.requests.RequestContainer; | ||
6 | |||
7 | /** | ||
8 | */ | ||
9 | public class WatchRequest extends RequestContainer.Request { | ||
10 | public String subsystem; | ||
11 | public String name; | ||
12 | public StatisticsWatcher watcher; | ||
13 | |||
14 | public WatchRequest(String subsystem, String name, StatisticsWatcher watcher) { | ||
15 | this.subsystem = subsystem; | ||
16 | this.name = name; | ||
17 | this.watcher = watcher; | ||
18 | } | ||
19 | |||
20 | @Override | ||
21 | public Envelope assembleRequest() { | ||
22 | WatchMessage m = new WatchMessage(); | ||
23 | m.statisticsName = name; | ||
24 | m.subsystemName = subsystem; | ||
25 | return new Envelope(m); | ||
26 | } | ||
27 | |||
28 | public void cancel() { | ||
29 | |||
30 | } | ||
31 | } | ||
diff --git a/src/org/gnunet/statistics/WatchResponseMessage.java b/src/main/java/org/gnunet/statistics/WatchResponseMessage.java index 19c56b5..19c56b5 100644 --- a/src/org/gnunet/statistics/WatchResponseMessage.java +++ b/src/main/java/org/gnunet/statistics/WatchResponseMessage.java | |||
diff --git a/src/org/gnunet/statistics/package-info.java b/src/main/java/org/gnunet/statistics/package-info.java index fce59e7..fce59e7 100644 --- a/src/org/gnunet/statistics/package-info.java +++ b/src/main/java/org/gnunet/statistics/package-info.java | |||
diff --git a/src/main/java/org/gnunet/testbed/Controller.java b/src/main/java/org/gnunet/testbed/Controller.java new file mode 100644 index 0000000..7372986 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/Controller.java | |||
@@ -0,0 +1,84 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | import org.gnunet.util.Client; | ||
4 | import org.gnunet.util.Configuration; | ||
5 | |||
6 | /** | ||
7 | * Handle to interact with a GNUnet testbed controller. Each | ||
8 | * controller has at least one master handle which is created when the | ||
9 | * controller is created; this master handle interacts with the | ||
10 | * controller process, destroying it destroys the controller (by | ||
11 | * closing stdin of the controller process). Additionally, | ||
12 | * controllers can interact with each other (in a P2P fashion); those | ||
13 | * links are established via TCP/IP on the controller's service port. | ||
14 | */ | ||
15 | public class Controller { | ||
16 | |||
17 | /** | ||
18 | * Client connecting to the testbed service. | ||
19 | */ | ||
20 | Client client; | ||
21 | |||
22 | /** | ||
23 | * Connect to a controller process. The configuration to use for the connection | ||
24 | * is retreived from the given host where a controller is started using | ||
25 | * GNUNET_TESTBED_controller_start(). | ||
26 | * | ||
27 | * @param host host to run the controller on; This should be the same host if | ||
28 | * the controller was previously started with | ||
29 | * GNUNET_TESTBED_controller_start() | ||
30 | * @param event_mask bit mask with set of events to call 'cc' for; | ||
31 | * or-ed values of "1LL" shifted by the | ||
32 | * respective 'enum GNUNET_TESTBED_EventType' | ||
33 | * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...") | ||
34 | * @param cb controller callback to invoke on events | ||
35 | */ | ||
36 | public Controller(Host host, long event_mask, ControllerEventCallback cb) { | ||
37 | client = new Client("testbed", host.cfg); | ||
38 | } | ||
39 | |||
40 | |||
41 | /** | ||
42 | * Create the given peer at the specified host using the given | ||
43 | * controller. If the given controller is not running on the target | ||
44 | * host, it should find or create a controller at the target host and | ||
45 | * delegate creating the peer. Explicit delegation paths can be setup | ||
46 | * using 'GNUNET_TESTBED_controller_link'. If no explicit delegation | ||
47 | * path exists, a direct link with a subordinate controller is setup | ||
48 | * for the first delegated peer to a particular host; the subordinate | ||
49 | * controller is then destroyed once the last peer that was delegated | ||
50 | * to the remote host is stopped. | ||
51 | * | ||
52 | * Creating the peer only creates the handle to manipulate and further | ||
53 | * configure the peer; use "GNUNET_TESTBED_peer_start" and | ||
54 | * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's | ||
55 | * processes. | ||
56 | * | ||
57 | * Note that the given configuration will be adjusted by the | ||
58 | * controller to avoid port/path conflicts with other peers. | ||
59 | * The "final" configuration can be obtained using | ||
60 | * 'GNUNET_TESTBED_peer_get_information'. | ||
61 | * | ||
62 | * @param host host to run the peer on; cannot be NULL | ||
63 | * @param cfg Template configuration to use for the peer. Should exist until | ||
64 | * operation is cancelled or GNUNET_TESTBED_operation_done() is called | ||
65 | * @param cb the callback to call when the peer has been created | ||
66 | * @return the operation handle | ||
67 | */ | ||
68 | |||
69 | public Operation createPeer(Host host, Configuration cfg, PeerCreateCallback cb) { | ||
70 | return null; | ||
71 | } | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Stop the given controller (also will terminate all peers and | ||
76 | * controllers dependent on this controller). This function | ||
77 | * blocks until the testbed has been fully terminated (!). | ||
78 | */ | ||
79 | public void disconnect () { | ||
80 | |||
81 | } | ||
82 | |||
83 | |||
84 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/ControllerEventCallback.java b/src/main/java/org/gnunet/testbed/ControllerEventCallback.java new file mode 100644 index 0000000..099977f --- /dev/null +++ b/src/main/java/org/gnunet/testbed/ControllerEventCallback.java | |||
@@ -0,0 +1,21 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | |||
5 | public abstract class ControllerEventCallback { | ||
6 | void onPeerStart() { | ||
7 | throw new AssertionError("event not handled"); | ||
8 | } | ||
9 | void onPeerStop() { | ||
10 | throw new AssertionError("event not handled"); | ||
11 | } | ||
12 | void onPeerConnect() { | ||
13 | throw new AssertionError("event not handled"); | ||
14 | } | ||
15 | void onPeerDisconnect() { | ||
16 | throw new AssertionError("event not handled"); | ||
17 | } | ||
18 | void onOperationFinished() { | ||
19 | throw new AssertionError("event not handled"); | ||
20 | } | ||
21 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/ControllerInitMessage.java b/src/main/java/org/gnunet/testbed/ControllerInitMessage.java new file mode 100644 index 0000000..95114d7 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/ControllerInitMessage.java | |||
@@ -0,0 +1,30 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | import org.gnunet.construct.UInt32; | ||
5 | import org.gnunet.construct.UInt64; | ||
6 | import org.gnunet.construct.UnionCase; | ||
7 | import org.gnunet.construct.ZeroTerminatedString; | ||
8 | import org.gnunet.util.GnunetMessage; | ||
9 | |||
10 | @UnionCase(460) | ||
11 | public class ControllerInitMessage implements GnunetMessage.Body { | ||
12 | /** | ||
13 | * Host ID that the controller is either given (if this is the | ||
14 | * dominating client) or assumed to have (for peer-connections | ||
15 | * between controllers). A controller must check that all | ||
16 | * connections make consistent claims... | ||
17 | */ | ||
18 | @UInt32 | ||
19 | public int host_id; | ||
20 | |||
21 | /** | ||
22 | * Event mask that specifies which events this client is interested in. | ||
23 | */ | ||
24 | @UInt64 | ||
25 | public long event_mask; | ||
26 | |||
27 | @ZeroTerminatedString | ||
28 | public String controler_hostname; | ||
29 | |||
30 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/ControllerProc.java b/src/main/java/org/gnunet/testbed/ControllerProc.java new file mode 100644 index 0000000..9eaca2c --- /dev/null +++ b/src/main/java/org/gnunet/testbed/ControllerProc.java | |||
@@ -0,0 +1,89 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | import com.google.common.base.Charsets; | ||
4 | import org.gnunet.util.Helper; | ||
5 | import org.gnunet.util.RunaboutMessageReceiver; | ||
6 | |||
7 | import java.io.ByteArrayOutputStream; | ||
8 | import java.util.zip.Deflater; | ||
9 | |||
10 | /** | ||
11 | * A controller process. | ||
12 | * The controller process is either a local helper process, or an ssh process that starts and controls | ||
13 | * the testbed helper on a remote machine. | ||
14 | */ | ||
15 | public class ControllerProc { | ||
16 | final Helper helper; | ||
17 | |||
18 | |||
19 | public class ControllerProcReceiver extends RunaboutMessageReceiver { | ||
20 | public void visit(HelperReplyMessage m) { | ||
21 | |||
22 | } | ||
23 | @Override | ||
24 | public void handleError() { | ||
25 | |||
26 | } | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Starts a controller process at the given host. The given host's configration | ||
31 | * is used as a Template configuration to use for the remote controller; the | ||
32 | * remote controller will be started with a slightly modified configuration | ||
33 | * (port numbers, unix domain sockets and service home values are changed as per | ||
34 | * TESTING library on the remote host). The modified configuration replaces the | ||
35 | * host's existing configuration before signalling success through the | ||
36 | * GNUNET_TESTBED_ControllerStatusCallback() | ||
37 | * | ||
38 | * @param trustedIP the ip address of the controller which will be set as TRUSTED | ||
39 | * HOST(all connections form this ip are permitted by the testbed) when | ||
40 | * starting testbed controller at host. This can either be a single ip | ||
41 | * address or a network address in CIDR notation. | ||
42 | * @param host the host where the controller has to be started. CANNOT be NULL. | ||
43 | * @param cb function called when the controller is successfully started or | ||
44 | * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be | ||
45 | * called if cb is called with GNUNET_SYSERR as status. Will never be | ||
46 | * called in the same task as 'GNUNET_TESTBED_controller_start' | ||
47 | * (synchronous errors will be signalled by returning NULL). This | ||
48 | * parameter cannot be NULL. | ||
49 | */ | ||
50 | public ControllerProc(String trustedIP, Host host, ControllerStatusCallback cb) { | ||
51 | if (host.isLocal()) { | ||
52 | helper = new Helper(false, "gnunet-testbed-helper", null, new ControllerProcReceiver()); | ||
53 | } else { | ||
54 | throw new AssertionError("not implemented yet"); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | |||
59 | private HelperInitMessage makeHelperInitMessage(String trustedIP, Host host) { | ||
60 | HelperInitMessage m = new HelperInitMessage(); | ||
61 | if (host.hostname == null) { | ||
62 | m.hostname = null; | ||
63 | m.hostname_size = 0; | ||
64 | } else { | ||
65 | m.hostname_size = host.hostname.length(); | ||
66 | m.hostname = host.hostname.getBytes(Charsets.UTF_8); | ||
67 | } | ||
68 | m.trusted_ip_size = trustedIP.length(); | ||
69 | m.trusted_ip = trustedIP; | ||
70 | |||
71 | byte[] serialized_config = host.cfg.serialize().getBytes(); | ||
72 | |||
73 | Deflater compresser = new Deflater(); | ||
74 | compresser.setInput(serialized_config); | ||
75 | compresser.finish(); | ||
76 | |||
77 | ByteArrayOutputStream s = new ByteArrayOutputStream(); | ||
78 | byte[] buf = new byte[1024]; | ||
79 | while (!compresser.finished()) { | ||
80 | int n = compresser.deflate(buf); | ||
81 | s.write(buf, 0, n); | ||
82 | } | ||
83 | |||
84 | m.compressed_config = s.toByteArray(); | ||
85 | m.config_size = serialized_config.length; | ||
86 | |||
87 | return m; | ||
88 | } | ||
89 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/ControllerStatusCallback.java b/src/main/java/org/gnunet/testbed/ControllerStatusCallback.java new file mode 100644 index 0000000..ba9e56f --- /dev/null +++ b/src/main/java/org/gnunet/testbed/ControllerStatusCallback.java | |||
@@ -0,0 +1,9 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | import org.gnunet.util.Configuration; | ||
5 | |||
6 | public interface ControllerStatusCallback { | ||
7 | void onStartupSuccess(Configuration cfg); | ||
8 | void onStartupFailure(); | ||
9 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/HelperInitMessage.java b/src/main/java/org/gnunet/testbed/HelperInitMessage.java new file mode 100644 index 0000000..29d8bb6 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/HelperInitMessage.java | |||
@@ -0,0 +1,37 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | import org.gnunet.construct.*; | ||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | |||
6 | /** | ||
7 | * Initialization message for gnunet-helper-testbed to start testbed service | ||
8 | */ | ||
9 | @UnionCase(495) | ||
10 | public class HelperInitMessage implements GnunetMessage.Body { | ||
11 | /** | ||
12 | * The controller hostname size excluding the NULL termination character - | ||
13 | * strlen (hostname); cannot be zero | ||
14 | */ | ||
15 | @UInt16 | ||
16 | int trusted_ip_size; | ||
17 | /** | ||
18 | * The hostname size excluding the NULL termination character - strlen | ||
19 | * (hostname); cannot be zero | ||
20 | */ | ||
21 | @UInt16 | ||
22 | int hostname_size; | ||
23 | /** | ||
24 | * The size of the uncompressed configuration | ||
25 | */ | ||
26 | @UInt16 | ||
27 | public int config_size; | ||
28 | |||
29 | @ZeroTerminatedString(optional = true) | ||
30 | public String trusted_ip; | ||
31 | |||
32 | @VariableSizeIntegerArray(signed = true, bitSize = 8, lengthField = "hostname_size") | ||
33 | public byte[] hostname; | ||
34 | |||
35 | @FillWith @UInt8 | ||
36 | public byte[] compressed_config; | ||
37 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/HelperReplyMessage.java b/src/main/java/org/gnunet/testbed/HelperReplyMessage.java new file mode 100644 index 0000000..bb50860 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/HelperReplyMessage.java | |||
@@ -0,0 +1,17 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | import org.gnunet.construct.FillWith; | ||
5 | import org.gnunet.construct.UInt16; | ||
6 | import org.gnunet.construct.UInt8; | ||
7 | import org.gnunet.construct.UnionCase; | ||
8 | import org.gnunet.util.GnunetMessage; | ||
9 | |||
10 | @UnionCase(496) | ||
11 | public class HelperReplyMessage implements GnunetMessage.Body { | ||
12 | @UInt16 | ||
13 | int uncompressed_config_size; | ||
14 | |||
15 | @FillWith @UInt8 | ||
16 | byte[] compressed_config; | ||
17 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/Host.java b/src/main/java/org/gnunet/testbed/Host.java new file mode 100644 index 0000000..fb89028 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/Host.java | |||
@@ -0,0 +1,51 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | import org.gnunet.util.Configuration; | ||
4 | |||
5 | /** | ||
6 | * Opaque handle to a host running experiments managed by the testing framework. | ||
7 | * The master process must be able to SSH to this host without password (via | ||
8 | * ssh-agent). | ||
9 | */ | ||
10 | public class Host { | ||
11 | static int nextUID = 1; | ||
12 | final Configuration cfg; | ||
13 | final String hostname; | ||
14 | final String username; | ||
15 | final int port; | ||
16 | |||
17 | private boolean controllerStarted; | ||
18 | |||
19 | /** | ||
20 | * Create a host to run peers and controllers on. | ||
21 | * | ||
22 | * @param hostname name of the host, use "NULL" for localhost | ||
23 | * @param username username to use for the login; may be NULL | ||
24 | * @param cfg the configuration to use as a template while starting a controller | ||
25 | * on this host. Operation queue sizes specific to a host are also | ||
26 | * read from this configuration handle | ||
27 | * @param port port number to use for ssh; use 0 to let ssh decide | ||
28 | */ | ||
29 | public Host(String hostname, String username, Configuration cfg, int port) { | ||
30 | this.port = (port == 0) ? 22 : port; | ||
31 | this.hostname = hostname; | ||
32 | this.username = username; | ||
33 | this.cfg = cfg; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Create a host handle for the local machine. | ||
38 | * | ||
39 | * @param cfg the configuration to use as a template while starting a controller | ||
40 | * on this host. Operation queue sizes specific to a host are also | ||
41 | * read from this configuration handle | ||
42 | * @param port port number to use for ssh; use 0 to let ssh decide | ||
43 | */ | ||
44 | public Host(Configuration cfg, int port) { | ||
45 | this(null, null, cfg, port); | ||
46 | } | ||
47 | |||
48 | public boolean isLocal() { | ||
49 | return hostname == null; | ||
50 | } | ||
51 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/Operation.java b/src/main/java/org/gnunet/testbed/Operation.java new file mode 100644 index 0000000..4403540 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/Operation.java | |||
@@ -0,0 +1,11 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | /** | ||
4 | * Created with IntelliJ IDEA. | ||
5 | * User: dold | ||
6 | * Date: 8/25/13 | ||
7 | * Time: 1:26 PM | ||
8 | * To change this template use File | Settings | File Templates. | ||
9 | */ | ||
10 | public interface Operation { | ||
11 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/OperationCompletionCallback.java b/src/main/java/org/gnunet/testbed/OperationCompletionCallback.java new file mode 100644 index 0000000..413be06 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/OperationCompletionCallback.java | |||
@@ -0,0 +1,11 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | /** | ||
4 | * Created with IntelliJ IDEA. | ||
5 | * User: dold | ||
6 | * Date: 8/25/13 | ||
7 | * Time: 1:35 PM | ||
8 | * To change this template use File | Settings | File Templates. | ||
9 | */ | ||
10 | public interface OperationCompletionCallback { | ||
11 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/Peer.java b/src/main/java/org/gnunet/testbed/Peer.java new file mode 100644 index 0000000..f9cfc3e --- /dev/null +++ b/src/main/java/org/gnunet/testbed/Peer.java | |||
@@ -0,0 +1,57 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | import org.gnunet.util.Configuration; | ||
4 | |||
5 | /** | ||
6 | * Opaque handle to a peer controlled by the testbed framework. A peer runs | ||
7 | * at a particular host. | ||
8 | */ | ||
9 | public class Peer { | ||
10 | public Operation start(PeerChurnCallback peerChurnCallback) { | ||
11 | return null; | ||
12 | } | ||
13 | |||
14 | public Operation stop(PeerChurnCallback peerChurnCallback) { | ||
15 | return null; | ||
16 | } | ||
17 | |||
18 | public Operation getInformation() { | ||
19 | return null; | ||
20 | } | ||
21 | |||
22 | /* | ||
23 | * Change peer configuration. Must only be called while the | ||
24 | * peer is stopped. Ports and paths cannot be changed this | ||
25 | * way. | ||
26 | */ | ||
27 | public Operation updateConfiguration(Configuration cfg) { | ||
28 | return null; | ||
29 | } | ||
30 | |||
31 | /* | ||
32 | * Change peer configuration. Must only be called while the | ||
33 | * peer is stopped. Ports and paths cannot be changed this | ||
34 | * way. | ||
35 | */ | ||
36 | public Operation destroy() { | ||
37 | return null; | ||
38 | } | ||
39 | |||
40 | public Operation manageService(String serviceName, boolean start) { | ||
41 | return null; | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Both peers must have been started before calling this function. | ||
46 | * This function then obtains a HELLO from this peer, gives it to 'otherPeer' | ||
47 | * and asks 'otherPeer' to connect to this peer.. | ||
48 | */ | ||
49 | public Operation connectOverlay(OperationCompletionCallback cb, Peer otherPeer) { | ||
50 | return null; | ||
51 | } | ||
52 | |||
53 | public Operation getServiceConnection(String serviceName /*,... */) { | ||
54 | return null; | ||
55 | } | ||
56 | |||
57 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/PeerChurnCallback.java b/src/main/java/org/gnunet/testbed/PeerChurnCallback.java new file mode 100644 index 0000000..a5dacbd --- /dev/null +++ b/src/main/java/org/gnunet/testbed/PeerChurnCallback.java | |||
@@ -0,0 +1,7 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | public interface PeerChurnCallback { | ||
5 | void onChurnSuccess(); | ||
6 | void onChurnError(String emsg); | ||
7 | } | ||
diff --git a/src/main/java/org/gnunet/testbed/PeerCreateCallback.java b/src/main/java/org/gnunet/testbed/PeerCreateCallback.java new file mode 100644 index 0000000..b6100f6 --- /dev/null +++ b/src/main/java/org/gnunet/testbed/PeerCreateCallback.java | |||
@@ -0,0 +1,7 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | public interface PeerCreateCallback { | ||
5 | void onPeerCreated(Peer peer); | ||
6 | void onError(String errorMessage); | ||
7 | } | ||
diff --git a/src/main/java/org/gnunet/testing/TestingFixture.java b/src/main/java/org/gnunet/testing/TestingFixture.java new file mode 100644 index 0000000..4303512 --- /dev/null +++ b/src/main/java/org/gnunet/testing/TestingFixture.java | |||
@@ -0,0 +1,21 @@ | |||
1 | package org.gnunet.testing; | ||
2 | |||
3 | import org.gnunet.util.Scheduler; | ||
4 | import org.junit.After; | ||
5 | import org.junit.Before; | ||
6 | |||
7 | /** | ||
8 | * Default JUnit4 fixture methods for gnunet-java tests. | ||
9 | * Resets the scheduler properly. | ||
10 | */ | ||
11 | public class TestingFixture { | ||
12 | @Before | ||
13 | public void beginGNJTest() { | ||
14 | Scheduler.forceReset(); | ||
15 | } | ||
16 | |||
17 | @After | ||
18 | public void endGNJTest() { | ||
19 | Scheduler.forceReset(); | ||
20 | } | ||
21 | } | ||
diff --git a/src/org/gnunet/testing/TestingServer.java b/src/main/java/org/gnunet/testing/TestingServer.java index 97b6bc0..97b6bc0 100644 --- a/src/org/gnunet/testing/TestingServer.java +++ b/src/main/java/org/gnunet/testing/TestingServer.java | |||
diff --git a/src/org/gnunet/testing/TestingSetup.java b/src/main/java/org/gnunet/testing/TestingSetup.java index 6f8d578..6f8d578 100644 --- a/src/org/gnunet/testing/TestingSetup.java +++ b/src/main/java/org/gnunet/testing/TestingSetup.java | |||
diff --git a/src/org/gnunet/testing/TestingSubsystem.java b/src/main/java/org/gnunet/testing/TestingSubsystem.java index 29de0bc..da7f2e4 100644 --- a/src/org/gnunet/testing/TestingSubsystem.java +++ b/src/main/java/org/gnunet/testing/TestingSubsystem.java | |||
@@ -51,7 +51,7 @@ public class TestingSubsystem { | |||
51 | 51 | ||
52 | public TestingSubsystem(String service) { | 52 | public TestingSubsystem(String service) { |
53 | try { | 53 | try { |
54 | p = Runtime.getRuntime().exec(new String[]{"gnunet-testing-run-service", "-s", service}); | 54 | p = Runtime.getRuntime().exec(new String[]{"gnunet-testing", "-r", service}); |
55 | } catch (IOException e) { | 55 | } catch (IOException e) { |
56 | throw new TestingSetup.SetupException(e); | 56 | throw new TestingSetup.SetupException(e); |
57 | } | 57 | } |
@@ -101,23 +101,13 @@ public class TestingSubsystem { | |||
101 | } catch (IOException e) { | 101 | } catch (IOException e) { |
102 | throw new TestingSetup.SetupException(e); | 102 | throw new TestingSetup.SetupException(e); |
103 | } | 103 | } |
104 | String line; | ||
105 | try { | ||
106 | line = reader.readLine(); | ||
107 | } catch (IOException e) { | ||
108 | throw new TestingSetup.SetupException(e); | ||
109 | } | ||
110 | if (line == null || !line.equals("bye")) { | ||
111 | throw new TestingSetup.SetupException("service not destroyed correctly"); | ||
112 | } | ||
113 | System.out.println("read bye"); | ||
114 | try { | 104 | try { |
115 | p.waitFor(); | 105 | p.waitFor(); |
116 | } catch (InterruptedException e) { | 106 | } catch (InterruptedException e) { |
117 | throw new TestingSetup.SetupException(e); | 107 | throw new TestingSetup.SetupException(e); |
118 | } | 108 | } |
119 | if (p.exitValue() != 0) { | 109 | if (p.exitValue() != 0) { |
120 | throw new TestingSetup.SetupException("gnunet-testing-run-service exit value unsuccessful"); | 110 | throw new TestingSetup.SetupException("gnunet-testing exit value unsuccessful"); |
121 | } | 111 | } |
122 | } | 112 | } |
123 | 113 | ||
@@ -129,7 +119,7 @@ public class TestingSubsystem { | |||
129 | throw new TestingSetup.SetupException(e); | 119 | throw new TestingSetup.SetupException(e); |
130 | } | 120 | } |
131 | String response; | 121 | String response; |
132 | logger.debug("waiting for gnunet-testing-run-service to respond to restart"); | 122 | logger.debug("waiting for gnunet-testing to respond to restart"); |
133 | try { | 123 | try { |
134 | response = reader.readLine(); | 124 | response = reader.readLine(); |
135 | } catch (IOException e) { | 125 | } catch (IOException e) { |
diff --git a/src/org/gnunet/testing/package-info.java b/src/main/java/org/gnunet/testing/package-info.java index 1a91586..1a91586 100644 --- a/src/org/gnunet/testing/package-info.java +++ b/src/main/java/org/gnunet/testing/package-info.java | |||
diff --git a/src/main/java/org/gnunet/transport/AddressIterateMessage.java b/src/main/java/org/gnunet/transport/AddressIterateMessage.java new file mode 100644 index 0000000..59ea6a8 --- /dev/null +++ b/src/main/java/org/gnunet/transport/AddressIterateMessage.java | |||
@@ -0,0 +1,34 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | |||
4 | import org.gnunet.construct.NestedMessage; | ||
5 | import org.gnunet.construct.UInt32; | ||
6 | import org.gnunet.construct.UnionCase; | ||
7 | import org.gnunet.util.AbsoluteTime; | ||
8 | import org.gnunet.util.PeerIdentity; | ||
9 | |||
10 | |||
11 | /** | ||
12 | * Message from the client to the transport service | ||
13 | * asking for binary addresses known for a peer. | ||
14 | */ | ||
15 | @UnionCase(380) | ||
16 | public class AddressIterateMessage { | ||
17 | /** | ||
18 | * One shot call or continous replies? | ||
19 | */ | ||
20 | @UInt32 | ||
21 | public int one_shot; | ||
22 | |||
23 | /** | ||
24 | * FIXME: This field seems to be deprecated in the C API? | ||
25 | */ | ||
26 | @NestedMessage | ||
27 | public AbsoluteTime timeout; | ||
28 | |||
29 | /** | ||
30 | * The identity of the peer to look up. | ||
31 | */ | ||
32 | @NestedMessage | ||
33 | public PeerIdentity peer; | ||
34 | } | ||
diff --git a/src/main/java/org/gnunet/transport/BlacklistCallback.java b/src/main/java/org/gnunet/transport/BlacklistCallback.java new file mode 100644 index 0000000..b763dec --- /dev/null +++ b/src/main/java/org/gnunet/transport/BlacklistCallback.java | |||
@@ -0,0 +1,12 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.util.PeerIdentity; | ||
4 | |||
5 | /** | ||
6 | * ... | ||
7 | * | ||
8 | * @author Florian Dold | ||
9 | */ | ||
10 | public interface BlacklistCallback { | ||
11 | boolean isAllowed(PeerIdentity peerIdentity); | ||
12 | } | ||
diff --git a/src/main/java/org/gnunet/transport/BlacklistInitMessage.java b/src/main/java/org/gnunet/transport/BlacklistInitMessage.java new file mode 100644 index 0000000..be3209e --- /dev/null +++ b/src/main/java/org/gnunet/transport/BlacklistInitMessage.java | |||
@@ -0,0 +1,9 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.construct.UnionCase; | ||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | |||
6 | @UnionCase(369) | ||
7 | public class BlacklistInitMessage implements GnunetMessage.Body { | ||
8 | // message body is empty | ||
9 | } | ||
diff --git a/src/org/gnunet/transport/HelloUpdateCallback.java b/src/main/java/org/gnunet/transport/HelloUpdateCallback.java index d5253b7..d5253b7 100644 --- a/src/org/gnunet/transport/HelloUpdateCallback.java +++ b/src/main/java/org/gnunet/transport/HelloUpdateCallback.java | |||
diff --git a/src/main/java/org/gnunet/transport/PeerIterateCallback.java b/src/main/java/org/gnunet/transport/PeerIterateCallback.java new file mode 100644 index 0000000..25bea5a --- /dev/null +++ b/src/main/java/org/gnunet/transport/PeerIterateCallback.java | |||
@@ -0,0 +1,12 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.util.PeerIdentity; | ||
4 | |||
5 | /** | ||
6 | * ... | ||
7 | * | ||
8 | * @author Florian Dold | ||
9 | */ | ||
10 | public interface PeerIterateCallback { | ||
11 | void processPeerAddress(PeerIdentity peer, Object hello); | ||
12 | } | ||
diff --git a/src/org/gnunet/transport/RequestConnectMessage.java b/src/main/java/org/gnunet/transport/RequestConnectMessage.java index 63bbc39..63bbc39 100644 --- a/src/org/gnunet/transport/RequestConnectMessage.java +++ b/src/main/java/org/gnunet/transport/RequestConnectMessage.java | |||
diff --git a/src/org/gnunet/transport/StartMessage.java b/src/main/java/org/gnunet/transport/StartMessage.java index 2169565..2169565 100644 --- a/src/org/gnunet/transport/StartMessage.java +++ b/src/main/java/org/gnunet/transport/StartMessage.java | |||
diff --git a/src/org/gnunet/transport/Transport.java b/src/main/java/org/gnunet/transport/Transport.java index 5ccbc77..b9881b6 100644 --- a/src/org/gnunet/transport/Transport.java +++ b/src/main/java/org/gnunet/transport/Transport.java | |||
@@ -2,6 +2,7 @@ package org.gnunet.transport; | |||
2 | 2 | ||
3 | import org.gnunet.hello.HelloMessage; | 3 | import org.gnunet.hello.HelloMessage; |
4 | import org.gnunet.mq.Envelope; | 4 | import org.gnunet.mq.Envelope; |
5 | import org.gnunet.mq.NotifySentHandler; | ||
5 | import org.gnunet.util.*; | 6 | import org.gnunet.util.*; |
6 | 7 | ||
7 | /** | 8 | /** |
@@ -11,8 +12,35 @@ import org.gnunet.util.*; | |||
11 | */ | 12 | */ |
12 | public class Transport { | 13 | public class Transport { |
13 | 14 | ||
14 | public Transport(Configuration cfg) { | 15 | /** |
16 | * Client that connects to the transport service, | ||
17 | */ | ||
18 | private final Client client; | ||
15 | 19 | ||
20 | boolean init_requested; | ||
21 | |||
22 | /** | ||
23 | * Blacklist callback, null if there is no active blacklist | ||
24 | * for this handle. | ||
25 | */ | ||
26 | BlacklistCallback blacklistCallback; | ||
27 | |||
28 | private final class TransportReceiver extends RunaboutMessageReceiver { | ||
29 | @Override | ||
30 | public void handleError() { | ||
31 | client.reconnect(); | ||
32 | // FIXME: complete | ||
33 | } | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Create a handle to the transport service. | ||
38 | * | ||
39 | * @param cfg configuration to use for connecting | ||
40 | */ | ||
41 | public Transport(Configuration cfg) { | ||
42 | client = new Client("transport", cfg); | ||
43 | client.installReceiver(new TransportReceiver()); | ||
16 | } | 44 | } |
17 | 45 | ||
18 | /** | 46 | /** |
@@ -22,16 +50,20 @@ public class Transport { | |||
22 | * @param target who we should try to connect to | 50 | * @param target who we should try to connect to |
23 | * @param cb callback to be called when request was transmitted to transport | 51 | * @param cb callback to be called when request was transmitted to transport |
24 | * service | 52 | * service |
25 | * @return a GNUNET_TRANSPORT_TryConnectHandle handle or | 53 | * @return a handle to cancel the operation |
26 | * NULL on failure (cb will not be called) | ||
27 | */ | 54 | */ |
28 | Cancelable tryConnect(PeerIdentity target, TryConnectCallback cb) { | 55 | Cancelable tryConnect(PeerIdentity target, final TryConnectCallback cb) { |
29 | RequestConnectMessage m = new RequestConnectMessage(); | 56 | RequestConnectMessage m = new RequestConnectMessage(); |
30 | m.peer = target; | 57 | m.peer = target; |
31 | m.reserved = 0; | 58 | m.reserved = 0; |
32 | final Envelope ev = new Envelope(m); | 59 | final Envelope ev = new Envelope(m); |
33 | ev.notifySent(null /* FIXME */); | 60 | ev.notifySent(new NotifySentHandler() { |
34 | //client_mq.send(ev); | 61 | @Override |
62 | public void onSent() { | ||
63 | cb.onDone(); | ||
64 | } | ||
65 | }); | ||
66 | client.send(ev); | ||
35 | 67 | ||
36 | return new Cancelable() { | 68 | return new Cancelable() { |
37 | @Override | 69 | @Override |
@@ -41,6 +73,13 @@ public class Transport { | |||
41 | }; | 73 | }; |
42 | } | 74 | } |
43 | 75 | ||
76 | /** | ||
77 | * ... (discuss first) | ||
78 | */ | ||
79 | public void init(Object receiveCallback, Object notifyConnect, Object notifyDisconnect) { | ||
80 | |||
81 | } | ||
82 | |||
44 | 83 | ||
45 | /** | 84 | /** |
46 | * Obtain the HELLO message for this peer. | 85 | * Obtain the HELLO message for this peer. |
@@ -76,39 +115,40 @@ public class Transport { | |||
76 | /** | 115 | /** |
77 | * Install a blacklist callback. The service will be queried for all | 116 | * Install a blacklist callback. The service will be queried for all |
78 | * existing connections as well as any fresh connections to check if | 117 | * existing connections as well as any fresh connections to check if |
79 | * they are permitted. If the blacklisting callback is unregistered, | 118 | * they are permitted. |
119 | * The blacklist is active until the Transport handle is destroyed. | ||
120 | * When the transport handle that installed the blacklist is destroyed, | ||
80 | * all hosts that were denied in the past will automatically be | 121 | * all hosts that were denied in the past will automatically be |
81 | * whitelisted again. Cancelling the blacklist handle is also the | 122 | * whitelisted again. This is the only way to re-enable |
82 | * only way to re-enable connections from peers that were previously | 123 | * connections from peers that were previously blacklisted. |
83 | * blacklisted. | ||
84 | * | 124 | * |
85 | * @param cfg configuration to use | 125 | * @param blacklistCallback callback to invoke to check if connections are allowed |
86 | * @param cb callback to invoke to check if connections are allowed | ||
87 | * @return NULL on error, otherwise handle for cancellation | ||
88 | */ | 126 | */ |
89 | static Cancelable blacklist(Configuration cfg, | 127 | public void blacklist(BlacklistCallback blacklistCallback) { |
90 | BlacklistCallback cb) { | 128 | if (this.blacklistCallback != null) |
91 | throw new UnsupportedOperationException(); | 129 | throw new AssertionError("there is already a blacklist"); |
130 | if (blacklistCallback == null) | ||
131 | throw new AssertionError("blacklist callback may not be null"); | ||
132 | this.blacklistCallback = blacklistCallback; | ||
133 | client.send(new BlacklistInitMessage()); | ||
92 | } | 134 | } |
93 | 135 | ||
94 | /** | 136 | /** |
95 | * Return all the known addresses for a specific peer or all peers. | 137 | * Return all the known addresses for a specific peer or all peers. |
96 | * Returns continuously all address if one_shot is set to GNUNET_NO | 138 | * Returns continuously all address if one_shot is set to false |
97 | * <p/> | 139 | * <p/> |
98 | * CHANGE: Returns the address(es) that we are currently using for this | 140 | * Returns the address(es) that we are currently using for this |
99 | * peer. Upon completion, the 'AddressLookUpCallback' is called one more | 141 | * peer. Upon completion, the 'AddressLookUpCallback' is called one more |
100 | * time with 'NULL' for the address and the peer. After this, the operation must no | 142 | * time with 'NULL' for the address and the peer. After this, the operation must no |
101 | * longer be explicitly canceled. | 143 | * longer be explicitly canceled. |
102 | * | 144 | * |
103 | * @param cfg configuration to use | ||
104 | * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers | 145 | * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers |
105 | * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL), | 146 | * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL), |
106 | * GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled) | 147 | * GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled) |
107 | * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO) | 148 | * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO) |
108 | * @param peer_address_callback function to call with the results | 149 | * @param peer_address_callback function to call with the results |
109 | */ | 150 | */ |
110 | Cancelable | 151 | Cancelable getActiveAddresses(PeerIdentity peer, int one_shot, |
111 | peer_get_active_addresses(Configuration cfg, PeerIdentity peer, int one_shot, | ||
112 | RelativeTime timeout, PeerIterateCallback peer_address_callback) { | 152 | RelativeTime timeout, PeerIterateCallback peer_address_callback) { |
113 | throw new UnsupportedOperationException(); | 153 | throw new UnsupportedOperationException(); |
114 | } | 154 | } |
diff --git a/src/org/gnunet/transport/TryConnectCallback.java b/src/main/java/org/gnunet/transport/TryConnectCallback.java index 93c0f76..41e743b 100644 --- a/src/org/gnunet/transport/TryConnectCallback.java +++ b/src/main/java/org/gnunet/transport/TryConnectCallback.java | |||
@@ -5,5 +5,6 @@ package org.gnunet.transport; | |||
5 | * | 5 | * |
6 | * @author Florian Dold | 6 | * @author Florian Dold |
7 | */ | 7 | */ |
8 | public class TryConnectCallback { | 8 | public interface TryConnectCallback { |
9 | void onDone(); | ||
9 | } | 10 | } |
diff --git a/src/org/gnunet/util/ATSInformation.java b/src/main/java/org/gnunet/util/ATSInformation.java index b93931b..b93931b 100644 --- a/src/org/gnunet/util/ATSInformation.java +++ b/src/main/java/org/gnunet/util/ATSInformation.java | |||
diff --git a/src/org/gnunet/util/AbsoluteTime.java b/src/main/java/org/gnunet/util/AbsoluteTime.java index 06f41ac..9d22d36 100644 --- a/src/org/gnunet/util/AbsoluteTime.java +++ b/src/main/java/org/gnunet/util/AbsoluteTime.java | |||
@@ -51,7 +51,7 @@ public class AbsoluteTime implements Comparable<AbsoluteTime> { | |||
51 | * @return the current time | 51 | * @return the current time |
52 | */ | 52 | */ |
53 | public static AbsoluteTime now() { | 53 | public static AbsoluteTime now() { |
54 | return new AbsoluteTime(System.currentTimeMillis()); | 54 | return new AbsoluteTime(System.currentTimeMillis() * 1000); |
55 | } | 55 | } |
56 | 56 | ||
57 | public AbsoluteTime(final long abs_value_us) { | 57 | public AbsoluteTime(final long abs_value_us) { |
@@ -264,6 +264,6 @@ public class AbsoluteTime implements Comparable<AbsoluteTime> { | |||
264 | } | 264 | } |
265 | 265 | ||
266 | public Date toDate() { | 266 | public Date toDate() { |
267 | return new Date(abs_value_us); | 267 | return new Date(abs_value_us / 1000); |
268 | } | 268 | } |
269 | } | 269 | } |
diff --git a/src/org/gnunet/util/AbsoluteTimeMessage.java b/src/main/java/org/gnunet/util/AbsoluteTimeMessage.java index f46d577..f46d577 100644 --- a/src/org/gnunet/util/AbsoluteTimeMessage.java +++ b/src/main/java/org/gnunet/util/AbsoluteTimeMessage.java | |||
diff --git a/src/org/gnunet/util/Cancelable.java b/src/main/java/org/gnunet/util/Cancelable.java index 173f4c0..173f4c0 100644 --- a/src/org/gnunet/util/Cancelable.java +++ b/src/main/java/org/gnunet/util/Cancelable.java | |||
diff --git a/src/org/gnunet/util/Client.java b/src/main/java/org/gnunet/util/Client.java index 182c797..18c206b 100644 --- a/src/org/gnunet/util/Client.java +++ b/src/main/java/org/gnunet/util/Client.java | |||
@@ -20,40 +20,46 @@ | |||
20 | package org.gnunet.util; | 20 | package org.gnunet.util; |
21 | 21 | ||
22 | import com.google.common.base.Optional; | 22 | import com.google.common.base.Optional; |
23 | import org.gnunet.mq.Envelope; | ||
24 | import org.gnunet.mq.MessageQueue; | ||
25 | import org.gnunet.statistics.Statistics; | ||
23 | import org.slf4j.Logger; | 26 | import org.slf4j.Logger; |
24 | import org.slf4j.LoggerFactory; | 27 | import org.slf4j.LoggerFactory; |
25 | 28 | ||
26 | 29 | ||
27 | /** | 30 | /** |
28 | * A connection to a gnunet service. | 31 | * A connection to a GNUnet service. |
29 | * | 32 | * |
30 | * Wraps a Connection, and is responsible for waiting until the underlying connection has been made | 33 | * Wraps a Connection, and is responsible for waiting until the underlying connection has been made |
31 | * and allows reconnects. | 34 | * and allows reconnects. |
32 | */ | 35 | */ |
33 | public class Client { | 36 | public class Client extends MessageQueue { |
34 | private static final Logger logger = LoggerFactory | 37 | private static final Logger logger = LoggerFactory |
35 | .getLogger(Client.class); | 38 | .getLogger(Client.class); |
36 | 39 | ||
37 | /** | 40 | /** |
38 | * Underlying connection to the service. | 41 | * Underlying connection to the service. |
42 | * May be NULL if the client is currently not connected. | ||
39 | */ | 43 | */ |
40 | private Connection connection; | 44 | private Connection connection; |
41 | 45 | ||
42 | /** | 46 | /** |
43 | * Host this client should be connected to. | 47 | * Host this client should be connected to. |
44 | */ | 48 | */ |
45 | private final int port; | ||
46 | private final String hostname; | 49 | private final String hostname; |
47 | 50 | ||
48 | /** | 51 | /** |
52 | * Port of the host the client should connect to. | ||
53 | */ | ||
54 | private final int port; | ||
55 | |||
56 | /** | ||
49 | * Initial value for connectBackoff. | 57 | * Initial value for connectBackoff. |
50 | * | ||
51 | */ | 58 | */ |
52 | private static final RelativeTime INITAL_BACKOFF = RelativeTime.MILLISECOND.multiply(5); | 59 | private static final RelativeTime INITAL_BACKOFF = RelativeTime.MILLISECOND.multiply(5); |
53 | 60 | ||
54 | /** | 61 | /** |
55 | * Maximum value for connectBackoff. | 62 | * Maximum value for connectBackoff. |
56 | * | ||
57 | */ | 63 | */ |
58 | private static final RelativeTime MAX_BACKOFF = RelativeTime.SECOND.multiply(5); | 64 | private static final RelativeTime MAX_BACKOFF = RelativeTime.SECOND.multiply(5); |
59 | 65 | ||
@@ -71,12 +77,25 @@ public class Client { | |||
71 | private boolean notifyTransmitReadyDelayed; | 77 | private boolean notifyTransmitReadyDelayed; |
72 | 78 | ||
73 | /** | 79 | /** |
74 | * When notifyTransmitReadyDelayed is true, this can be used to cancel the task | 80 | * When notifyTransmitReadyDelayed is true, This can be used to cancel the task |
75 | * waiting for the connection to be established. | 81 | * waiting for the connection to be established. |
76 | */ | 82 | */ |
77 | private Cancelable delayedNotifyTransmitHandle; | 83 | private Cancelable delayedNotifyTransmitHandle; |
78 | 84 | ||
79 | /** | 85 | /** |
86 | * Currently installed persistent receiver. | ||
87 | * Will receive all messages sent to the client. | ||
88 | */ | ||
89 | private RunaboutMessageReceiver receiver; | ||
90 | |||
91 | private boolean receiver_active; | ||
92 | |||
93 | /** | ||
94 | * Handle to cancel the message currently submitted in the queue, | ||
95 | */ | ||
96 | private Cancelable currentSubmit; | ||
97 | |||
98 | /** | ||
80 | * Create a connection to a service. | 99 | * Create a connection to a service. |
81 | * | 100 | * |
82 | * @param serviceName name of the service | 101 | * @param serviceName name of the service |
@@ -123,7 +142,7 @@ public class Client { | |||
123 | * @param timeout deadline after which MessageReceiver.deadline will be called | 142 | * @param timeout deadline after which MessageReceiver.deadline will be called |
124 | * @param receiver MessageReceiver that is responsible for the received message | 143 | * @param receiver MessageReceiver that is responsible for the received message |
125 | */ | 144 | */ |
126 | public Cancelable receive(RelativeTime timeout, MessageReceiver receiver) { | 145 | public Cancelable receiveOne(RelativeTime timeout, MessageReceiver receiver) { |
127 | return connection.receive(timeout, receiver); | 146 | return connection.receive(timeout, receiver); |
128 | } | 147 | } |
129 | 148 | ||
@@ -161,6 +180,7 @@ public class Client { | |||
161 | public void cont(boolean success) { | 180 | public void cont(boolean success) { |
162 | delayedNotifyTransmitHandle = null; | 181 | delayedNotifyTransmitHandle = null; |
163 | if (success) { | 182 | if (success) { |
183 | activateReceiver(); | ||
164 | notifyTransmitReadyDelayed = false; | 184 | notifyTransmitReadyDelayed = false; |
165 | delayedNotifyTransmitHandle = connection.notifyTransmitReady(0, timeout, new MessageTransmitter() { | 185 | delayedNotifyTransmitHandle = connection.notifyTransmitReady(0, timeout, new MessageTransmitter() { |
166 | @Override | 186 | @Override |
@@ -176,6 +196,7 @@ public class Client { | |||
176 | } | 196 | } |
177 | }); | 197 | }); |
178 | } else { | 198 | } else { |
199 | logger.debug("connect timed out, trying again"); | ||
179 | if (deadline.isDue()) { | 200 | if (deadline.isDue()) { |
180 | transmitter.handleError(); | 201 | transmitter.handleError(); |
181 | } else { | 202 | } else { |
@@ -236,7 +257,6 @@ public class Client { | |||
236 | return transmitWhenReady(RelativeTime.FOREVER, message, cont); | 257 | return transmitWhenReady(RelativeTime.FOREVER, message, cont); |
237 | } | 258 | } |
238 | 259 | ||
239 | |||
240 | public final void reconnect() { | 260 | public final void reconnect() { |
241 | if (connection != null) { | 261 | if (connection != null) { |
242 | connection.disconnect(); | 262 | connection.disconnect(); |
@@ -244,7 +264,6 @@ public class Client { | |||
244 | connection = new Connection(hostname, port); | 264 | connection = new Connection(hostname, port); |
245 | } | 265 | } |
246 | 266 | ||
247 | |||
248 | /** | 267 | /** |
249 | * Disconnect from the service. Cancel all pending receive/transmit requests. | 268 | * Disconnect from the service. Cancel all pending receive/transmit requests. |
250 | */ | 269 | */ |
@@ -259,4 +278,52 @@ public class Client { | |||
259 | public boolean isConnected() { | 278 | public boolean isConnected() { |
260 | return (connection != null) && connection.isConnected(); | 279 | return (connection != null) && connection.isConnected(); |
261 | } | 280 | } |
281 | |||
282 | @Override | ||
283 | protected void submit(Envelope ev) { | ||
284 | currentSubmit = transmitWhenReady(RelativeTime.FOREVER, ev.message, new Continuation() { | ||
285 | @Override | ||
286 | public void cont(boolean success) { | ||
287 | currentSubmit = null; | ||
288 | reportMessageSent(); | ||
289 | } | ||
290 | }); | ||
291 | } | ||
292 | |||
293 | @Override | ||
294 | protected void retract() { | ||
295 | if (currentSubmit == null) | ||
296 | throw new AssertionError(); | ||
297 | currentSubmit.cancel(); | ||
298 | currentSubmit = null; | ||
299 | } | ||
300 | |||
301 | private void activateReceiver() { | ||
302 | if (receiver_active || receiver == null) | ||
303 | return; | ||
304 | final MessageReceiver proxyReceiver = new MessageReceiver() { | ||
305 | @Override | ||
306 | public void process(GnunetMessage.Body msg) { | ||
307 | Client.this.receiver.process(msg); | ||
308 | if (connection != null && connection.isConnected()) | ||
309 | connection.receive(RelativeTime.FOREVER, this); | ||
310 | else | ||
311 | receiver_active = false; | ||
312 | } | ||
313 | |||
314 | @Override | ||
315 | public void handleError() { | ||
316 | Client.this.receiver.handleError(); | ||
317 | receiver_active = false; | ||
318 | } | ||
319 | }; | ||
320 | connection.receive(RelativeTime.FOREVER, proxyReceiver); | ||
321 | receiver_active = true; | ||
322 | } | ||
323 | |||
324 | public void installReceiver(RunaboutMessageReceiver receiver) { | ||
325 | this.receiver = receiver; | ||
326 | if (connection != null && connection.isConnected()) | ||
327 | activateReceiver(); | ||
328 | } | ||
262 | } | 329 | } |
diff --git a/src/org/gnunet/util/Configuration.java b/src/main/java/org/gnunet/util/Configuration.java index 5cf950c..ea67e1b 100644 --- a/src/org/gnunet/util/Configuration.java +++ b/src/main/java/org/gnunet/util/Configuration.java | |||
@@ -346,6 +346,17 @@ public class Configuration { | |||
346 | } | 346 | } |
347 | } | 347 | } |
348 | 348 | ||
349 | public String serialize() { | ||
350 | StringBuffer buf = new StringBuffer(); | ||
351 | for (Map.Entry<String, Map<String,String>> section : sections.rowMap().entrySet()) { | ||
352 | buf.append("[" + section.getKey() + "]\n"); | ||
353 | for (Map.Entry<String, String> option : section.getValue().entrySet()) { | ||
354 | buf.append(option.getKey() + " = " + option.getValue() + "\n"); | ||
355 | } | ||
356 | } | ||
357 | return buf.toString(); | ||
358 | } | ||
359 | |||
349 | 360 | ||
350 | public void loadDefaults() { | 361 | public void loadDefaults() { |
351 | Collection<File> dirs = new ArrayList<File>(5); | 362 | Collection<File> dirs = new ArrayList<File>(5); |
diff --git a/src/org/gnunet/util/Connection.java b/src/main/java/org/gnunet/util/Connection.java index f969cbe..1fd1458 100644 --- a/src/org/gnunet/util/Connection.java +++ b/src/main/java/org/gnunet/util/Connection.java | |||
@@ -48,6 +48,10 @@ public class Connection { | |||
48 | */ | 48 | */ |
49 | private SocketChannel connectionChannel = null; | 49 | private SocketChannel connectionChannel = null; |
50 | 50 | ||
51 | /** | ||
52 | * The list of all address probes. | ||
53 | * Each address probe tries to connect via a different address. | ||
54 | */ | ||
51 | private List<AddressProbe> addressProbes = null; | 55 | private List<AddressProbe> addressProbes = null; |
52 | 56 | ||
53 | /** | 57 | /** |
@@ -73,11 +77,11 @@ public class Connection { | |||
73 | */ | 77 | */ |
74 | private ByteBuffer recvBuffer = ByteBuffer.allocate(GnunetMessage.Header.SIZE); | 78 | private ByteBuffer recvBuffer = ByteBuffer.allocate(GnunetMessage.Header.SIZE); |
75 | 79 | ||
76 | |||
77 | /** | 80 | /** |
78 | * The handle for the current transmission. Writes data to the socket. | 81 | * The handle for the current transmission. Writes data to the socket. |
79 | */ | 82 | */ |
80 | private TransmitHelper currentTransmitHelper = null; | 83 | private TransmitHelper currentTransmitHelper = null; |
84 | |||
81 | /** | 85 | /** |
82 | * The handle for the next transmission. The next transmission will become the current | 86 | * The handle for the next transmission. The next transmission will become the current |
83 | * transmission once the current transmission has completed. | 87 | * transmission once the current transmission has completed. |
@@ -173,7 +177,6 @@ public class Connection { | |||
173 | recvBuffer.flip(); | 177 | recvBuffer.flip(); |
174 | 178 | ||
175 | boolean found = true; | 179 | boolean found = true; |
176 | |||
177 | Class unionClass = null; | 180 | Class unionClass = null; |
178 | 181 | ||
179 | try { | 182 | try { |
@@ -541,7 +544,6 @@ public class Connection { | |||
541 | 544 | ||
542 | 545 | ||
543 | public interface ReceiveHandle extends Cancelable { | 546 | public interface ReceiveHandle extends Cancelable { |
544 | public void cancel(); | ||
545 | } | 547 | } |
546 | 548 | ||
547 | /** | 549 | /** |
@@ -691,5 +693,4 @@ public class Connection { | |||
691 | connectionChannel = null; | 693 | connectionChannel = null; |
692 | } | 694 | } |
693 | } | 695 | } |
694 | |||
695 | } | 696 | } |
diff --git a/src/org/gnunet/util/Continuation.java b/src/main/java/org/gnunet/util/Continuation.java index e1027c0..e1027c0 100644 --- a/src/org/gnunet/util/Continuation.java +++ b/src/main/java/org/gnunet/util/Continuation.java | |||
diff --git a/src/org/gnunet/util/GnunetMessage.java b/src/main/java/org/gnunet/util/GnunetMessage.java index 89c7a80..89c7a80 100644 --- a/src/org/gnunet/util/GnunetMessage.java +++ b/src/main/java/org/gnunet/util/GnunetMessage.java | |||
diff --git a/src/org/gnunet/util/HashCode.java b/src/main/java/org/gnunet/util/HashCode.java index 0c2790f..0c2790f 100644 --- a/src/org/gnunet/util/HashCode.java +++ b/src/main/java/org/gnunet/util/HashCode.java | |||
diff --git a/src/main/java/org/gnunet/util/Helper.java b/src/main/java/org/gnunet/util/Helper.java new file mode 100644 index 0000000..e8c1723 --- /dev/null +++ b/src/main/java/org/gnunet/util/Helper.java | |||
@@ -0,0 +1,208 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import org.gnunet.construct.Construct; | ||
4 | import org.gnunet.construct.Message; | ||
5 | import org.gnunet.construct.MessageLoader; | ||
6 | import org.gnunet.construct.ProtocolViolationException; | ||
7 | import org.gnunet.mq.Envelope; | ||
8 | import org.gnunet.mq.MessageQueue; | ||
9 | |||
10 | import java.io.IOError; | ||
11 | import java.io.IOException; | ||
12 | import java.nio.Buffer; | ||
13 | import java.nio.ByteBuffer; | ||
14 | import java.nio.channels.Channels; | ||
15 | import java.nio.channels.ReadableByteChannel; | ||
16 | import java.util.EnumSet; | ||
17 | import java.util.LinkedList; | ||
18 | import java.util.List; | ||
19 | |||
20 | /** | ||
21 | * Process that we can communicate with standard GNUnet messages over stdin/stdout. | ||
22 | */ | ||
23 | public class Helper extends MessageQueue { | ||
24 | |||
25 | private final ProcessBuilder processBuilder; | ||
26 | private final RunaboutMessageReceiver receiver; | ||
27 | private Process process; | ||
28 | |||
29 | private volatile GnunetMessage.Body writeMessage; | ||
30 | |||
31 | private final class WriteThread implements Runnable { | ||
32 | @Override | ||
33 | public void run() { | ||
34 | GnunetMessage.Body msg; | ||
35 | while (true) { | ||
36 | synchronized (Helper.this) { | ||
37 | while (writeMessage == null) { | ||
38 | try { | ||
39 | wait(); | ||
40 | } catch (InterruptedException e) { | ||
41 | // do nothing | ||
42 | } | ||
43 | } | ||
44 | // we now have a message we can send | ||
45 | msg = writeMessage; | ||
46 | writeMessage = null; | ||
47 | // somebody can set the next send message | ||
48 | } | ||
49 | byte[] data = Construct.toBinary(GnunetMessage.fromBody(msg)); | ||
50 | try { | ||
51 | process.getOutputStream().write(data); | ||
52 | } catch (IOException e) { | ||
53 | // fixme: what now? | ||
54 | } | ||
55 | Scheduler.addContinuation(new Scheduler.Task() { | ||
56 | @Override | ||
57 | public void run(Scheduler.RunContext ctx) { | ||
58 | reportMessageSent(); | ||
59 | } | ||
60 | }, EnumSet.noneOf(Scheduler.Reason.class)); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | private final class ReadThread implements Runnable { | ||
66 | private ByteBuffer buffer; | ||
67 | ReadableByteChannel channel; | ||
68 | |||
69 | private void fillBuffer() { | ||
70 | while (buffer.hasRemaining()) { | ||
71 | try { | ||
72 | channel.read(buffer); | ||
73 | } catch (IOException e) { | ||
74 | // FIXME | ||
75 | return; | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | |||
80 | private void scheduleInvokeReceiver(final GnunetMessage.Body body) { | ||
81 | Scheduler.addContinuation(new Scheduler.Task() { | ||
82 | @Override | ||
83 | public void run(Scheduler.RunContext ctx) { | ||
84 | receiver.process(body); | ||
85 | } | ||
86 | }, EnumSet.noneOf(Scheduler.Reason.class)); | ||
87 | |||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public void run() { | ||
92 | // allocate just enough for the message header | ||
93 | buffer = ByteBuffer.allocate(4); | ||
94 | channel = Channels.newChannel(process.getInputStream()); | ||
95 | while (true) { | ||
96 | buffer.clear(); | ||
97 | buffer.limit(4); | ||
98 | fillBuffer(); | ||
99 | buffer.rewind(); | ||
100 | GnunetMessage.Header msgh = Construct.parseAs(buffer, GnunetMessage.Header.class); | ||
101 | if (msgh.messageSize > GnunetMessage.Header.SIZE) { | ||
102 | if (buffer.capacity() < msgh.messageSize) { | ||
103 | ByteBuffer newBuf = ByteBuffer.allocate(msgh.messageSize); | ||
104 | buffer.flip(); | ||
105 | newBuf.put(buffer); | ||
106 | buffer = newBuf; | ||
107 | } | ||
108 | buffer.limit(msgh.messageSize); | ||
109 | fillBuffer(); | ||
110 | } | ||
111 | // we now have a complete message | ||
112 | // prepare for reading again | ||
113 | buffer.flip(); | ||
114 | |||
115 | boolean found = true; | ||
116 | Class unionClass = null; | ||
117 | |||
118 | try { | ||
119 | unionClass = MessageLoader.getUnionClass(GnunetMessage.Body.class, msgh.messageType); | ||
120 | } catch (ProtocolViolationException e) { | ||
121 | found = false; | ||
122 | } | ||
123 | if (found) { | ||
124 | GnunetMessage msg; | ||
125 | msg = Construct.parseAs(buffer, GnunetMessage.class); | ||
126 | scheduleInvokeReceiver(msg.body); | ||
127 | } else { | ||
128 | UnknownMessageBody b = new UnknownMessageBody(); | ||
129 | b.id = msgh.messageType; | ||
130 | scheduleInvokeReceiver(b); | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | |||
136 | |||
137 | public Helper(boolean withControlPipe, String binaryName, List<String> argv, | ||
138 | RunaboutMessageReceiver receiver) { | ||
139 | this.receiver = receiver; | ||
140 | List<String> command = new LinkedList<String>(); | ||
141 | if (binaryName == null) { | ||
142 | throw new AssertionError(); | ||
143 | } | ||
144 | command.add(binaryName); | ||
145 | if (argv != null) | ||
146 | command.addAll(argv); | ||
147 | processBuilder = new ProcessBuilder(command); | ||
148 | try { | ||
149 | process = processBuilder.start(); | ||
150 | } catch (IOException e) { | ||
151 | throw new IOError(e); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Sends termination signal to the helper process. The helper process is not | ||
157 | * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process. | ||
158 | * | ||
159 | * @param softkill if GNUNET_YES, signals termination by closing the helper's | ||
160 | * stdin; GNUNET_NO to signal termination by sending SIGTERM to helper | ||
161 | * @return true on success, false on failure | ||
162 | */ | ||
163 | public boolean kill(boolean softkill) { | ||
164 | if (softkill) { | ||
165 | try { | ||
166 | process.getInputStream().close(); | ||
167 | } catch (IOException e) { | ||
168 | return false; | ||
169 | } | ||
170 | return true; | ||
171 | } | ||
172 | process.destroy(); | ||
173 | return true; | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * Reap the helper process. This call is blocking(!). The helper process | ||
178 | * should either be sent a termination signal before or should be dead before | ||
179 | * calling this function | ||
180 | * | ||
181 | * @return true on success, false on failure | ||
182 | */ | ||
183 | public boolean waitFor() { | ||
184 | try { | ||
185 | process.waitFor(); | ||
186 | } catch (InterruptedException e) { | ||
187 | return false; | ||
188 | } | ||
189 | return true; | ||
190 | } | ||
191 | |||
192 | @Override | ||
193 | protected void submit(Envelope ev) { | ||
194 | synchronized (this) { | ||
195 | if (writeMessage != null) | ||
196 | throw new AssertionError("message queue not implemented correctly"); | ||
197 | writeMessage = ev.message; | ||
198 | notifyAll(); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | @Override | ||
203 | protected void retract() { | ||
204 | synchronized (this) { | ||
205 | writeMessage = null; | ||
206 | } | ||
207 | } | ||
208 | } | ||
diff --git a/src/org/gnunet/util/MessageReceiver.java b/src/main/java/org/gnunet/util/MessageReceiver.java index 548695f..548695f 100644 --- a/src/org/gnunet/util/MessageReceiver.java +++ b/src/main/java/org/gnunet/util/MessageReceiver.java | |||
diff --git a/src/org/gnunet/util/MessageTransmitter.java b/src/main/java/org/gnunet/util/MessageTransmitter.java index 54a6555..54a6555 100644 --- a/src/org/gnunet/util/MessageTransmitter.java +++ b/src/main/java/org/gnunet/util/MessageTransmitter.java | |||
diff --git a/src/org/gnunet/util/PeerIdentity.java b/src/main/java/org/gnunet/util/PeerIdentity.java index 46a67cd..46a67cd 100644 --- a/src/org/gnunet/util/PeerIdentity.java +++ b/src/main/java/org/gnunet/util/PeerIdentity.java | |||
diff --git a/src/org/gnunet/util/Program.java b/src/main/java/org/gnunet/util/Program.java index c54322b..bd413b9 100644 --- a/src/org/gnunet/util/Program.java +++ b/src/main/java/org/gnunet/util/Program.java | |||
@@ -78,6 +78,8 @@ public abstract class Program { | |||
78 | 78 | ||
79 | private final String[] args; | 79 | private final String[] args; |
80 | 80 | ||
81 | private int returnValue = 0; | ||
82 | |||
81 | 83 | ||
82 | /** | 84 | /** |
83 | * A program with the desired environment for a gnunet utility. | 85 | * A program with the desired environment for a gnunet utility. |
@@ -169,6 +171,10 @@ public abstract class Program { | |||
169 | return "development version of gnunet-java"; | 171 | return "development version of gnunet-java"; |
170 | } | 172 | } |
171 | 173 | ||
174 | final protected void setReturnValue(int x) { | ||
175 | returnValue = x; | ||
176 | } | ||
177 | |||
172 | /** | 178 | /** |
173 | * Start the Program as the initial task of the Scheduler. | 179 | * Start the Program as the initial task of the Scheduler. |
174 | */ | 180 | */ |
@@ -198,6 +204,8 @@ public abstract class Program { | |||
198 | } | 204 | } |
199 | }); | 205 | }); |
200 | } | 206 | } |
207 | |||
208 | System.exit(returnValue); | ||
201 | } | 209 | } |
202 | 210 | ||
203 | /** | 211 | /** |
diff --git a/src/org/gnunet/util/RelativeTime.java b/src/main/java/org/gnunet/util/RelativeTime.java index 0faedfc..512ed01 100644 --- a/src/org/gnunet/util/RelativeTime.java +++ b/src/main/java/org/gnunet/util/RelativeTime.java | |||
@@ -59,6 +59,14 @@ public final class RelativeTime implements Comparable<RelativeTime> { | |||
59 | this.rel_value_us = abs_value; | 59 | this.rel_value_us = abs_value; |
60 | } | 60 | } |
61 | 61 | ||
62 | public static RelativeTime fromMilliseconds(final long ms) { | ||
63 | return new RelativeTime(ms * 1000); | ||
64 | } | ||
65 | |||
66 | public static RelativeTime fromMicroseconds(final long us) { | ||
67 | return new RelativeTime(us); | ||
68 | } | ||
69 | |||
62 | /** | 70 | /** |
63 | * Add relative times together. | 71 | * Add relative times together. |
64 | * | 72 | * |
diff --git a/src/org/gnunet/util/RelativeTimeMessage.java b/src/main/java/org/gnunet/util/RelativeTimeMessage.java index ac4e66c..ac4e66c 100644 --- a/src/org/gnunet/util/RelativeTimeMessage.java +++ b/src/main/java/org/gnunet/util/RelativeTimeMessage.java | |||
diff --git a/src/org/gnunet/util/Resolver.java b/src/main/java/org/gnunet/util/Resolver.java index 1d8ec87..22047aa 100644 --- a/src/org/gnunet/util/Resolver.java +++ b/src/main/java/org/gnunet/util/Resolver.java | |||
@@ -278,7 +278,7 @@ public class Resolver { | |||
278 | rh.transmitTask = null; | 278 | rh.transmitTask = null; |
279 | 279 | ||
280 | logger.debug("recv in notifyTransmitReady cb"); | 280 | logger.debug("recv in notifyTransmitReady cb"); |
281 | rh.receiveTask = client.receive(deadline.getRemaining(), new MessageReceiver() { | 281 | rh.receiveTask = client.receiveOne(deadline.getRemaining(), new MessageReceiver() { |
282 | @Override | 282 | @Override |
283 | public void process(GnunetMessage.Body msg) { | 283 | public void process(GnunetMessage.Body msg) { |
284 | rh.receiveTask = null; | 284 | rh.receiveTask = null; |
@@ -294,7 +294,7 @@ public class Resolver { | |||
294 | } | 294 | } |
295 | 295 | ||
296 | rh.cb.onAddress(in_addr); | 296 | rh.cb.onAddress(in_addr); |
297 | rh.receiveTask = client.receive(deadline.getRemaining(), this); | 297 | rh.receiveTask = client.receiveOne(deadline.getRemaining(), this); |
298 | } catch (UnknownHostException e) { | 298 | } catch (UnknownHostException e) { |
299 | throw new ProtocolViolationException("malformed address"); | 299 | throw new ProtocolViolationException("malformed address"); |
300 | } | 300 | } |
diff --git a/src/org/gnunet/util/RunaboutMessageReceiver.java b/src/main/java/org/gnunet/util/RunaboutMessageReceiver.java index 2a0f067..2a0f067 100644 --- a/src/org/gnunet/util/RunaboutMessageReceiver.java +++ b/src/main/java/org/gnunet/util/RunaboutMessageReceiver.java | |||
diff --git a/src/org/gnunet/util/RunaboutUtil.java b/src/main/java/org/gnunet/util/RunaboutUtil.java index a82dc0a..a82dc0a 100644 --- a/src/org/gnunet/util/RunaboutUtil.java +++ b/src/main/java/org/gnunet/util/RunaboutUtil.java | |||
diff --git a/src/org/gnunet/util/Scheduler.java b/src/main/java/org/gnunet/util/Scheduler.java index 6998633..78ea288 100644 --- a/src/org/gnunet/util/Scheduler.java +++ b/src/main/java/org/gnunet/util/Scheduler.java | |||
@@ -41,8 +41,8 @@ public class Scheduler { | |||
41 | // only valid while a task is executing | 41 | // only valid while a task is executing |
42 | private static TaskConfiguration activeTask = null; | 42 | private static TaskConfiguration activeTask = null; |
43 | 43 | ||
44 | // number of tasks in the ready queue | 44 | // cumulative number of tasks in the ready lists |
45 | private static int readyCount = 0; | 45 | private static volatile int readyCount = 0; |
46 | 46 | ||
47 | // for every priority, there is a list of tasks that is definitely ready to run | 47 | // for every priority, there is a list of tasks that is definitely ready to run |
48 | @SuppressWarnings("unchecked") | 48 | @SuppressWarnings("unchecked") |
@@ -82,7 +82,6 @@ public class Scheduler { | |||
82 | private static boolean scheduler_running = false; | 82 | private static boolean scheduler_running = false; |
83 | 83 | ||
84 | 84 | ||
85 | |||
86 | // tasks that are waiting for an event, which are executed anyway after the deadline has occurred | 85 | // tasks that are waiting for an event, which are executed anyway after the deadline has occurred |
87 | final private static Queue<TaskConfiguration> pending = new PriorityQueue<TaskConfiguration>(5, new Comparator | 86 | final private static Queue<TaskConfiguration> pending = new PriorityQueue<TaskConfiguration>(5, new Comparator |
88 | <TaskConfiguration>() { | 87 | <TaskConfiguration>() { |
@@ -94,6 +93,21 @@ public class Scheduler { | |||
94 | 93 | ||
95 | 94 | ||
96 | /** | 95 | /** |
96 | * Reset the scheduler forcefully. | ||
97 | * Intended to be used internally in the Scheduler, as well as in test teardown. | ||
98 | */ | ||
99 | public static void forceReset() { | ||
100 | scheduler_running = false; | ||
101 | readyCount = 0; | ||
102 | activeTask = null; | ||
103 | for (int i = 0; i < Priority.numberOfPriorities; ++i) { | ||
104 | readyLists[i] = new LinkedList<TaskConfiguration>(); | ||
105 | } | ||
106 | pending.clear(); | ||
107 | } | ||
108 | |||
109 | |||
110 | /** | ||
97 | * Priority for Tasks. | 111 | * Priority for Tasks. |
98 | */ | 112 | */ |
99 | public enum Priority { | 113 | public enum Priority { |
@@ -149,26 +163,17 @@ public class Scheduler { | |||
149 | /** | 163 | /** |
150 | * Create a TaskIdentifier. | 164 | * Create a TaskIdentifier. |
151 | * | 165 | * |
166 | * @param delay when will the task be run? | ||
167 | * may be null to indicate that this task may not be run | ||
168 | * (but only queued directly) | ||
152 | * @param task task to run with this TaskIdentifier | 169 | * @param task task to run with this TaskIdentifier |
153 | */ | 170 | */ |
154 | TaskConfiguration(RelativeTime delay, Task task) { | 171 | TaskConfiguration(RelativeTime delay, Task task) { |
155 | this.task = task; | 172 | this.task = task; |
156 | this.deadline = delay.toAbsolute(); | 173 | if (delay == null) |
157 | } | 174 | this.deadline = null; |
158 | 175 | else | |
159 | /** | 176 | this.deadline = delay.toAbsolute(); |
160 | * Create a light-weight task identifier that is not registered as pending in the Scheduler, | ||
161 | * used for continuations. | ||
162 | * | ||
163 | * @param t task | ||
164 | * @param ctx the RunContext | ||
165 | */ | ||
166 | TaskConfiguration(Task t, RunContext ctx) { | ||
167 | this.task = t; | ||
168 | this.ctx = ctx; | ||
169 | this.deadline = AbsoluteTime.ZERO; | ||
170 | this.priority = (activeTask == null) ? Priority.DEFAULT : activeTask.priority; | ||
171 | this.lifeness = true; | ||
172 | } | 177 | } |
173 | 178 | ||
174 | private void addChannelEvent(SelectableChannel channel, int eventType) { | 179 | private void addChannelEvent(SelectableChannel channel, int eventType) { |
@@ -232,6 +237,8 @@ public class Scheduler { | |||
232 | } | 237 | } |
233 | 238 | ||
234 | public Cancelable schedule() { | 239 | public Cancelable schedule() { |
240 | if (this.deadline == null) | ||
241 | throw new AssertionError("a task without deadline may not be scheduled"); | ||
235 | if (priority == null) { | 242 | if (priority == null) { |
236 | if (activeTask != null) { | 243 | if (activeTask != null) { |
237 | priority = activeTask.priority; | 244 | priority = activeTask.priority; |
@@ -280,11 +287,11 @@ public class Scheduler { | |||
280 | * Run the task regardless of any prerequisites, before any other task of | 287 | * Run the task regardless of any prerequisites, before any other task of |
281 | * the same priority. | 288 | * the same priority. |
282 | */ | 289 | */ |
283 | public static void addContinuation(Task task, | 290 | public static synchronized void addContinuation(Task task, EnumSet<Reason> reasons) { |
284 | EnumSet<Reason> reasons) { | 291 | TaskConfiguration t = new TaskConfiguration(null, task); |
285 | RunContext ctx = new RunContext(); | 292 | t.ctx.reasons = reasons; |
286 | ctx.reasons = reasons; | 293 | t.priority = Priority.DEFAULT; |
287 | queueReady(new TaskConfiguration(task, ctx)); | 294 | queueReady(t); |
288 | } | 295 | } |
289 | 296 | ||
290 | /** | 297 | /** |
@@ -299,7 +306,6 @@ public class Scheduler { | |||
299 | return addDelayed(RelativeTime.ZERO, task); | 306 | return addDelayed(RelativeTime.ZERO, task); |
300 | } | 307 | } |
301 | 308 | ||
302 | |||
303 | /** | 309 | /** |
304 | * Add a task to run after the specified delay. | 310 | * Add a task to run after the specified delay. |
305 | * | 311 | * |
@@ -360,11 +366,10 @@ public class Scheduler { | |||
360 | * | 366 | * |
361 | * @param tid TaskIdentifier of the ready task | 367 | * @param tid TaskIdentifier of the ready task |
362 | */ | 368 | */ |
363 | private static void queueReady(TaskConfiguration tid) { | 369 | private static synchronized void queueReady(TaskConfiguration tid) { |
364 | int idx = tid.priority.ordinal(); | 370 | int idx = tid.priority.ordinal(); |
365 | readyLists[idx].add(tid); | 371 | readyLists[idx].add(tid); |
366 | readyCount++; | 372 | readyCount++; |
367 | |||
368 | pending.remove(tid); | 373 | pending.remove(tid); |
369 | } | 374 | } |
370 | 375 | ||
@@ -405,15 +410,21 @@ public class Scheduler { | |||
405 | subscribers[eventType].ctx.reasons.add(eventToReason[eventType]); | 410 | subscribers[eventType].ctx.reasons.add(eventToReason[eventType]); |
406 | } | 411 | } |
407 | 412 | ||
413 | /** | ||
414 | * Select on channels and queue tasks that become executable. | ||
415 | * | ||
416 | * @param timeout timeout for select | ||
417 | */ | ||
408 | private static void handleSelect(RelativeTime timeout) { | 418 | private static void handleSelect(RelativeTime timeout) { |
419 | long timeout_ms = timeout.getMicroseconds() / 1000; | ||
409 | try { | 420 | try { |
410 | // selector.select(0) would block indefinitely (counter-intuitive, java's fault) | 421 | // selector.select(0) would block indefinitely (counter-intuitive, java's fault) |
411 | if (timeout.getMicroseconds() == 0) { | 422 | if (timeout_ms == 0) { |
412 | selector.selectNow(); | 423 | selector.selectNow(); |
413 | } else if (timeout.isForever()) { | 424 | } else if (timeout.isForever()) { |
414 | selector.select(0); | 425 | selector.select(0); |
415 | } else { | 426 | } else { |
416 | selector.select(timeout.getMicroseconds()); | 427 | selector.select(timeout_ms); |
417 | } | 428 | } |
418 | } catch (IOException e) { | 429 | } catch (IOException e) { |
419 | throw new IOError(e); | 430 | throw new IOError(e); |
@@ -472,13 +483,7 @@ public class Scheduler { | |||
472 | logger.info("cleaning up after scheduler ran"); | 483 | logger.info("cleaning up after scheduler ran"); |
473 | // ensure that after run returns, the scheduler is in its initial state, | 484 | // ensure that after run returns, the scheduler is in its initial state, |
474 | // even though there was an exception (e.g. after a test case that expects an exception) | 485 | // even though there was an exception (e.g. after a test case that expects an exception) |
475 | scheduler_running = false; | 486 | forceReset(); |
476 | readyCount = 0; | ||
477 | activeTask = null; | ||
478 | for (int i = 0; i < Priority.numberOfPriorities; ++i) { | ||
479 | readyLists[i] = new LinkedList<TaskConfiguration>(); | ||
480 | } | ||
481 | pending.clear(); | ||
482 | } | 487 | } |
483 | } | 488 | } |
484 | 489 | ||
@@ -495,25 +500,29 @@ public class Scheduler { | |||
495 | } | 500 | } |
496 | 501 | ||
497 | // the gnunet main loop | 502 | // the gnunet main loop |
498 | while (checkLiveness()) { | 503 | while (true) { |
499 | RelativeTime nextTimeout = handleTimeouts(); | 504 | synchronized (Scheduler.class) { |
500 | if (nextTimeout.getMicroseconds() < 0) { | 505 | if (checkLiveness() == false) |
501 | logger.warn("negative timeout for select"); | 506 | break; |
502 | } | 507 | RelativeTime nextTimeout = handleTimeouts(); |
508 | if (nextTimeout.getMicroseconds() < 0) { | ||
509 | logger.warn("negative timeout for select"); | ||
510 | } | ||
503 | 511 | ||
504 | // don't select if there are no tasks; we are done! | 512 | // don't select if there are no tasks; we are done! |
505 | if (readyCount == 0 && pending.isEmpty()) { | 513 | if (readyCount == 0 && pending.isEmpty()) { |
506 | return; | 514 | return; |
507 | } | 515 | } |
508 | 516 | ||
509 | // don't block in select if we have tasks ready to run! | 517 | // don't block in select if we have tasks ready to run! |
510 | if (readyCount > 0) { | 518 | if (readyCount > 0) { |
511 | handleSelect(RelativeTime.ZERO); | 519 | handleSelect(RelativeTime.ZERO); |
512 | } else { | 520 | } else { |
513 | handleSelect(nextTimeout); | 521 | handleSelect(nextTimeout); |
514 | } | 522 | } |
515 | 523 | ||
516 | runReady(); | 524 | runReady(); |
525 | } | ||
517 | } | 526 | } |
518 | 527 | ||
519 | if (readyCount != 0) { | 528 | if (readyCount != 0) { |
@@ -563,7 +572,7 @@ public class Scheduler { | |||
563 | } | 572 | } |
564 | 573 | ||
565 | /** | 574 | /** |
566 | * Request the shutdown of a scheduler. Marks all currently pending tasks as | 575 | * Request the shutdown of the scheduler. Marks all currently pending tasks as |
567 | * ready because of disconnect. This will cause all tasks to run (as soon as | 576 | * ready because of disconnect. This will cause all tasks to run (as soon as |
568 | * possible, respecting priorities and prerequisite tasks). Note that tasks | 577 | * possible, respecting priorities and prerequisite tasks). Note that tasks |
569 | * scheduled AFTER this call may still be delayed arbitrarily. | 578 | * scheduled AFTER this call may still be delayed arbitrarily. |
@@ -594,6 +603,9 @@ public class Scheduler { | |||
594 | 603 | ||
595 | } | 604 | } |
596 | 605 | ||
606 | /** | ||
607 | * A thread that reads from a file pipe. | ||
608 | */ | ||
597 | private static class FilePipeThread extends Thread { | 609 | private static class FilePipeThread extends Thread { |
598 | public File file; | 610 | public File file; |
599 | public Pipe pipe; | 611 | public Pipe pipe; |
@@ -662,13 +674,5 @@ public class Scheduler { | |||
662 | fpt.start(); | 674 | fpt.start(); |
663 | return new FilePipe(fpt); | 675 | return new FilePipe(fpt); |
664 | } | 676 | } |
665 | |||
666 | public static class AsyncProcess { | ||
667 | // getIn, getOut, getErr | ||
668 | |||
669 | } | ||
670 | |||
671 | public static AsyncProcess openAsyncProcess(/*...*/) { | ||
672 | throw new UnsupportedOperationException("not implemented yet"); | ||
673 | } | ||
674 | } | 677 | } |
678 | |||
diff --git a/src/org/gnunet/util/Server.java b/src/main/java/org/gnunet/util/Server.java index 8a86b45..8a86b45 100644 --- a/src/org/gnunet/util/Server.java +++ b/src/main/java/org/gnunet/util/Server.java | |||
diff --git a/src/org/gnunet/util/Service.java b/src/main/java/org/gnunet/util/Service.java index d93e296..d93e296 100644 --- a/src/org/gnunet/util/Service.java +++ b/src/main/java/org/gnunet/util/Service.java | |||
diff --git a/src/org/gnunet/util/Strings.java b/src/main/java/org/gnunet/util/Strings.java index a35568a..a35568a 100644 --- a/src/org/gnunet/util/Strings.java +++ b/src/main/java/org/gnunet/util/Strings.java | |||
diff --git a/src/org/gnunet/util/TestMessage.java b/src/main/java/org/gnunet/util/TestMessage.java index b08a706..b08a706 100644 --- a/src/org/gnunet/util/TestMessage.java +++ b/src/main/java/org/gnunet/util/TestMessage.java | |||
diff --git a/src/org/gnunet/util/UnknownMessageBody.java b/src/main/java/org/gnunet/util/UnknownMessageBody.java index b978ec6..b978ec6 100644 --- a/src/org/gnunet/util/UnknownMessageBody.java +++ b/src/main/java/org/gnunet/util/UnknownMessageBody.java | |||
diff --git a/src/org/gnunet/util/getopt/Argument.java b/src/main/java/org/gnunet/util/getopt/Argument.java index 34159d0..34159d0 100644 --- a/src/org/gnunet/util/getopt/Argument.java +++ b/src/main/java/org/gnunet/util/getopt/Argument.java | |||
diff --git a/src/org/gnunet/util/getopt/ArgumentAction.java b/src/main/java/org/gnunet/util/getopt/ArgumentAction.java index 077e71c..077e71c 100644 --- a/src/org/gnunet/util/getopt/ArgumentAction.java +++ b/src/main/java/org/gnunet/util/getopt/ArgumentAction.java | |||
diff --git a/src/org/gnunet/util/getopt/Parser.java b/src/main/java/org/gnunet/util/getopt/Parser.java index 6ecc220..6ecc220 100644 --- a/src/org/gnunet/util/getopt/Parser.java +++ b/src/main/java/org/gnunet/util/getopt/Parser.java | |||
diff --git a/src/org/gnunet/util/getopt/package-info.java b/src/main/java/org/gnunet/util/getopt/package-info.java index 0e3bdf5..0e3bdf5 100644 --- a/src/org/gnunet/util/getopt/package-info.java +++ b/src/main/java/org/gnunet/util/getopt/package-info.java | |||
diff --git a/src/org/gnunet/util/package-info.java b/src/main/java/org/gnunet/util/package-info.java index df61afd..df61afd 100644 --- a/src/org/gnunet/util/package-info.java +++ b/src/main/java/org/gnunet/util/package-info.java | |||
diff --git a/src/main/java/org/gnunet/voting/CertificateAuthorityService.java b/src/main/java/org/gnunet/voting/CertificateAuthorityService.java new file mode 100644 index 0000000..559bd97 --- /dev/null +++ b/src/main/java/org/gnunet/voting/CertificateAuthorityService.java | |||
@@ -0,0 +1,7 @@ | |||
1 | package org.gnunet.voting; | ||
2 | |||
3 | /** | ||
4 | * Permits or denies a voter to participate in an election. | ||
5 | */ | ||
6 | public class CertificateAuthorityService { | ||
7 | } | ||
diff --git a/src/main/java/org/gnunet/voting/ElectionCallTool.java b/src/main/java/org/gnunet/voting/ElectionCallTool.java new file mode 100644 index 0000000..52ad5fb --- /dev/null +++ b/src/main/java/org/gnunet/voting/ElectionCallTool.java | |||
@@ -0,0 +1,50 @@ | |||
1 | package org.gnunet.voting; | ||
2 | |||
3 | |||
4 | import org.gnunet.util.Program; | ||
5 | import org.gnunet.util.getopt.Argument; | ||
6 | import org.gnunet.util.getopt.ArgumentAction; | ||
7 | |||
8 | public class ElectionCallTool { | ||
9 | |||
10 | public static void main(String[] args) { | ||
11 | new Program(args) { | ||
12 | @Argument( | ||
13 | shortname = "t", | ||
14 | longname = "template", | ||
15 | action = ArgumentAction.SET, | ||
16 | description = "output election template") | ||
17 | boolean template = false; | ||
18 | @Argument( | ||
19 | shortname = "o", | ||
20 | longname = "outfile", | ||
21 | argumentName = "FILE", | ||
22 | action = ArgumentAction.STORE_STRING, | ||
23 | description = "write spec to FILE instead of standard output") | ||
24 | String outfilename = null; | ||
25 | @Argument( | ||
26 | shortname = "V", | ||
27 | longname = "verify", | ||
28 | action = ArgumentAction.SET, | ||
29 | description = "verify that the ESPEC is valid") | ||
30 | boolean verify = false; | ||
31 | @Argument( | ||
32 | shortname = "a", | ||
33 | longname = "authorize", | ||
34 | action = ArgumentAction.SET, | ||
35 | description = "authorize the ESPEC with the authorities") | ||
36 | boolean authorize = false; | ||
37 | |||
38 | @Override | ||
39 | protected String makeHelpText() { | ||
40 | return "gnunet-vote-call [OPTIONS]... ESPEC\n" + | ||
41 | "Create, authorize and verify an election specification"; | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public void run() { | ||
46 | } | ||
47 | |||
48 | }.start(); | ||
49 | } | ||
50 | } | ||
diff --git a/src/main/java/org/gnunet/voting/ElectionSpecification.java b/src/main/java/org/gnunet/voting/ElectionSpecification.java new file mode 100644 index 0000000..8671ad5 --- /dev/null +++ b/src/main/java/org/gnunet/voting/ElectionSpecification.java | |||
@@ -0,0 +1,5 @@ | |||
1 | package org.gnunet.voting; | ||
2 | |||
3 | public class ElectionSpecification { | ||
4 | |||
5 | } | ||
diff --git a/src/main/java/org/gnunet/voting/TallyAuthorityService.java b/src/main/java/org/gnunet/voting/TallyAuthorityService.java new file mode 100644 index 0000000..435c318 --- /dev/null +++ b/src/main/java/org/gnunet/voting/TallyAuthorityService.java | |||
@@ -0,0 +1,5 @@ | |||
1 | package org.gnunet.voting; | ||
2 | |||
3 | |||
4 | public class TallyAuthorityService { | ||
5 | } | ||
diff --git a/src/main/java/org/gnunet/voting/VotingTool.java b/src/main/java/org/gnunet/voting/VotingTool.java new file mode 100644 index 0000000..02874b6 --- /dev/null +++ b/src/main/java/org/gnunet/voting/VotingTool.java | |||
@@ -0,0 +1,49 @@ | |||
1 | package org.gnunet.voting; | ||
2 | |||
3 | |||
4 | import org.gnunet.util.Program; | ||
5 | import org.gnunet.util.getopt.Argument; | ||
6 | import org.gnunet.util.getopt.ArgumentAction; | ||
7 | |||
8 | public class VotingTool { | ||
9 | public static void main(String[] args) { | ||
10 | new Program(args) { | ||
11 | @Argument( | ||
12 | shortname = "q", | ||
13 | longname = "query", | ||
14 | action = ArgumentAction.SET, | ||
15 | description = "query election result") | ||
16 | boolean query = false; | ||
17 | |||
18 | @Argument( | ||
19 | shortname = "s", | ||
20 | longname = "submit", | ||
21 | action = ArgumentAction.STORE_STRING, | ||
22 | argumentName = "CHOICE", | ||
23 | description = "submit vote to the election") | ||
24 | String vote = null; | ||
25 | |||
26 | @Argument( | ||
27 | shortname = "p", | ||
28 | longname = "certificate", | ||
29 | action = ArgumentAction.STORE_STRING, | ||
30 | argumentName = "FILE", | ||
31 | description = "certificate file with the permission to vote") | ||
32 | String certfile = null; | ||
33 | |||
34 | @Override | ||
35 | protected String makeHelpText() { | ||
36 | return "gnunet-vote [OPTIONS]... ESPEC\n" + | ||
37 | "Submit a vote or query an election's result.\n" + | ||
38 | "The election is identified in the ESPEC file."; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public void run() { | ||
43 | |||
44 | } | ||
45 | }.start(); | ||
46 | |||
47 | } | ||
48 | |||
49 | } | ||
diff --git a/src/org/gnunet/voting/Authority.java b/src/main/java/org/gnunet/voting/simulation/Authority.java index 8c39100..792dbbd 100644 --- a/src/org/gnunet/voting/Authority.java +++ b/src/main/java/org/gnunet/voting/simulation/Authority.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import com.google.common.collect.Lists; | 3 | import com.google.common.collect.Lists; |
4 | import com.google.common.collect.Maps; | 4 | import com.google.common.collect.Maps; |
diff --git a/src/org/gnunet/voting/Ballot.java b/src/main/java/org/gnunet/voting/simulation/Ballot.java index fbc6e11..1c2bfbe 100644 --- a/src/org/gnunet/voting/Ballot.java +++ b/src/main/java/org/gnunet/voting/simulation/Ballot.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/BogusAuthority.java b/src/main/java/org/gnunet/voting/simulation/BogusAuthority.java index 8c0363d..d59f0cf 100644 --- a/src/org/gnunet/voting/BogusAuthority.java +++ b/src/main/java/org/gnunet/voting/simulation/BogusAuthority.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | /** | 3 | /** |
4 | * ... | 4 | * ... |
diff --git a/src/org/gnunet/voting/CallForVoters.java b/src/main/java/org/gnunet/voting/simulation/CallForVoters.java index d6436eb..a84484d 100644 --- a/src/org/gnunet/voting/CallForVoters.java +++ b/src/main/java/org/gnunet/voting/simulation/CallForVoters.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.util.List; | 3 | import java.util.List; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/CryptoUtil.java b/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java index 6f15bf6..87ecd9f 100644 --- a/src/org/gnunet/voting/CryptoUtil.java +++ b/src/main/java/org/gnunet/voting/simulation/CryptoUtil.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | import java.security.MessageDigest; | 4 | import java.security.MessageDigest; |
diff --git a/src/org/gnunet/voting/Cyphertext.java b/src/main/java/org/gnunet/voting/simulation/Cyphertext.java index 218252c..6e7d72f 100644 --- a/src/org/gnunet/voting/Cyphertext.java +++ b/src/main/java/org/gnunet/voting/simulation/Cyphertext.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/ElectionSupervisor.java b/src/main/java/org/gnunet/voting/simulation/ElectionSupervisor.java index 106095b..ee0b181 100644 --- a/src/org/gnunet/voting/ElectionSupervisor.java +++ b/src/main/java/org/gnunet/voting/simulation/ElectionSupervisor.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import com.google.common.collect.Lists; | 3 | import com.google.common.collect.Lists; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/GroupPublicKey.java b/src/main/java/org/gnunet/voting/simulation/GroupPublicKey.java index a7b0cae..a6f2c74 100644 --- a/src/org/gnunet/voting/GroupPublicKey.java +++ b/src/main/java/org/gnunet/voting/simulation/GroupPublicKey.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/TallyKeyShare.java b/src/main/java/org/gnunet/voting/simulation/TallyKeyShare.java index d67cfa9..1982358 100644 --- a/src/org/gnunet/voting/TallyKeyShare.java +++ b/src/main/java/org/gnunet/voting/simulation/TallyKeyShare.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/TransmitShareVerification.java b/src/main/java/org/gnunet/voting/simulation/TransmitShareVerification.java index a30e327..13da325 100644 --- a/src/org/gnunet/voting/TransmitShareVerification.java +++ b/src/main/java/org/gnunet/voting/simulation/TransmitShareVerification.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/Voter.java b/src/main/java/org/gnunet/voting/simulation/Voter.java index fa658dd..53a5640 100644 --- a/src/org/gnunet/voting/Voter.java +++ b/src/main/java/org/gnunet/voting/simulation/Voter.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | 4 | ||
diff --git a/src/org/gnunet/voting/VotingParameters.java b/src/main/java/org/gnunet/voting/simulation/VotingParameters.java index b563bff..bc6b987 100644 --- a/src/org/gnunet/voting/VotingParameters.java +++ b/src/main/java/org/gnunet/voting/simulation/VotingParameters.java | |||
@@ -1,8 +1,7 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import java.math.BigInteger; | 3 | import java.math.BigInteger; |
4 | import java.security.SecureRandom; | 4 | import java.security.SecureRandom; |
5 | import java.util.Random; | ||
6 | 5 | ||
7 | /** | 6 | /** |
8 | * Utilities for the modified ElGamal algorithm. | 7 | * Utilities for the modified ElGamal algorithm. |
diff --git a/src/org/gnunet/voting/VotingSimulation.java b/src/main/java/org/gnunet/voting/simulation/VotingSimulation.java index 853fdff..607c653 100644 --- a/src/org/gnunet/voting/VotingSimulation.java +++ b/src/main/java/org/gnunet/voting/simulation/VotingSimulation.java | |||
@@ -1,4 +1,4 @@ | |||
1 | package org.gnunet.voting; | 1 | package org.gnunet.voting.simulation; |
2 | 2 | ||
3 | import com.google.common.collect.Lists; | 3 | import com.google.common.collect.Lists; |
4 | 4 | ||
diff --git a/src/org/grothoff/Runabout.java b/src/main/java/org/grothoff/Runabout.java index 2a1dbb0..2a1dbb0 100644 --- a/src/org/grothoff/Runabout.java +++ b/src/main/java/org/grothoff/Runabout.java | |||
diff --git a/src/org/grothoff/package-info.java b/src/main/java/org/grothoff/package-info.java index 2a8b8f0..2a8b8f0 100644 --- a/src/org/grothoff/package-info.java +++ b/src/main/java/org/grothoff/package-info.java | |||
diff --git a/src/main/resources/org/gnunet/construct/MsgMap.txt b/src/main/resources/org/gnunet/construct/MsgMap.txt new file mode 100644 index 0000000..2ca1246 --- /dev/null +++ b/src/main/resources/org/gnunet/construct/MsgMap.txt | |||
@@ -0,0 +1,46 @@ | |||
1 | org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress | ||
2 | org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress | ||
3 | org.gnunet.util.GnunetMessage$Body|68=org.gnunet.core.DisconnectNotifyMessage | ||
4 | org.gnunet.util.GnunetMessage$Body|274=org.gnunet.mesh.TunnelDestroyMessage | ||
5 | org.gnunet.util.GnunetMessage$Body|1=org.gnunet.util.TestMessage | ||
6 | org.gnunet.util.GnunetMessage$Body|70=org.gnunet.core.NotifyInboundTrafficMessage | ||
7 | org.gnunet.util.GnunetMessage$Body|273=org.gnunet.mesh.TunnelCreateMessage | ||
8 | org.gnunet.util.GnunetMessage$Body|71=org.gnunet.core.NotifyOutboundTrafficMessage | ||
9 | org.gnunet.util.GnunetMessage$Body|272=org.gnunet.mesh.ClientConnectMessage | ||
10 | org.gnunet.util.GnunetMessage$Body|64=org.gnunet.core.InitMessage | ||
11 | org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage | ||
12 | org.gnunet.util.GnunetMessage$Body|65=org.gnunet.core.InitReplyMessage | ||
13 | org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse | ||
14 | org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.ClientGetMessage | ||
15 | org.gnunet.util.GnunetMessage$Body|67=org.gnunet.core.ConnectNotifyMessage | ||
16 | org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.ClientPutMessage | ||
17 | org.gnunet.util.GnunetMessage$Body|76=org.gnunet.core.SendMessage | ||
18 | org.gnunet.util.GnunetMessage$Body|286=org.gnunet.mesh.LocalAckMessage | ||
19 | org.gnunet.util.GnunetMessage$Body|74=org.gnunet.core.SendMessageRequest | ||
20 | org.gnunet.util.GnunetMessage$Body|75=org.gnunet.core.SendMessageReady | ||
21 | org.gnunet.util.GnunetMessage$Body|153=org.gnunet.dht.MonitorStartStop | ||
22 | org.gnunet.util.GnunetMessage$Body|155=org.gnunet.dht.ClientPutConfirmationMessage | ||
23 | org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.UpdateMessage | ||
24 | org.gnunet.util.GnunetMessage$Body|260=org.gnunet.mesh.DataMessage | ||
25 | org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.StartMessage | ||
26 | org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.ClientGetStopMessage | ||
27 | org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.ClientResultMessage | ||
28 | org.gnunet.util.GnunetMessage$Body|332=org.gnunet.peerinfo.InfoMessage | ||
29 | org.gnunet.util.GnunetMessage$Body|333=org.gnunet.peerinfo.InfoEnd | ||
30 | org.gnunet.util.GnunetMessage$Body|149=org.gnunet.dht.MonitorGetMessage | ||
31 | org.gnunet.util.GnunetMessage$Body|331=org.gnunet.peerinfo.ListAllPeersMessage | ||
32 | org.gnunet.util.GnunetMessage$Body|150=org.gnunet.dht.MonitorGetRespMessage | ||
33 | org.gnunet.util.GnunetMessage$Body|151=org.gnunet.dht.MonitorPutMessage | ||
34 | org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.GetResponseEndMessage | ||
35 | org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.GetResponseMessage | ||
36 | org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.GetMessage | ||
37 | org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.SetMessage | ||
38 | org.gnunet.util.GnunetMessage$Body|374=org.gnunet.transport.RequestConnectMessage | ||
39 | org.gnunet.util.GnunetMessage$Body|173=org.gnunet.statistics.WatchResponseMessage | ||
40 | org.gnunet.util.GnunetMessage$Body|172=org.gnunet.statistics.WatchMessage | ||
41 | org.gnunet.util.GnunetMessage$Body|524=org.gnunet.consensus.ConcludeMessage | ||
42 | org.gnunet.util.GnunetMessage$Body|521=org.gnunet.consensus.InsertElementMessage | ||
43 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage | ||
44 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage | ||
45 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage | ||
46 | # generated 2013/08/22 21:29:40 | ||
diff --git a/src/org/gnunet/mq/ClientMessageQueue.java b/src/org/gnunet/mq/ClientMessageQueue.java deleted file mode 100644 index b3fccc7..0000000 --- a/src/org/gnunet/mq/ClientMessageQueue.java +++ /dev/null | |||
@@ -1,44 +0,0 @@ | |||
1 | package org.gnunet.mq; | ||
2 | |||
3 | |||
4 | import org.gnunet.construct.Construct; | ||
5 | import org.gnunet.util.*; | ||
6 | |||
7 | /** | ||
8 | * Message queue for org.util.Connection | ||
9 | */ | ||
10 | public class ClientMessageQueue extends MessageQueue { | ||
11 | private final Client client; | ||
12 | private final RunaboutMessageReceiver receiver; | ||
13 | |||
14 | public ClientMessageQueue(Client client, RunaboutMessageReceiver receiver) { | ||
15 | this.client = client; | ||
16 | this.receiver = receiver; | ||
17 | } | ||
18 | |||
19 | public ClientMessageQueue(Client client) { | ||
20 | this(client, null); | ||
21 | } | ||
22 | |||
23 | @Override | ||
24 | protected void sendImmediate(final Envelope ev) { | ||
25 | int size = Construct.getSize(ev.message); | ||
26 | client.notifyTransmitReady(RelativeTime.FOREVER, false, size, new MessageTransmitter() { | ||
27 | @Override | ||
28 | public void transmit(Connection.MessageSink sink) { | ||
29 | sink.send(ev.message); | ||
30 | reportMessageSent(); | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public void handleError() { | ||
35 | // FIXME | ||
36 | } | ||
37 | }); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public void destroy() { | ||
42 | |||
43 | } | ||
44 | } | ||
diff --git a/src/org/gnunet/mq/MessageQueue.java b/src/org/gnunet/mq/MessageQueue.java deleted file mode 100644 index de08edf..0000000 --- a/src/org/gnunet/mq/MessageQueue.java +++ /dev/null | |||
@@ -1,41 +0,0 @@ | |||
1 | package org.gnunet.mq; | ||
2 | |||
3 | |||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | |||
6 | import java.util.LinkedList; | ||
7 | |||
8 | /** | ||
9 | * General-purpose message queue | ||
10 | */ | ||
11 | public abstract class MessageQueue { | ||
12 | private LinkedList<Envelope> queued_envelopes = new LinkedList<>(); | ||
13 | protected Envelope current_envelope; | ||
14 | |||
15 | protected abstract void sendImmediate(Envelope ev); | ||
16 | |||
17 | public void send(GnunetMessage.Body body) { | ||
18 | send(new Envelope(body)); | ||
19 | } | ||
20 | |||
21 | public void send(Envelope ev) { | ||
22 | if (null == current_envelope) { | ||
23 | current_envelope = ev; | ||
24 | sendImmediate(current_envelope); | ||
25 | } else { | ||
26 | queued_envelopes.addLast(ev); | ||
27 | } | ||
28 | } | ||
29 | |||
30 | protected void reportMessageSent() { | ||
31 | if (null == current_envelope) | ||
32 | throw new AssertionError(); | ||
33 | current_envelope.invokeSentNotification(); | ||
34 | if (queued_envelopes.isEmpty()) | ||
35 | return; | ||
36 | current_envelope = queued_envelopes.pop(); | ||
37 | sendImmediate(current_envelope); | ||
38 | } | ||
39 | |||
40 | public abstract void destroy(); | ||
41 | } | ||
diff --git a/src/org/gnunet/peerinfo/PeerInfo.java b/src/org/gnunet/peerinfo/PeerInfo.java deleted file mode 100644 index 9d92763..0000000 --- a/src/org/gnunet/peerinfo/PeerInfo.java +++ /dev/null | |||
@@ -1,123 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.peerinfo; | ||
22 | |||
23 | |||
24 | import org.gnunet.hello.HelloMessage; | ||
25 | import org.gnunet.requests.Request; | ||
26 | import org.gnunet.requests.RequestQueue; | ||
27 | import org.gnunet.util.*; | ||
28 | import org.slf4j.Logger; | ||
29 | import org.slf4j.LoggerFactory; | ||
30 | |||
31 | /** | ||
32 | * Interface to the service that maintains all known hosts. | ||
33 | * | ||
34 | * @author Florian Dold | ||
35 | */ | ||
36 | public class PeerInfo { | ||
37 | private static final Logger logger = LoggerFactory | ||
38 | .getLogger(AbsoluteTime.class); | ||
39 | |||
40 | private final RequestQueue requests; | ||
41 | private PeerIterateRequest activeRequest; | ||
42 | |||
43 | private class PeerIterateRequest extends Request { | ||
44 | public PeerIdentity peer; | ||
45 | public PeerProcessor peerProcessor; | ||
46 | @Override | ||
47 | public void transmit(Connection.MessageSink sink) { | ||
48 | if (peer == null) { | ||
49 | ListAllPeersMessage lapm = new ListAllPeersMessage(); | ||
50 | sink.send(lapm); | ||
51 | } else { | ||
52 | ListPeerMessage lpm = new ListPeerMessage(); | ||
53 | lpm.peer = peer; | ||
54 | sink.send(lpm); | ||
55 | } | ||
56 | activeRequest = this; | ||
57 | requests.clog(); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | private class PeerInfoMessageReceiver extends RunaboutMessageReceiver { | ||
62 | public void visit(InfoEnd infoEnd) { | ||
63 | activeRequest.peerProcessor.onEnd(); | ||
64 | activeRequest = null; | ||
65 | requests.unclog(); | ||
66 | } | ||
67 | public void visit(InfoMessage infoMessage) { | ||
68 | if (activeRequest == null) { | ||
69 | logger.warn("PEERINFO sent info message, but no request is active"); | ||
70 | } | ||
71 | activeRequest.peerProcessor.onPeer(infoMessage.peerIdentity, infoMessage.hello); | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public void handleError() { | ||
76 | } | ||
77 | } | ||
78 | |||
79 | |||
80 | public PeerInfo(Configuration cfg) { | ||
81 | Client client = new Client("peerinfo", cfg); | ||
82 | requests = new RequestQueue(client, new PeerInfoMessageReceiver()); | ||
83 | } | ||
84 | |||
85 | public Cancelable iterate(RelativeTime timeout, PeerProcessor processor) { | ||
86 | return iterate(timeout, null, processor); | ||
87 | } | ||
88 | |||
89 | public Cancelable iterate(RelativeTime timeout, PeerIdentity peer, PeerProcessor processor) { | ||
90 | PeerIterateRequest peerIterateRequest = new PeerIterateRequest(); | ||
91 | peerIterateRequest.peer = peer; | ||
92 | peerIterateRequest.peerProcessor = processor; | ||
93 | peerIterateRequest.setDeadline(timeout.toAbsolute()); | ||
94 | return requests.add(peerIterateRequest); | ||
95 | } | ||
96 | |||
97 | public void disconnect() { | ||
98 | requests.destroy(); | ||
99 | } | ||
100 | |||
101 | public static void main(String... args) { | ||
102 | new Program(args) { | ||
103 | |||
104 | @Override | ||
105 | public void run() { | ||
106 | final PeerInfo peerInfo = new PeerInfo(getConfiguration()); | ||
107 | peerInfo.iterate(RelativeTime.FOREVER, new PeerProcessor() { | ||
108 | @Override | ||
109 | public void onPeer(PeerIdentity peerIdentity, HelloMessage hello) { | ||
110 | System.out.println("peer " + peerIdentity.toString()); | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public void onEnd() { | ||
115 | System.out.println("got peer end"); | ||
116 | peerInfo.disconnect(); | ||
117 | } | ||
118 | }); | ||
119 | } | ||
120 | }.start(); | ||
121 | |||
122 | } | ||
123 | } | ||
diff --git a/src/org/gnunet/requests/FixedMessageRequest.java b/src/org/gnunet/requests/FixedMessageRequest.java deleted file mode 100644 index 1420e28..0000000 --- a/src/org/gnunet/requests/FixedMessageRequest.java +++ /dev/null | |||
@@ -1,23 +0,0 @@ | |||
1 | package org.gnunet.requests; | ||
2 | |||
3 | import org.gnunet.util.Connection; | ||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | |||
6 | /** | ||
7 | * A request that sends a message, pre-determined at construction of the FixedMessageRequest. | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class FixedMessageRequest extends Request { | ||
12 | private final GnunetMessage.Body msg; | ||
13 | |||
14 | public FixedMessageRequest(GnunetMessage.Body msg) { | ||
15 | this.msg = msg; | ||
16 | } | ||
17 | |||
18 | final | ||
19 | @Override | ||
20 | public void transmit(Connection.MessageSink sink) { | ||
21 | sink.send(msg); | ||
22 | } | ||
23 | } | ||
diff --git a/src/org/gnunet/requests/Request.java b/src/org/gnunet/requests/Request.java deleted file mode 100644 index 953b5e1..0000000 --- a/src/org/gnunet/requests/Request.java +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | package org.gnunet.requests; | ||
2 | |||
3 | import org.gnunet.util.AbsoluteTime; | ||
4 | import org.gnunet.util.Connection; | ||
5 | import org.gnunet.util.RelativeTime; | ||
6 | |||
7 | /** | ||
8 | * Abstract base class for a Request. | ||
9 | * | ||
10 | * Every request defines what happens when one of the following happens: | ||
11 | * <ul> | ||
12 | * <li> | ||
13 | * The request is canceled. There may be some cleanup necessary, depending on whether the request has already been | ||
14 | * sent to the service or not. | ||
15 | * </li> | ||
16 | * <li> | ||
17 | * On timeout. | ||
18 | * </li> | ||
19 | * <li> | ||
20 | * On reconnect. In particular, every Request has to decide whether it will be kept after a reconnect to the service. | ||
21 | * </li> | ||
22 | * <li> | ||
23 | * On destruction of the request queue. Some request may be important enough to delay the destruction until they have been sent. | ||
24 | * </li> | ||
25 | * </ul> | ||
26 | * | ||
27 | * @author Florian Dold | ||
28 | */ | ||
29 | public abstract class Request { | ||
30 | protected AbsoluteTime deadline = AbsoluteTime.FOREVER; | ||
31 | |||
32 | /** | ||
33 | * Called whenever the request could not be transmitted due to timeout. | ||
34 | * | ||
35 | * @return true if the request should be queued again | ||
36 | */ | ||
37 | public boolean onTransmitTimeout() { | ||
38 | // per default, just drop the message on timeout! | ||
39 | return false; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * | ||
44 | * | ||
45 | * @return true if the request should be kept after the destroy request | ||
46 | */ | ||
47 | public boolean onDestroy() { | ||
48 | // per default, do not keep on destroy | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * @return true if the request should be kept after the reconnect | ||
54 | */ | ||
55 | public boolean onReconnect() { | ||
56 | // per default, do not keep on reconnect | ||
57 | return false; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * @param alreadyTransmitted true if message has already been sent over the network | ||
62 | */ | ||
63 | public void onCancel(boolean alreadyTransmitted) { | ||
64 | // do nothing | ||
65 | } | ||
66 | |||
67 | public void setDeadline(AbsoluteTime deadline) { | ||
68 | this.deadline = deadline; | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Called to determine after how long the request should time out. | ||
73 | * Per default, the deadline is FOREVER. | ||
74 | * | ||
75 | * @return the deadline for this request | ||
76 | */ | ||
77 | public AbsoluteTime getDeadline() { | ||
78 | return deadline; | ||
79 | } | ||
80 | |||
81 | public abstract void transmit(Connection.MessageSink sink); | ||
82 | } | ||
diff --git a/src/org/gnunet/requests/RequestQueue.java b/src/org/gnunet/requests/RequestQueue.java deleted file mode 100644 index 6f7102d..0000000 --- a/src/org/gnunet/requests/RequestQueue.java +++ /dev/null | |||
@@ -1,294 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.requests; | ||
22 | |||
23 | import org.gnunet.util.*; | ||
24 | |||
25 | import java.util.LinkedList; | ||
26 | |||
27 | /** | ||
28 | * Generic queues for Requests to be sent to the service. | ||
29 | */ | ||
30 | public class RequestQueue { | ||
31 | |||
32 | /** | ||
33 | * Requests to be transmitted to the service. | ||
34 | */ | ||
35 | private final LinkedList<Request> requestsAwaitingTransmit = new LinkedList<Request>(); | ||
36 | |||
37 | /** | ||
38 | * Persistent requests. Will be informed about reconnect / destroy events even | ||
39 | * if already transmitted. Have to be canceled manually. | ||
40 | */ | ||
41 | private final LinkedList<Request> persistentRequests = new LinkedList<Request>(); | ||
42 | |||
43 | /** | ||
44 | * List of all requests from requestAwaitingTransmit and persistentRequest, containing no duplicates. | ||
45 | */ | ||
46 | private final LinkedList<Request> allRequests = new LinkedList<Request>(); | ||
47 | |||
48 | /** | ||
49 | * The designated receiver for all messages. | ||
50 | */ | ||
51 | private MessageReceiver receiver; | ||
52 | |||
53 | /** | ||
54 | * The active transmit request handle, if any. | ||
55 | */ | ||
56 | private Cancelable currentTransmit; | ||
57 | |||
58 | /** | ||
59 | * Current receive handler. | ||
60 | */ | ||
61 | private Cancelable currentReceive; | ||
62 | |||
63 | /** | ||
64 | * True if we should not send further requests until queue is unclogged. | ||
65 | */ | ||
66 | private boolean clogged = false; | ||
67 | |||
68 | private boolean destroyed = false; | ||
69 | private final Client client; | ||
70 | |||
71 | public RequestQueue(Client client, MessageReceiver receiver) { | ||
72 | this.client = client; | ||
73 | this.receiver = receiver; | ||
74 | } | ||
75 | |||
76 | /** | ||
77 | * Handle next request. | ||
78 | */ | ||
79 | private void handleNextTransmit() { | ||
80 | |||
81 | if (clogged) { | ||
82 | return; | ||
83 | } | ||
84 | |||
85 | // return if we are already transmitting something | ||
86 | if (currentTransmit != null) { | ||
87 | return; | ||
88 | } | ||
89 | |||
90 | final Request request = requestsAwaitingTransmit.poll(); | ||
91 | if (request == null) { | ||
92 | handleReceive(); | ||
93 | return; | ||
94 | } | ||
95 | |||
96 | AbsoluteTime deadline = request.getDeadline(); | ||
97 | if (deadline == null) { | ||
98 | throw new AssertionError("getDeadline() must return a non-null AbsoluteTime"); | ||
99 | } | ||
100 | |||
101 | currentTransmit = client.notifyTransmitReady(deadline.getRemaining(), true, 0, new MessageTransmitter() { | ||
102 | @Override | ||
103 | public void transmit(Connection.MessageSink sink) { | ||
104 | currentTransmit = null; | ||
105 | |||
106 | try { | ||
107 | request.transmit(sink); | ||
108 | } finally { | ||
109 | handleReceive(); | ||
110 | handleNextTransmit(); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | @Override | ||
115 | public void handleError() { | ||
116 | throw new AssertionError("not implemented"); | ||
117 | } | ||
118 | }); | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Continue receiving if necessary. | ||
123 | */ | ||
124 | private void handleReceive() { | ||
125 | if (currentReceive != null || destroyed || !client.isConnected()) { | ||
126 | return; | ||
127 | } | ||
128 | currentReceive = client.receive(RelativeTime.FOREVER, new MessageReceiver() { | ||
129 | @Override | ||
130 | public void process(GnunetMessage.Body msg) { | ||
131 | currentReceive = null; | ||
132 | |||
133 | try { | ||
134 | receiver.process(msg); | ||
135 | } finally { | ||
136 | handleNextTransmit(); | ||
137 | handleReceive(); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | @Override | ||
142 | public void handleError() { | ||
143 | receiver.handleError(); | ||
144 | } | ||
145 | }); | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * Add a request to the end of the queue. | ||
150 | * | ||
151 | * @param request request to be added | ||
152 | * @return a handle to cancel the request | ||
153 | */ | ||
154 | public Cancelable add(final Request request) { | ||
155 | allRequests.add(request); | ||
156 | requestsAwaitingTransmit.add(request); | ||
157 | handleNextTransmit(); | ||
158 | |||
159 | return new Cancelable() { | ||
160 | @Override | ||
161 | public void cancel() { | ||
162 | RequestQueue.this.requestsAwaitingTransmit.remove(request); | ||
163 | RequestQueue.this.persistentRequests.remove(request); | ||
164 | RequestQueue.this.allRequests.remove(request); | ||
165 | request.onCancel(!requestsAwaitingTransmit.contains(request)); | ||
166 | } | ||
167 | }; | ||
168 | } | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Add a request so that it will get notified about reconnect/destroy events, | ||
173 | * even if it already has been transmitted. | ||
174 | */ | ||
175 | public Cancelable addPersistent(final Request request) { | ||
176 | persistentRequests.add(request); | ||
177 | return add(request); | ||
178 | } | ||
179 | |||
180 | |||
181 | /** | ||
182 | * Add a request to the front of the queue, this request will be sent as | ||
183 | * the next message (if not preempted by another sendNext). | ||
184 | * | ||
185 | * @param request request to be sent next | ||
186 | * @return a handle to cancel the request | ||
187 | */ | ||
188 | public Cancelable sendNext(final Request request) { | ||
189 | requestsAwaitingTransmit.addFirst(request); | ||
190 | handleNextTransmit(); | ||
191 | // todo: should this really return Cancelable? When do we want to cancel a request added by sendNext? | ||
192 | return new Cancelable() { | ||
193 | @Override | ||
194 | public void cancel() { | ||
195 | RequestQueue.this.requestsAwaitingTransmit.remove(request); | ||
196 | RequestQueue.this.persistentRequests.remove(request); | ||
197 | RequestQueue.this.allRequests.remove(request); | ||
198 | request.onCancel(!requestsAwaitingTransmit.contains(request)); | ||
199 | } | ||
200 | }; | ||
201 | } | ||
202 | |||
203 | /** | ||
204 | * Reconnect the client and notify all pending request of the reconnect. | ||
205 | */ | ||
206 | public void reconnect() { | ||
207 | client.reconnect(); | ||
208 | currentReceive = null; | ||
209 | currentTransmit = null; | ||
210 | |||
211 | final LinkedList<Request> remove = new LinkedList<Request>(); | ||
212 | |||
213 | |||
214 | for (Request r : allRequests) { | ||
215 | boolean keep = r.onReconnect(); | ||
216 | if (!keep) { | ||
217 | remove.add(r); | ||
218 | } else { | ||
219 | // retransmit an apparently persistent request. | ||
220 | if (!requestsAwaitingTransmit.contains(r)) { | ||
221 | requestsAwaitingTransmit.add(r); | ||
222 | } | ||
223 | } | ||
224 | } | ||
225 | requestsAwaitingTransmit.removeAll(remove); | ||
226 | persistentRequests.removeAll(remove); | ||
227 | allRequests.removeAll(remove); | ||
228 | |||
229 | // only transmit, receive should only be called after the first transmit | ||
230 | handleNextTransmit(); | ||
231 | } | ||
232 | |||
233 | /** | ||
234 | * Notify all request of the shutdown. Does not actually destroy the connection. | ||
235 | */ | ||
236 | public void shutdown() { | ||
237 | final LinkedList<Request> remove = new LinkedList<Request>(); | ||
238 | |||
239 | for (Request r : allRequests) { | ||
240 | boolean keep = r.onDestroy(); | ||
241 | if (!keep) { | ||
242 | remove.add(r); | ||
243 | } else { | ||
244 | // retransmit an apparently persistent request. | ||
245 | if (!requestsAwaitingTransmit.contains(r)) { | ||
246 | requestsAwaitingTransmit.add(r); | ||
247 | } | ||
248 | } | ||
249 | } | ||
250 | requestsAwaitingTransmit.removeAll(remove); | ||
251 | persistentRequests.removeAll(remove); | ||
252 | allRequests.removeAll(remove); | ||
253 | |||
254 | handleNextTransmit(); | ||
255 | handleReceive(); | ||
256 | } | ||
257 | |||
258 | /** | ||
259 | * Cancel all requests and destroy the connection. | ||
260 | */ | ||
261 | public void destroy() { | ||
262 | destroyed = true; | ||
263 | allRequests.clear(); | ||
264 | persistentRequests.clear(); | ||
265 | requestsAwaitingTransmit.clear(); | ||
266 | if (currentTransmit != null) { | ||
267 | currentTransmit.cancel(); | ||
268 | } | ||
269 | if (currentReceive != null) { | ||
270 | currentReceive.cancel(); | ||
271 | } | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * Allow no further requests to be transmitted until the queue is unclogged. | ||
276 | */ | ||
277 | public void clog() { | ||
278 | if (clogged) { | ||
279 | throw new AssertionError("double clog"); | ||
280 | } | ||
281 | clogged = true; | ||
282 | } | ||
283 | |||
284 | /** | ||
285 | * Unclog the queue, must have been previously clogged. | ||
286 | */ | ||
287 | public void unclog() { | ||
288 | if (!clogged) { | ||
289 | throw new AssertionError("unclogg before clog"); | ||
290 | } | ||
291 | clogged = false; | ||
292 | handleNextTransmit(); | ||
293 | } | ||
294 | } | ||
diff --git a/src/org/gnunet/statistics/Statistics.java b/src/org/gnunet/statistics/Statistics.java deleted file mode 100644 index d755dd7..0000000 --- a/src/org/gnunet/statistics/Statistics.java +++ /dev/null | |||
@@ -1,408 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * The stuff below does nothing whatsoever, first milestone of | ||
23 | * this project is to implement the StatisticsService api | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | package org.gnunet.statistics; | ||
28 | |||
29 | import org.gnunet.requests.Request; | ||
30 | import org.gnunet.requests.RequestQueue; | ||
31 | import org.gnunet.util.*; | ||
32 | import org.gnunet.util.getopt.Argument; | ||
33 | import org.gnunet.util.getopt.ArgumentAction; | ||
34 | import org.slf4j.Logger; | ||
35 | import org.slf4j.LoggerFactory; | ||
36 | |||
37 | import java.util.ArrayList; | ||
38 | |||
39 | /** | ||
40 | * API for the gnunet statistics service. | ||
41 | * <p/> | ||
42 | * Set, get and monitor statistics values, represented as unsigned 64bit integer. | ||
43 | * Note that {@literal long}, java's largest primitive type, can only store signed 64bit integers. | ||
44 | * With absolute operation, its negative values are interpreted as large numbers by the statistics api. | ||
45 | */ | ||
46 | public class Statistics { | ||
47 | private static final Logger logger = LoggerFactory | ||
48 | .getLogger(Statistics.class); | ||
49 | |||
50 | /** | ||
51 | * Time after we give up on setting values in statistics | ||
52 | */ | ||
53 | private static final RelativeTime SET_TIMEOUT = RelativeTime.SECOND.multiply(10); | ||
54 | |||
55 | private final static int SETFLAG_RELATIVE = 1; | ||
56 | private final static int SETFLAG_PERSIST = 2; | ||
57 | |||
58 | private final Client client; | ||
59 | |||
60 | private final RequestQueue requestQueue; | ||
61 | |||
62 | /** | ||
63 | * Callback for the current get request. Only one get request ist allowed at a time. | ||
64 | */ | ||
65 | private StatisticsReceiver currentGetReceiver; | ||
66 | /** | ||
67 | * Success/Error continuation for the current get request. | ||
68 | */ | ||
69 | private Continuation currentGetContinuation; | ||
70 | |||
71 | /** | ||
72 | * List of all watch requests, canceled watch requests are null (but stay due to protocol limitations) | ||
73 | */ | ||
74 | private ArrayList<StatisticsWatchRequest> watchRequests = new ArrayList<StatisticsWatchRequest>(); | ||
75 | |||
76 | /** | ||
77 | * A request to the statistics service. | ||
78 | */ | ||
79 | private abstract class StatisticsRequest extends Request { | ||
80 | public String name; | ||
81 | public String subsystem; | ||
82 | } | ||
83 | |||
84 | private class StatisticsGetRequest extends StatisticsRequest { | ||
85 | public StatisticsReceiver receiver; | ||
86 | |||
87 | public void onCancel(boolean alreadyTransmitted) { | ||
88 | currentGetReceiver = null; | ||
89 | } | ||
90 | |||
91 | public void transmit(Connection.MessageSink sink) { | ||
92 | GetMessage rm = new GetMessage(); | ||
93 | rm.statisticsName = name; | ||
94 | rm.subsystemName = subsystem; | ||
95 | |||
96 | sink.send(rm); | ||
97 | } | ||
98 | |||
99 | public boolean onReconnect() { | ||
100 | return true; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | private class StatisticsSetRequest extends StatisticsRequest { | ||
105 | public long value; | ||
106 | public int flags; | ||
107 | |||
108 | public AbsoluteTime getDeadline() { | ||
109 | return SET_TIMEOUT.toAbsolute(); | ||
110 | } | ||
111 | |||
112 | public void transmit(Connection.MessageSink sink) { | ||
113 | SetMessage sm = new SetMessage(); | ||
114 | sm.statisticName = name; | ||
115 | sm.subsystemName = subsystem; | ||
116 | sm.value = value; | ||
117 | sm.flags = flags; | ||
118 | sink.send(sm); | ||
119 | } | ||
120 | |||
121 | public boolean onDestroy() { | ||
122 | // keep the request | ||
123 | return true; | ||
124 | } | ||
125 | |||
126 | public boolean onReconnect() { | ||
127 | // just keep the request on reconnect | ||
128 | return true; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | |||
133 | private class StatisticsWatchRequest extends StatisticsRequest { | ||
134 | public StatisticsReceiver receiver; | ||
135 | |||
136 | public AbsoluteTime getDeadline() { | ||
137 | return AbsoluteTime.FOREVER; | ||
138 | } | ||
139 | |||
140 | public void onCancel(boolean alreadyTransmitted) { | ||
141 | System.out.println("already transmitted: " + alreadyTransmitted); | ||
142 | if (alreadyTransmitted) { | ||
143 | watchRequests.set(watchRequests.indexOf(this), null); | ||
144 | } | ||
145 | |||
146 | } | ||
147 | |||
148 | public boolean onReconnect() { | ||
149 | // do this because we'll probably get new WatchIDs for every watch request on reconnect | ||
150 | if (watchRequests.contains(this)) { | ||
151 | watchRequests.clear(); | ||
152 | } | ||
153 | |||
154 | watchRequests.add(this); | ||
155 | |||
156 | return true; | ||
157 | } | ||
158 | |||
159 | public void transmit(Connection.MessageSink sink) { | ||
160 | WatchMessage wm = new WatchMessage(); | ||
161 | wm.statisticsName = name; | ||
162 | wm.subsystemName = subsystem; | ||
163 | sink.send(wm); | ||
164 | |||
165 | |||
166 | watchRequests.add(this); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | private static class TESTRequest extends Request { | ||
171 | private AbsoluteTime deadline; | ||
172 | |||
173 | public TESTRequest(AbsoluteTime deadline) { | ||
174 | this.deadline = deadline; | ||
175 | } | ||
176 | |||
177 | public boolean onDestroy() { | ||
178 | // keep on destroy | ||
179 | return true; | ||
180 | } | ||
181 | |||
182 | public AbsoluteTime getDeadline() { | ||
183 | return deadline; | ||
184 | } | ||
185 | |||
186 | public void transmit(Connection.MessageSink sink) { | ||
187 | sink.send(new TestMessage()); | ||
188 | // todo: disconnect when not receiving the TEST message back after timeout | ||
189 | } | ||
190 | } | ||
191 | |||
192 | |||
193 | public class StatisticsMessageReceiver extends RunaboutMessageReceiver { | ||
194 | public void visit(GetResponseMessage m) { | ||
195 | currentGetReceiver.onReceive(m.subsystemName, m.statisticName, m.value); | ||
196 | } | ||
197 | |||
198 | public void visit(GetResponseEndMessage m) { | ||
199 | currentGetReceiver = null; | ||
200 | if (currentGetContinuation != null) { | ||
201 | currentGetContinuation.cont(true); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | public void visit(TestMessage m) { | ||
206 | client.disconnect(); | ||
207 | } | ||
208 | |||
209 | public void visit(WatchResponseMessage wrm) { | ||
210 | if (watchRequests.size() <= wrm.wid) { | ||
211 | logger.warn("statistics service got confused with watch request"); | ||
212 | return; | ||
213 | } | ||
214 | StatisticsWatchRequest wr = watchRequests.get(wrm.wid); | ||
215 | // request may have been canceled by the api (but not by the server) | ||
216 | if (wr != null) { | ||
217 | wr.receiver.onReceive(wr.subsystem, wr.name, wrm.value); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | @Override | ||
222 | public void handleError() { | ||
223 | requestQueue.reconnect(); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | public Statistics(Configuration cfg) { | ||
228 | client = new Client("statistics", cfg); | ||
229 | requestQueue = new RequestQueue(this.client, new StatisticsMessageReceiver()); | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * Retrieve values from statistics. | ||
234 | * | ||
235 | * @param timeout time after we give up and call receiver.onTimeout | ||
236 | * @param subsystem the subsystem of interest | ||
237 | * @param name name of the statistics value belongs to | ||
238 | * @param receiver callback | ||
239 | * @param continuation | ||
240 | * @return handle to cancel the request | ||
241 | */ | ||
242 | public Cancelable get(RelativeTime timeout, final String subsystem, final String name, | ||
243 | final StatisticsReceiver receiver, Continuation continuation) { | ||
244 | |||
245 | if (currentGetReceiver != null) { | ||
246 | throw new AssertionError("only one Statistics get request can be active at a time"); | ||
247 | } | ||
248 | currentGetReceiver = receiver; | ||
249 | currentGetContinuation = continuation; | ||
250 | |||
251 | final StatisticsGetRequest getRequest = new StatisticsGetRequest(); | ||
252 | getRequest.setDeadline(timeout.toAbsolute()); | ||
253 | getRequest.name = (name == null) ? "" : name; | ||
254 | getRequest.subsystem = (subsystem == null) ? "" : subsystem; | ||
255 | getRequest.receiver = receiver; | ||
256 | |||
257 | return requestQueue.add(getRequest); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Sets a statistics value asynchronously. | ||
263 | * | ||
264 | * @param name name of the entry | ||
265 | * @param value desired value | ||
266 | * @param persist keep value even if the statistics service restarts | ||
267 | * @return a handle to cancel the request | ||
268 | */ | ||
269 | public Cancelable set(final String subsystem, final String name, final long value, boolean persist) { | ||
270 | StatisticsSetRequest setRequest = new StatisticsSetRequest(); | ||
271 | setRequest.setDeadline(SET_TIMEOUT.toAbsolute()); | ||
272 | setRequest.subsystem = subsystem; | ||
273 | setRequest.name = name; | ||
274 | setRequest.value = value; | ||
275 | setRequest.flags = persist ? SETFLAG_PERSIST : 0; | ||
276 | |||
277 | return requestQueue.add(setRequest); | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * Changes a statistics value asynchronously. | ||
282 | * | ||
283 | * @param name name of the entry | ||
284 | * @param delta relative difference to the old value | ||
285 | * @param persist keep value even if the statistics service restarts | ||
286 | * @return a handle to cancel the request | ||
287 | */ | ||
288 | public Cancelable update(final String subsystem, final String name, final long delta, boolean persist) { | ||
289 | StatisticsSetRequest setRequest = new StatisticsSetRequest(); | ||
290 | setRequest.setDeadline(SET_TIMEOUT.toAbsolute()); | ||
291 | setRequest.subsystem = subsystem; | ||
292 | setRequest.name = name; | ||
293 | setRequest.value = delta; | ||
294 | setRequest.flags = (persist ? SETFLAG_PERSIST : 0) | SETFLAG_RELATIVE; | ||
295 | |||
296 | return requestQueue.add(setRequest); | ||
297 | } | ||
298 | |||
299 | public Cancelable watch(final String subsystem, final String name, StatisticsReceiver receiver) { | ||
300 | StatisticsWatchRequest watchRequest = new StatisticsWatchRequest(); | ||
301 | watchRequest.setDeadline(AbsoluteTime.FOREVER); | ||
302 | watchRequest.name = name; | ||
303 | watchRequest.subsystem = subsystem; | ||
304 | watchRequest.receiver = receiver; | ||
305 | |||
306 | // even after the request has been sent, we want to keep it | ||
307 | // (e.g. for retransmission on reconnect) | ||
308 | return requestQueue.addPersistent(watchRequest); | ||
309 | } | ||
310 | |||
311 | /** | ||
312 | * Destroy handle to the statistics service. Always finishes writing pending values. | ||
313 | */ | ||
314 | public void destroy() { | ||
315 | // the request queue handles the destruction, maybe we still have important messages pending etc. | ||
316 | requestQueue.add(new TESTRequest(SET_TIMEOUT.toAbsolute())); | ||
317 | requestQueue.shutdown(); | ||
318 | } | ||
319 | |||
320 | |||
321 | /** | ||
322 | * Statistics command line utility entry point | ||
323 | * | ||
324 | * @param args command line arguments | ||
325 | */ | ||
326 | public static void main(String[] args) { | ||
327 | new Program(args) { | ||
328 | @Argument( | ||
329 | shortname = "x", | ||
330 | longname = "set", | ||
331 | action = ArgumentAction.SET, | ||
332 | description = "set a value") | ||
333 | boolean test; | ||
334 | @Argument( | ||
335 | shortname = "w", | ||
336 | longname = "watch", | ||
337 | action = ArgumentAction.SET, | ||
338 | description = "set a value") | ||
339 | boolean watch; | ||
340 | @Argument( | ||
341 | shortname = "n", | ||
342 | longname = "name", | ||
343 | action = ArgumentAction.STORE_STRING, | ||
344 | description = "statistics name") | ||
345 | String statisticsName = ""; | ||
346 | @Argument( | ||
347 | shortname = "s", | ||
348 | longname = "subsystem", | ||
349 | action = ArgumentAction.STORE_STRING, | ||
350 | description = "subsystem name") | ||
351 | String subsystemName = ""; | ||
352 | |||
353 | public void run() { | ||
354 | final Statistics statistics = new Statistics(cfg); | ||
355 | if (test) { | ||
356 | if (subsystemName.isEmpty() || statisticsName.isEmpty()) { | ||
357 | System.err.println("must specify non-empty subsystem and name"); | ||
358 | return; | ||
359 | } | ||
360 | if (unprocessedArgs.length != 1) { | ||
361 | System.err.println("must specify exactly one value to set"); | ||
362 | return; | ||
363 | } | ||
364 | long value; | ||
365 | try { | ||
366 | value = Long.parseLong(unprocessedArgs[0]); | ||
367 | } catch (NumberFormatException e) { | ||
368 | System.err.println("invalid value (not a long)"); | ||
369 | return; | ||
370 | } | ||
371 | statistics.set(subsystemName, statisticsName, value, false); | ||
372 | statistics.destroy(); | ||
373 | } else { | ||
374 | if (unprocessedArgs.length == 0) { | ||
375 | if (watch) { | ||
376 | statistics.watch(subsystemName, statisticsName, | ||
377 | new StatisticsReceiver() { | ||
378 | @Override | ||
379 | public void onReceive(String subsystem, String name, long value) { | ||
380 | System.out.println(subsystem + "(" + name + ") = " + value); | ||
381 | } | ||
382 | } | ||
383 | ); | ||
384 | } else { | ||
385 | statistics.get(RelativeTime.SECOND, subsystemName, statisticsName, | ||
386 | new StatisticsReceiver() { | ||
387 | @Override | ||
388 | public void onReceive(String subsystem, String name, long value) { | ||
389 | System.out.println(subsystem + "(" + name + ") = " + value); | ||
390 | } | ||
391 | }, | ||
392 | new Continuation() { | ||
393 | @Override | ||
394 | public void cont(boolean success) { | ||
395 | statistics.destroy(); | ||
396 | } | ||
397 | } | ||
398 | ); | ||
399 | } | ||
400 | } else { | ||
401 | System.err.println("dumping statistics does not take any positional parameters"); | ||
402 | } | ||
403 | } | ||
404 | } | ||
405 | }.start(); | ||
406 | } | ||
407 | |||
408 | } | ||
diff --git a/src/org/gnunet/transport/BlacklistCallback.java b/src/org/gnunet/transport/BlacklistCallback.java deleted file mode 100644 index a9da35f..0000000 --- a/src/org/gnunet/transport/BlacklistCallback.java +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | /** | ||
4 | * ... | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class BlacklistCallback { | ||
9 | } | ||
diff --git a/src/org/gnunet/transport/PeerIterateCallback.java b/src/org/gnunet/transport/PeerIterateCallback.java deleted file mode 100644 index da9a3ae..0000000 --- a/src/org/gnunet/transport/PeerIterateCallback.java +++ /dev/null | |||
@@ -1,9 +0,0 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | /** | ||
4 | * ... | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class PeerIterateCallback { | ||
9 | } | ||
diff --git a/src/org/gnunet/util/HelperProcess.java b/src/org/gnunet/util/HelperProcess.java deleted file mode 100644 index 5b0458f..0000000 --- a/src/org/gnunet/util/HelperProcess.java +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | /** | ||
4 | * A helper process is a process that can be started, shut down and communicated with asynchronously. | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class HelperProcess { | ||
9 | |||
10 | public void HelperProcess(String name, String... args) { | ||
11 | |||
12 | } | ||
13 | |||
14 | public void readLine() { | ||
15 | |||
16 | } | ||
17 | |||
18 | public void write(String s) { | ||
19 | |||
20 | } | ||
21 | } | ||
diff --git a/src/test/java/org/gnunet/construct/ByteFillMessage.java b/src/test/java/org/gnunet/construct/ByteFillMessage.java new file mode 100644 index 0000000..090f7d0 --- /dev/null +++ b/src/test/java/org/gnunet/construct/ByteFillMessage.java | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.construct; | ||
22 | |||
23 | |||
24 | public class ByteFillMessage implements Message { | ||
25 | |||
26 | @UInt32 | ||
27 | public int someValue; | ||
28 | |||
29 | @FillWith @UInt8 | ||
30 | public byte[] rest; | ||
31 | |||
32 | } | ||
diff --git a/src/test/java/org/gnunet/construct/ConstructTest.java b/src/test/java/org/gnunet/construct/ConstructTest.java new file mode 100644 index 0000000..ed6ac51 --- /dev/null +++ b/src/test/java/org/gnunet/construct/ConstructTest.java | |||
@@ -0,0 +1,82 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | import java.util.Random; | ||
7 | |||
8 | /** | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class ConstructTest { | ||
12 | public static class ByteFillTestMessage implements Message { | ||
13 | @FrameSize | ||
14 | @UInt32 | ||
15 | public int frameSize; | ||
16 | @FillWith @UInt8 | ||
17 | public byte[] bytes; | ||
18 | } | ||
19 | |||
20 | @Test | ||
21 | public void test_ByteFill() { | ||
22 | ByteFillTestMessage msg = new ByteFillTestMessage(); | ||
23 | msg.bytes = new byte[]{0,1,2,3}; | ||
24 | Construct.patch(msg); | ||
25 | byte[] bin = Construct.toBinary(msg); | ||
26 | |||
27 | ByteFillTestMessage msg_r = Construct.parseAs(bin, ByteFillTestMessage.class); | ||
28 | |||
29 | Assert.assertArrayEquals(new byte[]{0,1,2,3}, msg_r.bytes); | ||
30 | } | ||
31 | |||
32 | |||
33 | @Test | ||
34 | public void test_IntMessage() { | ||
35 | IntMessage im = new IntMessage(); | ||
36 | Random r = new Random(); | ||
37 | im.i1 = r.nextLong(); | ||
38 | im.i2 = r.nextLong(); | ||
39 | im.i3 = r.nextLong(); | ||
40 | im.i4 = r.nextLong(); | ||
41 | im.i5 = r.nextLong(); | ||
42 | im.i6 = r.nextLong(); | ||
43 | im.i7 = r.nextLong(); | ||
44 | im.i8 = r.nextLong(); | ||
45 | |||
46 | byte[] data = Construct.toBinary(im); | ||
47 | |||
48 | Construct.parseAs(data, IntMessage.class); | ||
49 | |||
50 | Assert.assertEquals((1+2+4+8)*2, data.length); | ||
51 | Assert.assertEquals((1+2+4+8)*2, Construct.getStaticSize(im)); | ||
52 | } | ||
53 | |||
54 | @Test(expected = AssertionError.class) | ||
55 | public void test_PrivateMemberMessage() { | ||
56 | PrivateMemberMessage m1 = new PrivateMemberMessage(); | ||
57 | byte[] data = Construct.toBinary(m1); | ||
58 | Construct.parseAs(data, PrivateMemberMessage.class); | ||
59 | } | ||
60 | |||
61 | |||
62 | @Test | ||
63 | public void test_variable_size() { | ||
64 | VariableSizeMessage m1 = new VariableSizeMessage(); | ||
65 | m1.n1 = 2; | ||
66 | m1.n2 = 3; | ||
67 | m1.a1 = new int[]{42,43}; | ||
68 | m1.a2 = new int[]{1,2,1000}; | ||
69 | |||
70 | byte[] data = Construct.toBinary(m1); | ||
71 | |||
72 | VariableSizeMessage m2 = Construct.parseAs(data, VariableSizeMessage.class); | ||
73 | |||
74 | Assert.assertEquals(m1.n1, m2.n1); | ||
75 | Assert.assertEquals(m1.n2, m2.n2); | ||
76 | Assert.assertArrayEquals(m1.a1, m2.a1); | ||
77 | Assert.assertArrayEquals(m1.a2, m2.a2); | ||
78 | } | ||
79 | |||
80 | |||
81 | } | ||
82 | |||
diff --git a/src/test/java/org/gnunet/construct/DoubleTest.java b/src/test/java/org/gnunet/construct/DoubleTest.java new file mode 100644 index 0000000..c66bd93 --- /dev/null +++ b/src/test/java/org/gnunet/construct/DoubleTest.java | |||
@@ -0,0 +1,35 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class DoubleTest { | ||
12 | public static class DoubleMessage implements Message { | ||
13 | @DoubleValue | ||
14 | public double d1; | ||
15 | @DoubleValue | ||
16 | public double d2; | ||
17 | } | ||
18 | |||
19 | @Test | ||
20 | public void test_double() { | ||
21 | DoubleMessage m = new DoubleMessage(); | ||
22 | m.d1 = 1.123; | ||
23 | m.d2 = java.lang.Double.NaN; | ||
24 | |||
25 | byte[] data = Construct.toBinary(m); | ||
26 | |||
27 | DoubleMessage m2 = Construct.parseAs(data, DoubleMessage.class); | ||
28 | |||
29 | Assert.assertEquals(m.d1, m2.d1, 0); | ||
30 | Assert.assertEquals(m.d2, m2.d2, 0); | ||
31 | |||
32 | Assert.assertEquals(8+8, data.length); | ||
33 | } | ||
34 | |||
35 | } | ||
diff --git a/src/test/java/org/gnunet/construct/FillParserTest.java b/src/test/java/org/gnunet/construct/FillParserTest.java new file mode 100644 index 0000000..d982088 --- /dev/null +++ b/src/test/java/org/gnunet/construct/FillParserTest.java | |||
@@ -0,0 +1,37 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class FillParserTest { | ||
12 | |||
13 | public static class FillTestMessage implements Message { | ||
14 | @FrameSize | ||
15 | @UInt32 | ||
16 | public int size; | ||
17 | @FillWith | ||
18 | public StringTuple[] strings; | ||
19 | } | ||
20 | |||
21 | @Test | ||
22 | public void test_fillParser() { | ||
23 | FillTestMessage m = new FillTestMessage(); | ||
24 | m.strings = new StringTuple[]{new StringTuple("foo", "bar"), new StringTuple("quux", "spam")}; | ||
25 | Construct.patch(m); | ||
26 | System.out.println(m.size); | ||
27 | byte[] data = Construct.toBinary(m); | ||
28 | Assert.assertEquals(m.size, data.length); | ||
29 | |||
30 | FillTestMessage m2 = Construct.parseAs(data, FillTestMessage.class); | ||
31 | |||
32 | Assert.assertEquals(m.strings.length, m2.strings.length); | ||
33 | |||
34 | Assert.assertEquals(m.strings[0], m2.strings[0]); | ||
35 | Assert.assertEquals(m.strings[1], m2.strings[1]); | ||
36 | } | ||
37 | } | ||
diff --git a/src/test/java/org/gnunet/construct/FixedSizeTest.java b/src/test/java/org/gnunet/construct/FixedSizeTest.java new file mode 100644 index 0000000..149862c --- /dev/null +++ b/src/test/java/org/gnunet/construct/FixedSizeTest.java | |||
@@ -0,0 +1,52 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class FixedSizeTest { | ||
12 | |||
13 | public static class Msg implements Message { | ||
14 | @UInt8 | ||
15 | public int v; | ||
16 | |||
17 | public Msg() { | ||
18 | // default ctor required by Construct | ||
19 | } | ||
20 | |||
21 | public Msg(int v) { | ||
22 | this.v = v; | ||
23 | } | ||
24 | } | ||
25 | |||
26 | public static class FixedSizeTestMessage implements Message { | ||
27 | @FixedSizeArray(length = 4) | ||
28 | public Msg[] msgs; | ||
29 | } | ||
30 | |||
31 | |||
32 | @Test | ||
33 | public void test_fixedNested() { | ||
34 | FixedSizeTestMessage m = new FixedSizeTestMessage(); | ||
35 | m.msgs = new Msg[]{new Msg(1), new Msg(2), new Msg(3), new Msg(4)}; | ||
36 | byte[] bytes = Construct.toBinary(m); | ||
37 | |||
38 | FixedSizeTestMessage m2 = Construct.parseAs(bytes, FixedSizeTestMessage.class); | ||
39 | |||
40 | Assert.assertEquals(m.msgs[0].v, m2.msgs[0].v); | ||
41 | Assert.assertEquals(m.msgs[1].v, m2.msgs[1].v); | ||
42 | Assert.assertEquals(m.msgs[2].v, m2.msgs[2].v); | ||
43 | Assert.assertEquals(m.msgs[3].v, m2.msgs[3].v); | ||
44 | } | ||
45 | |||
46 | @Test(expected = AssertionError.class) | ||
47 | public void test_sizeMismatch() { | ||
48 | FixedSizeTestMessage m = new FixedSizeTestMessage(); | ||
49 | m.msgs = new Msg[]{new Msg(1), new Msg(2)}; | ||
50 | Construct.toBinary(m); | ||
51 | } | ||
52 | } | ||
diff --git a/src/test/java/org/gnunet/construct/FrameSizeTest.java b/src/test/java/org/gnunet/construct/FrameSizeTest.java new file mode 100644 index 0000000..1aa53e2 --- /dev/null +++ b/src/test/java/org/gnunet/construct/FrameSizeTest.java | |||
@@ -0,0 +1,50 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | import org.junit.Test; | ||
6 | |||
7 | /** | ||
8 | * ... | ||
9 | * | ||
10 | * @author Florian Dold | ||
11 | */ | ||
12 | public class FrameSizeTest { | ||
13 | public static class CoordMessage implements Message { | ||
14 | @FrameSize | ||
15 | @UInt32 | ||
16 | public int size; | ||
17 | @UInt32 | ||
18 | public int x; | ||
19 | @UInt8 | ||
20 | public int y; | ||
21 | } | ||
22 | |||
23 | public static class RecursiveMessage implements Message { | ||
24 | @FrameSize | ||
25 | @UInt32 | ||
26 | public int size; | ||
27 | |||
28 | @ZeroTerminatedString | ||
29 | public String data; | ||
30 | |||
31 | @NestedMessage(newFrame = true, optional = true) | ||
32 | public RecursiveMessage rec; | ||
33 | |||
34 | } | ||
35 | |||
36 | @Test | ||
37 | public void test_simple() { | ||
38 | CoordMessage m = new CoordMessage(); | ||
39 | Construct.patch(m); | ||
40 | Assert.assertEquals(9, m.size); | ||
41 | } | ||
42 | |||
43 | |||
44 | //@Test | ||
45 | public void test_recursive_1() { | ||
46 | RecursiveMessage rm = new RecursiveMessage(); | ||
47 | rm.data = "foo"; | ||
48 | Construct.patch(rm); | ||
49 | } | ||
50 | } | ||
diff --git a/src/test/java/org/gnunet/construct/IntMessage.java b/src/test/java/org/gnunet/construct/IntMessage.java new file mode 100644 index 0000000..14971b2 --- /dev/null +++ b/src/test/java/org/gnunet/construct/IntMessage.java | |||
@@ -0,0 +1,25 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | /** | ||
4 | * ... | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class IntMessage implements Message { | ||
9 | @UInt64 | ||
10 | public long i1; | ||
11 | @UInt32 | ||
12 | public long i2; | ||
13 | @UInt16 | ||
14 | public long i3; | ||
15 | @UInt8 | ||
16 | public long i4; | ||
17 | @Int64 | ||
18 | public long i5; | ||
19 | @Int32 | ||
20 | public long i6; | ||
21 | @Int16 | ||
22 | public long i7; | ||
23 | @Int8 | ||
24 | public long i8; | ||
25 | } | ||
diff --git a/src/test/java/org/gnunet/construct/OptionalUnionTest.java b/src/test/java/org/gnunet/construct/OptionalUnionTest.java new file mode 100644 index 0000000..1c45972 --- /dev/null +++ b/src/test/java/org/gnunet/construct/OptionalUnionTest.java | |||
@@ -0,0 +1,72 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.gnunet.util.GnunetMessage; | ||
5 | import org.junit.Test; | ||
6 | |||
7 | /** | ||
8 | * ... | ||
9 | * | ||
10 | * @author Florian Dold | ||
11 | */ | ||
12 | public class OptionalUnionTest { | ||
13 | public interface TestUnion extends MessageUnion {} | ||
14 | @UnionCase(1) | ||
15 | public static class UnionMember1 implements TestUnion { | ||
16 | @ZeroTerminatedString | ||
17 | public String str; | ||
18 | } | ||
19 | @UnionCase(2) | ||
20 | public static class UnionMember2 implements TestUnion { | ||
21 | @Int32 | ||
22 | public int i; | ||
23 | } | ||
24 | |||
25 | public static class OptionalUnionMessage implements Message { | ||
26 | @FrameSize | ||
27 | @UInt32 | ||
28 | public int size; | ||
29 | @UInt32 | ||
30 | public int tag; | ||
31 | @Union(tag = "tag", optional = true) | ||
32 | public TestUnion x; | ||
33 | } | ||
34 | |||
35 | public void setupMessageMap() { | ||
36 | MessageLoader.registerUnionCase(TestUnion.class, UnionMember1.class, 1); | ||
37 | MessageLoader.registerUnionCase(TestUnion.class, UnionMember2.class, 2); | ||
38 | } | ||
39 | |||
40 | @Test | ||
41 | public void test_optional_union1() { | ||
42 | setupMessageMap(); | ||
43 | |||
44 | OptionalUnionMessage m = new OptionalUnionMessage(); | ||
45 | UnionMember1 u1 = new UnionMember1(); | ||
46 | u1.str = "foo"; | ||
47 | m.x = u1; | ||
48 | |||
49 | Construct.patch(m); | ||
50 | byte[] data = Construct.toBinary(m); | ||
51 | |||
52 | System.out.println(data.length); | ||
53 | |||
54 | OptionalUnionMessage m2 = Construct.parseAs(data, OptionalUnionMessage.class); | ||
55 | |||
56 | Assert.assertNotNull(m2.x); | ||
57 | } | ||
58 | |||
59 | @Test | ||
60 | public void test_optional_union2() { | ||
61 | setupMessageMap(); | ||
62 | |||
63 | OptionalUnionMessage m = new OptionalUnionMessage(); | ||
64 | |||
65 | byte[] data = Construct.toBinary(m); | ||
66 | |||
67 | OptionalUnionMessage m2 = Construct.parseAs(data, OptionalUnionMessage.class); | ||
68 | |||
69 | Assert.assertNull(m2.x); | ||
70 | |||
71 | } | ||
72 | } | ||
diff --git a/src/test/java/org/gnunet/construct/PrivateMemberMessage.java b/src/test/java/org/gnunet/construct/PrivateMemberMessage.java new file mode 100644 index 0000000..726d1b7 --- /dev/null +++ b/src/test/java/org/gnunet/construct/PrivateMemberMessage.java | |||
@@ -0,0 +1,13 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | /** | ||
4 | * ... | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class PrivateMemberMessage implements Message { | ||
9 | @UInt32 | ||
10 | public int foo; | ||
11 | @UInt32 | ||
12 | private int bar; | ||
13 | } | ||
diff --git a/src/test/java/org/gnunet/construct/QueryMessage.java b/src/test/java/org/gnunet/construct/QueryMessage.java new file mode 100644 index 0000000..4bf9af5 --- /dev/null +++ b/src/test/java/org/gnunet/construct/QueryMessage.java | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.construct; | ||
22 | |||
23 | public class QueryMessage implements Message { | ||
24 | |||
25 | |||
26 | @UInt8 | ||
27 | public int query; | ||
28 | |||
29 | @FillWith @UInt8 | ||
30 | public byte[] varsize; | ||
31 | } | ||
diff --git a/src/test/java/org/gnunet/construct/SendMessageTest.java b/src/test/java/org/gnunet/construct/SendMessageTest.java new file mode 100644 index 0000000..b95cc78 --- /dev/null +++ b/src/test/java/org/gnunet/construct/SendMessageTest.java | |||
@@ -0,0 +1,34 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.gnunet.core.SendMessage; | ||
4 | import org.gnunet.util.AbsoluteTime; | ||
5 | import org.gnunet.util.GnunetMessage; | ||
6 | import org.gnunet.util.PeerIdentity; | ||
7 | import org.gnunet.util.TestMessage; | ||
8 | import org.junit.Test; | ||
9 | |||
10 | /** | ||
11 | * Regression test for a message class in org.gnunet.core | ||
12 | * | ||
13 | * todo: should this test be really here? | ||
14 | * | ||
15 | * @author Florian Dold | ||
16 | */ | ||
17 | public class SendMessageTest { | ||
18 | |||
19 | @Test | ||
20 | public void test_patch() { | ||
21 | SendMessage m = new SendMessage(); | ||
22 | m.deadline = AbsoluteTime.FOREVER.asMessage(); | ||
23 | m.peer = new PeerIdentity(); // null identity | ||
24 | m.payloadMessage = new GnunetMessage(); | ||
25 | m.payloadMessage.body = new TestMessage(); | ||
26 | m.payloadMessage.header = new GnunetMessage.Header(); | ||
27 | |||
28 | GnunetMessage container = new GnunetMessage(); | ||
29 | container.body = m; | ||
30 | container.header = new GnunetMessage.Header(); | ||
31 | |||
32 | Construct.patch(container); | ||
33 | } | ||
34 | } | ||
diff --git a/src/test/java/org/gnunet/construct/StringMessage.java b/src/test/java/org/gnunet/construct/StringMessage.java new file mode 100644 index 0000000..20158a1 --- /dev/null +++ b/src/test/java/org/gnunet/construct/StringMessage.java | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.construct; | ||
22 | |||
23 | public class StringMessage implements Message { | ||
24 | @UInt8 | ||
25 | public int num; | ||
26 | |||
27 | @ZeroTerminatedString | ||
28 | public String str; | ||
29 | |||
30 | @UInt8 | ||
31 | public int num2; | ||
32 | } | ||
diff --git a/src/test/java/org/gnunet/construct/StringTest.java b/src/test/java/org/gnunet/construct/StringTest.java new file mode 100644 index 0000000..7dbedab --- /dev/null +++ b/src/test/java/org/gnunet/construct/StringTest.java | |||
@@ -0,0 +1,50 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class StringTest { | ||
12 | public static class StrMsg implements Message { | ||
13 | @FrameSize | ||
14 | @UInt32 | ||
15 | public int len; | ||
16 | @ZeroTerminatedString(optional = false) | ||
17 | public String str1; | ||
18 | @ZeroTerminatedString(optional = true) | ||
19 | public String str2; | ||
20 | } | ||
21 | |||
22 | |||
23 | @Test | ||
24 | public void test_empty() { | ||
25 | StrMsg m = new StrMsg(); | ||
26 | m.str1 = ""; | ||
27 | m.str2 = ""; | ||
28 | Construct.patch(m); | ||
29 | byte[] data = Construct.toBinary(m); | ||
30 | Assert.assertEquals(4+1+1, data.length); | ||
31 | StrMsg m2 = Construct.parseAs(data, StrMsg.class); | ||
32 | Assert.assertEquals("", m2.str1); | ||
33 | Assert.assertEquals("", m2.str2); | ||
34 | } | ||
35 | |||
36 | @Test | ||
37 | public void test_null() { | ||
38 | StrMsg m = new StrMsg(); | ||
39 | m.str1 = ""; | ||
40 | m.str2 = null; | ||
41 | Construct.patch(m); | ||
42 | byte[] data = Construct.toBinary(m); | ||
43 | Assert.assertEquals(4+1, data.length); | ||
44 | Assert.assertEquals(4+1, m.len); | ||
45 | StrMsg m2 = Construct.parseAs(data, StrMsg.class); | ||
46 | Assert.assertEquals("", m2.str1); | ||
47 | Assert.assertEquals(null, m2.str2); | ||
48 | } | ||
49 | } | ||
50 | |||
diff --git a/src/test/java/org/gnunet/construct/StringTuple.java b/src/test/java/org/gnunet/construct/StringTuple.java new file mode 100644 index 0000000..1820ae1 --- /dev/null +++ b/src/test/java/org/gnunet/construct/StringTuple.java | |||
@@ -0,0 +1,37 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import com.google.common.base.Objects; | ||
4 | import com.google.common.hash.HashCodes; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class StringTuple implements Message { | ||
12 | @ZeroTerminatedString | ||
13 | public String str1; | ||
14 | @ZeroTerminatedString | ||
15 | public String str2; | ||
16 | |||
17 | public StringTuple() { | ||
18 | // empty default ctor needed by Construct | ||
19 | } | ||
20 | public StringTuple(String str1, String str2) { | ||
21 | this.str1 = str1; | ||
22 | this.str2 = str2; | ||
23 | } | ||
24 | @Override | ||
25 | public boolean equals(Object other) { | ||
26 | if (!(other instanceof StringTuple)) { | ||
27 | return false; | ||
28 | } | ||
29 | StringTuple otherT = (StringTuple) other; | ||
30 | return otherT.str1.equals(this.str1) && otherT.str2.equals(this.str2); | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public int hashCode() { | ||
35 | return Objects.hashCode(str1, str2); | ||
36 | } | ||
37 | } | ||
diff --git a/src/test/java/org/gnunet/construct/VariableSizeArrayTest.java b/src/test/java/org/gnunet/construct/VariableSizeArrayTest.java new file mode 100644 index 0000000..e6772a1 --- /dev/null +++ b/src/test/java/org/gnunet/construct/VariableSizeArrayTest.java | |||
@@ -0,0 +1,33 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class VariableSizeArrayTest { | ||
12 | public static class VariableTestMessage implements Message { | ||
13 | @UInt32 | ||
14 | public int num; | ||
15 | @VariableSizeArray(lengthField = "num") | ||
16 | public StringTuple[] msgs; | ||
17 | } | ||
18 | |||
19 | @Test | ||
20 | public void test_variableSizeArray() { | ||
21 | VariableTestMessage m = new VariableTestMessage(); | ||
22 | m.msgs = new StringTuple[]{new StringTuple("foo", "bar"), new StringTuple("quux", "baz"), new StringTuple("spam", "eggs")}; | ||
23 | Construct.patch(m); | ||
24 | Assert.assertEquals(3, m.num); | ||
25 | byte[] data = Construct.toBinary(m); | ||
26 | VariableTestMessage m2 = Construct.parseAs(data, VariableTestMessage.class); | ||
27 | Assert.assertEquals(m2.num, 3); | ||
28 | Assert.assertEquals(m.msgs[0], m2.msgs[0]); | ||
29 | Assert.assertEquals(m.msgs[1], m2.msgs[1]); | ||
30 | Assert.assertEquals(m.msgs[2], m2.msgs[2]); | ||
31 | |||
32 | } | ||
33 | } | ||
diff --git a/src/test/java/org/gnunet/construct/VariableSizeMessage.java b/src/test/java/org/gnunet/construct/VariableSizeMessage.java new file mode 100644 index 0000000..9444108 --- /dev/null +++ b/src/test/java/org/gnunet/construct/VariableSizeMessage.java | |||
@@ -0,0 +1,17 @@ | |||
1 | package org.gnunet.construct; | ||
2 | |||
3 | /** | ||
4 | * ... | ||
5 | * | ||
6 | * @author Florian Dold | ||
7 | */ | ||
8 | public class VariableSizeMessage implements Message { | ||
9 | @UInt16 | ||
10 | public int n1; | ||
11 | @VariableSizeIntegerArray(lengthField = "n1", signed = false, bitSize = 16) | ||
12 | public int[] a1; | ||
13 | @UInt16 | ||
14 | public int n2; | ||
15 | @VariableSizeIntegerArray(lengthField = "n2", signed = false, bitSize = 16) | ||
16 | public int[] a2; | ||
17 | } | ||
diff --git a/src/test/java/org/gnunet/core/CoreTest.java b/src/test/java/org/gnunet/core/CoreTest.java new file mode 100644 index 0000000..0cb7817 --- /dev/null +++ b/src/test/java/org/gnunet/core/CoreTest.java | |||
@@ -0,0 +1,111 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.core; | ||
22 | |||
23 | |||
24 | import org.gnunet.testing.TestingFixture; | ||
25 | import org.gnunet.testing.TestingSetup; | ||
26 | import org.gnunet.testing.TestingSubsystem; | ||
27 | import org.gnunet.util.*; | ||
28 | import org.grothoff.Runabout; | ||
29 | import org.junit.Assert; | ||
30 | import org.junit.Test; | ||
31 | |||
32 | import static org.junit.Assert.assertTrue; | ||
33 | |||
34 | public class CoreTest extends TestingFixture { | ||
35 | @Test(timeout = 10000) | ||
36 | public void test_core_init() { | ||
37 | Program.configureLogging("DEBUG"); | ||
38 | TestingSubsystem ts = new TestingSubsystem("core"); | ||
39 | |||
40 | final Wrapper<Boolean> res = new Wrapper<Boolean>(false); | ||
41 | |||
42 | final Core core = new Core(ts.getConfiguration()); | ||
43 | core.observeConnect(new ConnectHandler() { | ||
44 | @Override | ||
45 | public void onConnect(PeerIdentity peerIdentity) { | ||
46 | } | ||
47 | }); | ||
48 | core.init(new InitCallback() { | ||
49 | @Override | ||
50 | public void onInit(PeerIdentity myIdentity) { | ||
51 | res.value = true; | ||
52 | System.out.println("in core init"); | ||
53 | assertTrue(myIdentity != null); | ||
54 | core.disconnect(); | ||
55 | } | ||
56 | }); | ||
57 | |||
58 | Scheduler.run(); | ||
59 | |||
60 | ts.destroy(); | ||
61 | |||
62 | assertTrue(res.value); | ||
63 | } | ||
64 | |||
65 | |||
66 | @Test(timeout = 10000) | ||
67 | public void test_core_echo() { | ||
68 | Program.configureLogging("DEBUG"); | ||
69 | new TestingSubsystem("core"); | ||
70 | new TestingSubsystem("core"); | ||
71 | new TestingSubsystem("core"); | ||
72 | new TestingSubsystem("core"); | ||
73 | TestingSubsystem ts = new TestingSubsystem("core"); | ||
74 | |||
75 | final Wrapper<Boolean> gotResponse = new Wrapper<Boolean>(false); | ||
76 | |||
77 | final Core core = new Core(ts.getConfiguration()); | ||
78 | core.setMessageHandler(new Runabout() { | ||
79 | public void visit(TestMessage t) { | ||
80 | gotResponse.set(true); | ||
81 | core.disconnect(); | ||
82 | } | ||
83 | }); | ||
84 | |||
85 | core.init(new InitCallback() { | ||
86 | @Override | ||
87 | public void onInit(PeerIdentity myIdentity) { | ||
88 | System.out.println("in core init"); | ||
89 | core.notifyTransmitReady(0, RelativeTime.FOREVER, myIdentity, 4, new MessageTransmitter() { | ||
90 | @Override | ||
91 | public void transmit(Connection.MessageSink sink) { | ||
92 | System.out.println("ntr called, calling send"); | ||
93 | sink.send(new TestMessage()); | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public void handleError() { | ||
98 | Assert.fail(); | ||
99 | } | ||
100 | }); | ||
101 | } | ||
102 | }); | ||
103 | |||
104 | Scheduler.run(); | ||
105 | |||
106 | ts.destroy(); | ||
107 | |||
108 | assertTrue(gotResponse.get()); | ||
109 | |||
110 | } | ||
111 | } | ||
diff --git a/src/test/java/org/gnunet/dht/DHTTest.java b/src/test/java/org/gnunet/dht/DHTTest.java new file mode 100644 index 0000000..9540af1 --- /dev/null +++ b/src/test/java/org/gnunet/dht/DHTTest.java | |||
@@ -0,0 +1,171 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.dht; | ||
22 | |||
23 | import static org.junit.Assert.*; | ||
24 | |||
25 | import org.gnunet.testing.TestingFixture; | ||
26 | import org.gnunet.testing.TestingSubsystem; | ||
27 | import org.gnunet.util.*; | ||
28 | import org.junit.Assert; | ||
29 | import org.junit.Test; | ||
30 | |||
31 | import java.util.EnumSet; | ||
32 | import java.util.List; | ||
33 | |||
34 | public class DHTTest extends TestingFixture { | ||
35 | @Test(timeout = 1000) | ||
36 | public void test_dht_put() { | ||
37 | Program.configureLogging(); | ||
38 | |||
39 | final Wrapper<Boolean> putFinished = new Wrapper<Boolean>(true); | ||
40 | |||
41 | TestingSubsystem ts = new TestingSubsystem("dht"); | ||
42 | |||
43 | final DistributedHashTable dht = new DistributedHashTable(ts.getConfiguration()); | ||
44 | dht.put(new HashCode("gnj-test"), new byte[]{1, 2, 3}, 1, EnumSet.noneOf(RouteOption.class), | ||
45 | BlockType.TEST.val, RelativeTime.HOUR.toAbsolute(), RelativeTime.FOREVER, new Continuation() { | ||
46 | @Override | ||
47 | public void cont(boolean success) { | ||
48 | putFinished.set(true); | ||
49 | dht.destroy(); | ||
50 | } | ||
51 | }); | ||
52 | |||
53 | Scheduler.run(); | ||
54 | Assert.assertTrue(putFinished.get()); | ||
55 | } | ||
56 | |||
57 | @Test | ||
58 | public void test_dht_put_get() { | ||
59 | Program.configureLogging(); | ||
60 | |||
61 | final Wrapper<Boolean> getFinished = new Wrapper<Boolean>(true); | ||
62 | |||
63 | TestingSubsystem ts = new TestingSubsystem("dht"); | ||
64 | |||
65 | final HashCode hash1 = new HashCode("gnj-test"); | ||
66 | final byte[] data = new byte[]{1, 2, 3}; | ||
67 | |||
68 | final DistributedHashTable dht = new DistributedHashTable(ts.getConfiguration()); | ||
69 | dht.put(hash1, data, 1, EnumSet.noneOf(RouteOption.class), | ||
70 | BlockType.TEST.val, RelativeTime.HOUR.toAbsolute(), RelativeTime.FOREVER, new Continuation() { | ||
71 | @Override | ||
72 | public void cont(boolean success) { | ||
73 | dht.startGet(RelativeTime.FOREVER, BlockType.TEST.val, hash1, 1, EnumSet.noneOf(RouteOption.class), null, new ResultCallback() { | ||
74 | @Override | ||
75 | public void handleResult(AbsoluteTime expiration, HashCode key, List<PeerIdentity> getPath, List<PeerIdentity> putPath, BlockType type, byte[] recData) { | ||
76 | assertArrayEquals(data, recData); | ||
77 | getFinished.set(true); | ||
78 | dht.destroy(); | ||
79 | } | ||
80 | }); | ||
81 | } | ||
82 | }); | ||
83 | |||
84 | Scheduler.run(); | ||
85 | Assert.assertTrue(getFinished.get()); | ||
86 | } | ||
87 | |||
88 | |||
89 | @Test(timeout = 500) | ||
90 | public void test_dht_monitor_put() { | ||
91 | Program.configureLogging(); | ||
92 | |||
93 | final Wrapper<Integer> putMonitorCount = new Wrapper<Integer>(0); | ||
94 | |||
95 | TestingSubsystem ts = new TestingSubsystem("dht"); | ||
96 | |||
97 | final HashCode hash = new HashCode("gnj-test"); | ||
98 | final byte[] data1 = new byte[]{1, 2, 3}; | ||
99 | |||
100 | final byte[] data2 = new byte[]{5, 4, 1, 2, 6}; | ||
101 | |||
102 | final DistributedHashTable dht = new DistributedHashTable(ts.getConfiguration()); | ||
103 | |||
104 | dht.startMonitor(BlockType.TEST.val, hash, null, null, new MonitorPutHandler() { | ||
105 | @Override | ||
106 | public void onPut(int options, int type, int hop_count, AbsoluteTimeMessage expirationTime, PeerIdentity[] putPath, HashCode key, byte[] data) { | ||
107 | putMonitorCount.set(putMonitorCount.get() + 1); | ||
108 | if (putMonitorCount.get() == 2) { | ||
109 | dht.destroy(); | ||
110 | } | ||
111 | } | ||
112 | }); | ||
113 | |||
114 | Scheduler.addDelayed(new RelativeTime(50), new Scheduler.Task() { | ||
115 | @Override | ||
116 | public void run(Scheduler.RunContext ctx) { | ||
117 | dht.put(hash, data1, 1, EnumSet.noneOf(RouteOption.class), BlockType.TEST.val, RelativeTime.HOUR.toAbsolute(), RelativeTime.FOREVER, null); | ||
118 | } | ||
119 | }); | ||
120 | |||
121 | |||
122 | Scheduler.addDelayed(new RelativeTime(100), new Scheduler.Task() { | ||
123 | @Override | ||
124 | public void run(Scheduler.RunContext ctx) { | ||
125 | dht.put(hash, data2, 1, EnumSet.noneOf(RouteOption.class), BlockType.TEST.val, RelativeTime.HOUR.toAbsolute(), RelativeTime.FOREVER, null); | ||
126 | } | ||
127 | }); | ||
128 | |||
129 | |||
130 | Scheduler.run(); | ||
131 | Assert.assertTrue(putMonitorCount.get() == 2); | ||
132 | } | ||
133 | |||
134 | @Test(timeout = 500) | ||
135 | public void test_dht_monitor_get() { | ||
136 | Program.configureLogging("debug"); | ||
137 | |||
138 | final Wrapper<Boolean> ok = new Wrapper<Boolean>(false); | ||
139 | |||
140 | TestingSubsystem ts = new TestingSubsystem("dht"); | ||
141 | |||
142 | final HashCode hash = new HashCode("gnj-test"); | ||
143 | |||
144 | final DistributedHashTable dht1 = new DistributedHashTable(ts.getConfiguration()); | ||
145 | final DistributedHashTable dht2 = new DistributedHashTable(ts.getConfiguration()); | ||
146 | |||
147 | dht1.startMonitor(BlockType.TEST.val, hash, new MonitorGetHandler() { | ||
148 | @Override | ||
149 | public void onGet(int options, int type, int hop_count, int desired_replication_level, PeerIdentity[] getPath, HashCode key) { | ||
150 | System.out.println("here!"); | ||
151 | ok.set(true); | ||
152 | dht2.destroy(); | ||
153 | dht1.destroy(); | ||
154 | } | ||
155 | }, null, null); | ||
156 | |||
157 | Scheduler.addDelayed(RelativeTime.fromMilliseconds(50), new Scheduler.Task() { | ||
158 | @Override | ||
159 | public void run(Scheduler.RunContext ctx) { | ||
160 | dht2.startGet(RelativeTime.FOREVER, BlockType.TEST.val, hash, 1, EnumSet.noneOf(RouteOption.class), null, null); | ||
161 | } | ||
162 | }); | ||
163 | |||
164 | |||
165 | Scheduler.run(); | ||
166 | Assert.assertTrue(ok.get()); | ||
167 | } | ||
168 | |||
169 | |||
170 | } | ||
171 | |||
diff --git a/src/test/java/org/gnunet/mesh/MeshTest.java b/src/test/java/org/gnunet/mesh/MeshTest.java new file mode 100644 index 0000000..ad5eaa3 --- /dev/null +++ b/src/test/java/org/gnunet/mesh/MeshTest.java | |||
@@ -0,0 +1,47 @@ | |||
1 | package org.gnunet.mesh; | ||
2 | |||
3 | import org.gnunet.testing.TestingFixture; | ||
4 | import org.gnunet.testing.TestingSubsystem; | ||
5 | import org.gnunet.util.*; | ||
6 | import org.junit.Test; | ||
7 | import java.lang.Void; | ||
8 | |||
9 | /** | ||
10 | * ... | ||
11 | * | ||
12 | * @author Florian Dold | ||
13 | */ | ||
14 | public class MeshTest extends TestingFixture { | ||
15 | public static class MessageHandler1 extends MeshRunabout { | ||
16 | public Mesh m1; | ||
17 | public Mesh m2; | ||
18 | public void visit(TestMessage m) { | ||
19 | m1.destroy(); | ||
20 | m2.destroy(); | ||
21 | } | ||
22 | } | ||
23 | //@Test | ||
24 | public void test_mesh_send() { | ||
25 | Program.configureLogging("DEBUG"); | ||
26 | final TestingSubsystem ts = new TestingSubsystem("mesh"); | ||
27 | Configuration cfg = ts.getConfiguration(); | ||
28 | MessageHandler1 mh = new MessageHandler1(); | ||
29 | final Mesh mesh1 = new Mesh(cfg, null, null, null); | ||
30 | final Mesh mesh2 = new Mesh(cfg, new InboundTunnelHandler() { | ||
31 | @Override | ||
32 | public void onInboundTunnel(Mesh.Tunnel tunnel, PeerIdentity initiator) { | ||
33 | |||
34 | } | ||
35 | }, new TunnelEndHandler() { | ||
36 | @Override | ||
37 | public void onTunnelEnd(Mesh.Tunnel tunnel) { | ||
38 | |||
39 | } | ||
40 | }, mh, 42); | ||
41 | mh.m1 = mesh1; | ||
42 | mh.m2 = mesh2; | ||
43 | Mesh.Tunnel<Void> tunnel = mesh2.createTunnel(null, 42, false, true, null); | ||
44 | tunnel.send(new TestMessage()); | ||
45 | Scheduler.run(); | ||
46 | } | ||
47 | } | ||
diff --git a/src/test/java/org/gnunet/nse/NSETest.java b/src/test/java/org/gnunet/nse/NSETest.java new file mode 100644 index 0000000..c3e6512 --- /dev/null +++ b/src/test/java/org/gnunet/nse/NSETest.java | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.nse; | ||
22 | |||
23 | import org.gnunet.testing.TestingSetup; | ||
24 | import org.gnunet.testing.TestingSubsystem; | ||
25 | import org.gnunet.util.AbsoluteTime; | ||
26 | import org.gnunet.util.Scheduler; | ||
27 | import org.gnunet.util.Wrapper; | ||
28 | import org.junit.Test; | ||
29 | |||
30 | import static org.junit.Assert.assertNotNull; | ||
31 | import static org.junit.Assert.assertTrue; | ||
32 | |||
33 | /** | ||
34 | * @author Florian Dold | ||
35 | */ | ||
36 | public class NSETest { | ||
37 | @Test | ||
38 | public void test_nse() { | ||
39 | final Wrapper<Boolean> gotResult = new Wrapper<Boolean>(false); | ||
40 | TestingSubsystem ts = new TestingSubsystem("nse"); | ||
41 | |||
42 | final NetworkSizeEstimation nse = new NetworkSizeEstimation(ts.getConfiguration()); | ||
43 | nse.subscribe(new NetworkSizeEstimation.Subscriber() { | ||
44 | @Override | ||
45 | public void update(AbsoluteTime timestamp, double estimate, double deviation) { | ||
46 | assertNotNull(timestamp); | ||
47 | gotResult.set(true); | ||
48 | nse.disconnect(); | ||
49 | } | ||
50 | }); | ||
51 | |||
52 | Scheduler.run(); | ||
53 | |||
54 | assertTrue(gotResult.get()); | ||
55 | } | ||
56 | } | ||
diff --git a/src/test/java/org/gnunet/peerinfo/PeerInfoTest.java b/src/test/java/org/gnunet/peerinfo/PeerInfoTest.java new file mode 100644 index 0000000..4239731 --- /dev/null +++ b/src/test/java/org/gnunet/peerinfo/PeerInfoTest.java | |||
@@ -0,0 +1,41 @@ | |||
1 | package org.gnunet.peerinfo; | ||
2 | |||
3 | import org.gnunet.hello.HelloMessage; | ||
4 | import org.gnunet.testing.TestingSubsystem; | ||
5 | import org.gnunet.util.*; | ||
6 | import org.junit.Test; | ||
7 | |||
8 | import static org.junit.Assert.assertNotNull; | ||
9 | import static org.junit.Assert.assertTrue; | ||
10 | |||
11 | /** | ||
12 | * ... | ||
13 | * | ||
14 | * @author Florian Dold | ||
15 | */ | ||
16 | public class PeerInfoTest { | ||
17 | |||
18 | @Test | ||
19 | public void test_peerinfo() { | ||
20 | Program.configureLogging("debug"); | ||
21 | final Wrapper<Boolean> ended = new Wrapper<Boolean>(false); | ||
22 | TestingSubsystem ts = new TestingSubsystem("peerinfo"); | ||
23 | final PeerInfo peerInfo = new PeerInfo(ts.getConfiguration()); | ||
24 | |||
25 | peerInfo.iterate(RelativeTime.FOREVER, false, new PeerProcessor() { | ||
26 | @Override | ||
27 | public void onPeer(PeerIdentity peerIdentity, HelloMessage hello) { | ||
28 | // we can't expect to get anything here, peerinfo is the only running service in this setup | ||
29 | } | ||
30 | |||
31 | @Override | ||
32 | public void onEnd() { | ||
33 | peerInfo.disconnect(); | ||
34 | ended.set(true); | ||
35 | } | ||
36 | }); | ||
37 | |||
38 | Scheduler.run(); | ||
39 | assertTrue(ended.get()); | ||
40 | } | ||
41 | } | ||
diff --git a/src/test/java/org/gnunet/statistics/StatisticsTest.java b/src/test/java/org/gnunet/statistics/StatisticsTest.java new file mode 100644 index 0000000..f4bdbe9 --- /dev/null +++ b/src/test/java/org/gnunet/statistics/StatisticsTest.java | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.statistics; | ||
22 | |||
23 | import org.junit.Assert; | ||
24 | import org.gnunet.testing.TestingFixture; | ||
25 | import org.gnunet.testing.TestingSetup; | ||
26 | import org.gnunet.testing.TestingSubsystem; | ||
27 | import org.gnunet.util.*; | ||
28 | import org.junit.Test; | ||
29 | |||
30 | |||
31 | public class StatisticsTest extends TestingFixture { | ||
32 | public interface Next { | ||
33 | void next(); | ||
34 | } | ||
35 | |||
36 | public void assertStatisticsGet(AssertionList assertions, Statistics stat, final String subsystem, | ||
37 | final String name, | ||
38 | final long expectedValue, final Next next) { | ||
39 | Program.configureLogging(); | ||
40 | final Assertion getAssertion = assertions.create("get %s:%s -> %s", subsystem, name, expectedValue); | ||
41 | stat.get(RelativeTime.FOREVER, subsystem, name, new StatisticsReceiver() { | ||
42 | @Override | ||
43 | public void onReceive(String rsubsystem, String rname, long rvalue) { | ||
44 | if (rsubsystem.equals(subsystem) && rname.equals(name) && rvalue == expectedValue) { | ||
45 | getAssertion.assertTrue(true); | ||
46 | } else { | ||
47 | getAssertion.assertTrue(false); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public void onTimeout() { | ||
53 | Assert.fail(); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public void onDone() { | ||
58 | if (next != null) { | ||
59 | next.next(); | ||
60 | } | ||
61 | } | ||
62 | }); | ||
63 | } | ||
64 | |||
65 | |||
66 | @Test(timeout = 1000) | ||
67 | public void test_simple() { | ||
68 | Program.configureLogging("DEBUG"); | ||
69 | final TestingSubsystem ts = new TestingSubsystem("statistics"); | ||
70 | |||
71 | final Statistics stat = new Statistics(ts.getConfiguration()); | ||
72 | |||
73 | final Wrapper<Boolean> contReached = new Wrapper<Boolean>(false); | ||
74 | |||
75 | stat.set("gnj-test", "test", 42, false); | ||
76 | |||
77 | stat.get(RelativeTime.FOREVER, "gnj-test", "test", | ||
78 | new StatisticsReceiver() { | ||
79 | @Override | ||
80 | public void onReceive(String subsystem, String name, long value) { | ||
81 | Assert.assertEquals("gnj-test", subsystem); | ||
82 | Assert.assertEquals("test", name); | ||
83 | Assert.assertEquals(42, value); | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public void onTimeout() { | ||
88 | |||
89 | } | ||
90 | |||
91 | public void onDone() { | ||
92 | contReached.set(true); | ||
93 | stat.destroy(); | ||
94 | |||
95 | } | ||
96 | }); | ||
97 | |||
98 | Scheduler.run(); | ||
99 | |||
100 | Assert.assertTrue(contReached.get()); | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Test setting, updating and getting values with two statistics handles. | ||
105 | * This test is somewhat fragile, as we have a fixed time we wait for | ||
106 | * the values to be set in the service. | ||
107 | */ | ||
108 | @Test(timeout = 1000) | ||
109 | public void test_statistics_get_set() { | ||
110 | Program.configureLogging(); | ||
111 | final TestingSubsystem ts = new TestingSubsystem("statistics"); | ||
112 | |||
113 | final AssertionList assertions = new AssertionList(); | ||
114 | |||
115 | final Statistics stat1 = new Statistics(ts.getConfiguration()); | ||
116 | |||
117 | stat1.set("gnj-test", "test1", 5, false); | ||
118 | stat1.set("gnj-test", "test2", 7, false); | ||
119 | stat1.set("gnj-test", "test3", 0, false); | ||
120 | stat1.update("gnj-test", "test3", 5, false); | ||
121 | stat1.update("gnj-test", "test3", -1, false); | ||
122 | |||
123 | Scheduler.addDelayed(RelativeTime.fromMilliseconds(200), new Scheduler.Task() { | ||
124 | @Override | ||
125 | public void run(Scheduler.RunContext ctx) { | ||
126 | stat1.destroy(); | ||
127 | final Statistics stat2 = new Statistics(ts.getConfiguration()); | ||
128 | assertStatisticsGet(assertions, stat2, "gnj-test", "test1", 5, new Next() { | ||
129 | @Override | ||
130 | public void next() { | ||
131 | assertStatisticsGet(assertions, stat2, "gnj-test", "test2", 7, new Next() { | ||
132 | @Override | ||
133 | public void next() { | ||
134 | assertStatisticsGet(assertions, stat2, "gnj-test", "test3", 4, new Next() { | ||
135 | @Override | ||
136 | public void next() { | ||
137 | stat2.destroy(); | ||
138 | } | ||
139 | }); | ||
140 | } | ||
141 | }); | ||
142 | |||
143 | } | ||
144 | }); | ||
145 | } | ||
146 | }); | ||
147 | |||
148 | |||
149 | Scheduler.run(); | ||
150 | assertions.assertAll(); | ||
151 | } | ||
152 | |||
153 | |||
154 | @Test(timeout = 1000) | ||
155 | public void test_watch() { | ||
156 | Program.configureLogging("DEBUG"); | ||
157 | final TestingSubsystem ts = new TestingSubsystem("statistics"); | ||
158 | |||
159 | final Statistics stat = new Statistics(ts.getConfiguration()); | ||
160 | |||
161 | stat.watch("gnj-test", "test", new StatisticsWatcher() { | ||
162 | @Override | ||
163 | public void onReceive(String subsystem, String name, long value) { | ||
164 | System.out.println("received update!"); | ||
165 | stat.destroy(); | ||
166 | } | ||
167 | |||
168 | @Override | ||
169 | public void onTimeout() { | ||
170 | Assert.fail(); | ||
171 | } | ||
172 | }); | ||
173 | |||
174 | // the set is sent after the watch, thus it is guaranteed we | ||
175 | // well get the update | ||
176 | stat.set("gnj-test", "test", 42, false); | ||
177 | |||
178 | Scheduler.run(); | ||
179 | } | ||
180 | } | ||
diff --git a/src/test/java/org/gnunet/testing/TestingSetupTest.java b/src/test/java/org/gnunet/testing/TestingSetupTest.java new file mode 100644 index 0000000..263d88f --- /dev/null +++ b/src/test/java/org/gnunet/testing/TestingSetupTest.java | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.testing; | ||
22 | |||
23 | import org.gnunet.util.Program; | ||
24 | import org.junit.Test; | ||
25 | |||
26 | /** | ||
27 | * @author Florian Dold | ||
28 | */ | ||
29 | public class TestingSetupTest { | ||
30 | static final String service = "nse"; | ||
31 | |||
32 | @Test(timeout = 1000) | ||
33 | public void test_testing() { | ||
34 | Program.configureLogging(); | ||
35 | // could be any service, just use statistics | ||
36 | TestingSubsystem ts = new TestingSubsystem(service); | ||
37 | String port = ts.getConfiguration().getValueString(service, "PORT").get(); | ||
38 | org.junit.Assert.assertTrue(port != null); | ||
39 | |||
40 | ts.destroy(); | ||
41 | } | ||
42 | |||
43 | @Test(expected = TestingSetup.SetupException.class) | ||
44 | public void test_no_service() { | ||
45 | new TestingSubsystem("foobar _ !!!"); | ||
46 | } | ||
47 | |||
48 | @Test(timeout = 1000) | ||
49 | public void test_restart() { | ||
50 | TestingSubsystem ts = new TestingSubsystem(service); | ||
51 | ts.restart(); | ||
52 | ts.destroy(); | ||
53 | } | ||
54 | } | ||
diff --git a/src/test/java/org/gnunet/util/Assertion.java b/src/test/java/org/gnunet/util/Assertion.java new file mode 100644 index 0000000..3636aa8 --- /dev/null +++ b/src/test/java/org/gnunet/util/Assertion.java | |||
@@ -0,0 +1,37 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | |||
24 | public class Assertion { | ||
25 | public boolean success; | ||
26 | public int asserted; | ||
27 | public String message; | ||
28 | |||
29 | public Assertion(String message) { | ||
30 | this.message = message; | ||
31 | } | ||
32 | |||
33 | public void assertTrue(boolean b) { | ||
34 | success = b; | ||
35 | asserted += 1; | ||
36 | } | ||
37 | } | ||
diff --git a/src/test/java/org/gnunet/util/AssertionList.java b/src/test/java/org/gnunet/util/AssertionList.java new file mode 100644 index 0000000..d80ed0e --- /dev/null +++ b/src/test/java/org/gnunet/util/AssertionList.java | |||
@@ -0,0 +1,30 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import java.util.LinkedList; | ||
4 | import java.util.List; | ||
5 | |||
6 | public class AssertionList { | ||
7 | |||
8 | List<Assertion> asyncAssertions = new LinkedList<Assertion>(); | ||
9 | |||
10 | |||
11 | public Assertion create(String message, Object... args) { | ||
12 | Assertion assertion = new Assertion(String.format(message, args)); | ||
13 | asyncAssertions.add(assertion); | ||
14 | return assertion; | ||
15 | } | ||
16 | |||
17 | public void assertAll() { | ||
18 | for (Assertion assertion : asyncAssertions) { | ||
19 | if (assertion.asserted != 1) { | ||
20 | throw new AssertionError( | ||
21 | String.format("Assertion '%s' asserted %s times", assertion.message, assertion.asserted)); | ||
22 | } | ||
23 | if (!assertion.success) { | ||
24 | throw new AssertionError( | ||
25 | String.format("Assertion '%s' failed", assertion.message)); | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | |||
30 | } | ||
diff --git a/src/test/java/org/gnunet/util/ClientServerTest.java b/src/test/java/org/gnunet/util/ClientServerTest.java new file mode 100644 index 0000000..616ce98 --- /dev/null +++ b/src/test/java/org/gnunet/util/ClientServerTest.java | |||
@@ -0,0 +1,251 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import com.google.common.collect.Lists; | ||
4 | import org.gnunet.construct.UInt32; | ||
5 | import org.gnunet.construct.UnionCase; | ||
6 | import org.gnunet.testing.TestingServer; | ||
7 | import org.gnunet.testing.TestingSetup; | ||
8 | import org.junit.Assert; | ||
9 | import org.junit.Test; | ||
10 | |||
11 | import java.io.IOException; | ||
12 | import java.net.*; | ||
13 | import java.nio.ByteBuffer; | ||
14 | import java.nio.channels.Pipe; | ||
15 | import java.nio.channels.spi.SelectorProvider; | ||
16 | import java.util.ArrayList; | ||
17 | |||
18 | /** | ||
19 | * ... | ||
20 | * | ||
21 | * @author Florian Dold | ||
22 | */ | ||
23 | public class ClientServerTest { | ||
24 | |||
25 | @Test | ||
26 | public void test_start_stop() { | ||
27 | Program.configureLogging("DEBUG", null); | ||
28 | final TestingServer srv = new TestingServer(); | ||
29 | srv.server.stopListening(); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Test if the server receives a message sent by a client. | ||
34 | */ | ||
35 | @Test | ||
36 | public void test_testing_server() { | ||
37 | Program.configureLogging("DEBUG", null); | ||
38 | |||
39 | final TestingServer srv = new TestingServer(); | ||
40 | |||
41 | final Wrapper<Boolean> gotMessage = new Wrapper<Boolean>(false); | ||
42 | |||
43 | srv.server.setHandler(new Server.MessageRunabout() { | ||
44 | public void visit(TestMessage tm) { | ||
45 | gotMessage.set(true); | ||
46 | srv.server.destroy(); | ||
47 | } | ||
48 | }); | ||
49 | |||
50 | Scheduler.run(new Scheduler.Task() { | ||
51 | @Override | ||
52 | public void run(Scheduler.RunContext ctx) { | ||
53 | final Client cli = srv.createClient(); | ||
54 | cli.notifyTransmitReady(RelativeTime.FOREVER,true, 0, new MessageTransmitter() { | ||
55 | @Override | ||
56 | public void transmit(Connection.MessageSink sink) { | ||
57 | System.out.println("ntr!"); | ||
58 | sink.send(new TestMessage()); | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public void handleError() { | ||
63 | Assert.fail(); | ||
64 | } | ||
65 | }); | ||
66 | System.out.println("done"); | ||
67 | } | ||
68 | }); | ||
69 | |||
70 | Assert.assertTrue(gotMessage.get()); | ||
71 | } | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Test what happens when a client calls notifyTransmitReady, but does not send | ||
76 | * a message in the callback and disconnects. | ||
77 | */ | ||
78 | @Test(timeout = 1000) | ||
79 | public void test_premature_disconnect() { | ||
80 | Program.configureLogging("DEBUG", null); | ||
81 | final TestingServer srv = new TestingServer(); | ||
82 | |||
83 | srv.server.notifyDisconnect(new Server.DisconnectHandler() { | ||
84 | @Override | ||
85 | public void onDisconnect(Server.ClientHandle clientHandle) { | ||
86 | srv.server.destroy(); | ||
87 | } | ||
88 | }); | ||
89 | |||
90 | Scheduler.run(new Scheduler.Task() { | ||
91 | @Override | ||
92 | public void run(Scheduler.RunContext ctx) { | ||
93 | final Client cli = srv.createClient(); | ||
94 | cli.notifyTransmitReady(RelativeTime.FOREVER,true, 0, new MessageTransmitter() { | ||
95 | @Override | ||
96 | public void transmit(Connection.MessageSink sink) { | ||
97 | sink.send(new TestMessage()); | ||
98 | cli.disconnect(); | ||
99 | } | ||
100 | |||
101 | @Override | ||
102 | public void handleError() { | ||
103 | Assert.fail(); | ||
104 | } | ||
105 | }); | ||
106 | } | ||
107 | }); | ||
108 | } | ||
109 | |||
110 | |||
111 | @Test | ||
112 | public void test_receiveDone() { | ||
113 | Program.configureLogging("DEBUG", null); | ||
114 | final TestingServer srv = new TestingServer(); | ||
115 | |||
116 | final Wrapper<Integer> msgCount = new Wrapper<Integer>(0); | ||
117 | |||
118 | srv.server.setHandler(new Server.MessageRunabout() { | ||
119 | public void visit(TestMessage tm) { | ||
120 | msgCount.set(msgCount.get() + 1); | ||
121 | if (msgCount.get() == 3) { | ||
122 | getSender().receiveDone(false); | ||
123 | srv.server.stopListening(); | ||
124 | } else { | ||
125 | getSender().receiveDone(true); | ||
126 | } | ||
127 | } | ||
128 | }); | ||
129 | |||
130 | Scheduler.run(new Scheduler.Task() { | ||
131 | @Override | ||
132 | public void run(Scheduler.RunContext ctx) { | ||
133 | final Client cli = srv.createClient(); | ||
134 | |||
135 | cli.transmitWhenReady(RelativeTime.FOREVER, new TestMessage(), new Continuation() { | ||
136 | @Override | ||
137 | public void cont(boolean success) { | ||
138 | cli.transmitWhenReady(RelativeTime.FOREVER, new TestMessage(), new Continuation() { | ||
139 | @Override | ||
140 | public void cont(boolean success) { | ||
141 | cli.transmitWhenReady(RelativeTime.FOREVER, new TestMessage(), null); | ||
142 | } | ||
143 | }); | ||
144 | } | ||
145 | }); | ||
146 | } | ||
147 | }); | ||
148 | } | ||
149 | |||
150 | @Test | ||
151 | public void test_acceptFromAddresses() { | ||
152 | Program.configureLogging("DEBUG", null); | ||
153 | |||
154 | InetAddress localhost = null; | ||
155 | try { | ||
156 | localhost = Inet4Address.getLocalHost(); | ||
157 | } catch (UnknownHostException e) { | ||
158 | Assert.fail(); | ||
159 | } | ||
160 | |||
161 | // does this work on all operating systems? | ||
162 | SocketAddress addr = new InetSocketAddress(localhost, 0); | ||
163 | |||
164 | Server server = new Server(Lists.newArrayList(addr), RelativeTime.FOREVER, false); | ||
165 | |||
166 | server.destroy(); | ||
167 | |||
168 | } | ||
169 | |||
170 | |||
171 | @Test | ||
172 | public void test_keep_drop() { | ||
173 | Program.configureLogging("DEBUG", null); | ||
174 | final TestingServer srv = new TestingServer(); | ||
175 | |||
176 | final Wrapper<Integer> msgCount = new Wrapper<Integer>(0); | ||
177 | |||
178 | |||
179 | |||
180 | srv.server.setHandler(new Server.MessageRunabout() { | ||
181 | public void visit(TestMessage tm) { | ||
182 | srv.server.stopListening(); | ||
183 | if (msgCount.get() == 0) { | ||
184 | getSender().keep(); | ||
185 | getSender().drop(); | ||
186 | getSender().receiveDone(true); | ||
187 | } else if (msgCount.get() == 1) { | ||
188 | getSender().receiveDone(false); | ||
189 | |||
190 | } else { | ||
191 | Assert.fail(); | ||
192 | } | ||
193 | |||
194 | msgCount.set(msgCount.get() + 1); | ||
195 | } | ||
196 | }); | ||
197 | |||
198 | Scheduler.run(new Scheduler.Task() { | ||
199 | @Override | ||
200 | public void run(Scheduler.RunContext ctx) { | ||
201 | final Client cli = srv.createClient(); | ||
202 | cli.transmitWhenReady(new TestMessage(), new Continuation() { | ||
203 | @Override | ||
204 | public void cont(boolean success) { | ||
205 | cli.transmitWhenReady(new TestMessage(), null); | ||
206 | } | ||
207 | }); | ||
208 | } | ||
209 | }); | ||
210 | } | ||
211 | |||
212 | |||
213 | |||
214 | /** | ||
215 | * test if markMonitor / soft shutdown works. | ||
216 | */ | ||
217 | @Test | ||
218 | public void test_monitor_clients() { | ||
219 | Program.configureLogging("DEBUG", null); | ||
220 | final TestingServer srv = new TestingServer(); | ||
221 | |||
222 | final Wrapper<Integer> msgCount = new Wrapper<Integer>(0); | ||
223 | |||
224 | srv.server.setHandler(new Server.MessageRunabout() { | ||
225 | public void visit(TestMessage tm) { | ||
226 | if (msgCount.get() == 0) { | ||
227 | getSender().markMonitor(); | ||
228 | getSender().receiveDone(true); | ||
229 | } else if (msgCount.get() == 1) { | ||
230 | srv.server.stopListening(); | ||
231 | getSender().receiveDone(false); | ||
232 | } else { | ||
233 | Assert.fail(); | ||
234 | } | ||
235 | |||
236 | msgCount.set(msgCount.get() + 1); | ||
237 | } | ||
238 | }); | ||
239 | |||
240 | Scheduler.run(new Scheduler.Task() { | ||
241 | @Override | ||
242 | public void run(Scheduler.RunContext ctx) { | ||
243 | final Client cli1 = srv.createClient(); | ||
244 | final Client cli2 = srv.createClient(); | ||
245 | |||
246 | cli1.transmitWhenReady(new TestMessage(), null); | ||
247 | cli2.transmitWhenReady(new TestMessage(), null); | ||
248 | } | ||
249 | }); | ||
250 | } | ||
251 | } | ||
diff --git a/src/test/java/org/gnunet/util/FilePipeExample.java b/src/test/java/org/gnunet/util/FilePipeExample.java new file mode 100644 index 0000000..94696c9 --- /dev/null +++ b/src/test/java/org/gnunet/util/FilePipeExample.java | |||
@@ -0,0 +1,41 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import java.io.File; | ||
4 | import java.io.IOError; | ||
5 | import java.io.IOException; | ||
6 | import java.nio.ByteBuffer; | ||
7 | |||
8 | /** | ||
9 | * ... | ||
10 | * | ||
11 | * @author Florian Dold | ||
12 | */ | ||
13 | public class FilePipeExample { | ||
14 | public static void main(String... args) { | ||
15 | |||
16 | Program.configureLogging("DEBUG", null); | ||
17 | |||
18 | final Scheduler.FilePipe fp = Scheduler.openFilePipe(new File("test.pipe")); | ||
19 | |||
20 | |||
21 | Scheduler.addRead(RelativeTime.FOREVER, fp.getSource(), new Scheduler.Task() { | ||
22 | @Override | ||
23 | public void run(Scheduler.RunContext ctx) { | ||
24 | ByteBuffer b = ByteBuffer.allocate(1); | ||
25 | b.clear(); | ||
26 | try { | ||
27 | fp.getSource().read(b); | ||
28 | } catch (IOException e) { | ||
29 | throw new IOError(e); | ||
30 | } | ||
31 | b.flip(); | ||
32 | System.out.println("got: " + b.get()); | ||
33 | |||
34 | Scheduler.addRead(RelativeTime.FOREVER, fp.getSource(), this); | ||
35 | |||
36 | } | ||
37 | }); | ||
38 | |||
39 | Scheduler.run(); | ||
40 | } | ||
41 | } | ||
diff --git a/src/test/java/org/gnunet/util/ResolverTest.java b/src/test/java/org/gnunet/util/ResolverTest.java new file mode 100644 index 0000000..ae56b79 --- /dev/null +++ b/src/test/java/org/gnunet/util/ResolverTest.java | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import org.gnunet.testing.TestingSetup; | ||
24 | import org.gnunet.testing.TestingSubsystem; | ||
25 | import org.junit.Assert; | ||
26 | import org.junit.Test; | ||
27 | import org.slf4j.Logger; | ||
28 | import org.slf4j.LoggerFactory; | ||
29 | |||
30 | import java.net.InetAddress; | ||
31 | |||
32 | import static org.junit.Assert.assertTrue; | ||
33 | import static org.junit.Assert.fail; | ||
34 | |||
35 | /** | ||
36 | * @author Florian Dold | ||
37 | */ | ||
38 | public class ResolverTest { | ||
39 | private static final Logger logger = LoggerFactory | ||
40 | .getLogger(ResolverTest.class); | ||
41 | @Test | ||
42 | public void test_resolver() { | ||
43 | final Wrapper<Boolean> finished1 = new Wrapper<Boolean>(true); | ||
44 | final Wrapper<Boolean> finished2 = new Wrapper<Boolean>(true); | ||
45 | |||
46 | TestingSubsystem ts = new TestingSubsystem("resolver"); | ||
47 | |||
48 | |||
49 | Resolver r = Resolver.getInstance(); | ||
50 | r.setConfiguration(ts.getConfiguration()); | ||
51 | |||
52 | r.resolveHostname("gnunet.org", RelativeTime.FOREVER, new Resolver.AddressCallback() { | ||
53 | @Override | ||
54 | public void onAddress(InetAddress addr) { | ||
55 | logger.info("Hostname resolved to " + addr.getHostAddress()); | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public void onFinished() { | ||
60 | finished1.set(true); | ||
61 | } | ||
62 | |||
63 | @Override | ||
64 | public void onTimeout() { | ||
65 | fail(); | ||
66 | } | ||
67 | }); | ||
68 | r.resolveHostname("gnu.org", RelativeTime.FOREVER, new Resolver.AddressCallback() { | ||
69 | @Override | ||
70 | public void onAddress(InetAddress addr) { | ||
71 | logger.info("Hostname resolved to " + addr.getHostAddress()); | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public void onFinished() { | ||
76 | finished2.set(true); | ||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public void onTimeout() { | ||
81 | fail(); | ||
82 | } | ||
83 | }); | ||
84 | |||
85 | Scheduler.run(); | ||
86 | |||
87 | assertTrue(finished1.get()); | ||
88 | |||
89 | assertTrue(finished2.get()); | ||
90 | } | ||
91 | } | ||
92 | |||
diff --git a/src/test/java/org/gnunet/util/ServerExample.java b/src/test/java/org/gnunet/util/ServerExample.java new file mode 100644 index 0000000..a344e2a --- /dev/null +++ b/src/test/java/org/gnunet/util/ServerExample.java | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | import java.net.InetSocketAddress; | ||
24 | import java.net.SocketAddress; | ||
25 | import java.util.Arrays; | ||
26 | |||
27 | import static org.gnunet.util.Server.*; | ||
28 | |||
29 | /** | ||
30 | * Example server implementation. | ||
31 | * | ||
32 | * @author Florian Dold | ||
33 | */ | ||
34 | public class ServerExample { | ||
35 | |||
36 | public static void main(String[] args) { | ||
37 | // usually servers should run inside a service, this is just for testing | ||
38 | new Program(args) { | ||
39 | @Override | ||
40 | public void run() { | ||
41 | Server s = new Server(Arrays.asList(new SocketAddress[]{new InetSocketAddress("127.0.0.1", 3456)}), | ||
42 | RelativeTime.MINUTE, | ||
43 | false); | ||
44 | s.setHandler(new Server.MessageRunabout() { | ||
45 | public void visit(TestMessage tm) { | ||
46 | System.out.println("got TEST message"); | ||
47 | final Server.ClientHandle sender = getSender(); | ||
48 | sender.notifyTransmitReady(4, RelativeTime.FOREVER, new MessageTransmitter() { | ||
49 | @Override | ||
50 | public void transmit(Connection.MessageSink sink) { | ||
51 | sink.send(new TestMessage()); | ||
52 | System.out.println("TEST message sent"); | ||
53 | sender.receiveDone(true); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public void handleError() { | ||
58 | System.out.println("error talking to client!"); | ||
59 | } | ||
60 | }); | ||
61 | } | ||
62 | |||
63 | public void visit(UnknownMessageBody b) { | ||
64 | System.out.println("got message of unknown type " + b.id); | ||
65 | } | ||
66 | }); | ||
67 | |||
68 | s.notifyDisconnect(new DisconnectHandler() { | ||
69 | @Override | ||
70 | public void onDisconnect(Server.ClientHandle clientHandle) { | ||
71 | System.out.println("client disconnected"); | ||
72 | |||
73 | } | ||
74 | }); | ||
75 | |||
76 | } | ||
77 | }.start(); | ||
78 | } | ||
79 | } | ||
diff --git a/src/test/java/org/gnunet/util/StringsTest.java b/src/test/java/org/gnunet/util/StringsTest.java new file mode 100644 index 0000000..49cbfe9 --- /dev/null +++ b/src/test/java/org/gnunet/util/StringsTest.java | |||
@@ -0,0 +1,20 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class StringsTest { | ||
12 | @Test | ||
13 | public void test_inverse() { | ||
14 | byte[] data = "asdfgASDD$!123".getBytes(); | ||
15 | String str = Strings.dataToString(data); | ||
16 | byte[] data2 = Strings.stringToData(str, data.length); | ||
17 | Assert.assertArrayEquals(data, data2); | ||
18 | } | ||
19 | } | ||
20 | |||
diff --git a/src/test/java/org/gnunet/util/TimeTest.java b/src/test/java/org/gnunet/util/TimeTest.java new file mode 100644 index 0000000..92115a8 --- /dev/null +++ b/src/test/java/org/gnunet/util/TimeTest.java | |||
@@ -0,0 +1,29 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import org.junit.Assert; | ||
4 | import org.junit.Test; | ||
5 | |||
6 | /** | ||
7 | * ... | ||
8 | * | ||
9 | * @author Florian Dold | ||
10 | */ | ||
11 | public class TimeTest { | ||
12 | |||
13 | @Test | ||
14 | public void test_absolute_add_subtract() { | ||
15 | AbsoluteTime t1 = AbsoluteTime.now().add(RelativeTime.FOREVER); | ||
16 | Assert.assertEquals(t1, AbsoluteTime.FOREVER); | ||
17 | Assert.assertTrue(t1.isForever()); | ||
18 | |||
19 | t1 = AbsoluteTime.FOREVER.add(RelativeTime.SECOND); | ||
20 | Assert.assertEquals(t1, AbsoluteTime.FOREVER); | ||
21 | Assert.assertTrue(t1.isForever()); | ||
22 | |||
23 | AbsoluteTime t2 = (new AbsoluteTime(123000000)).add(RelativeTime.SECOND); | ||
24 | Assert.assertEquals(124000000, t2.getMicroseconds()); | ||
25 | |||
26 | AbsoluteTime t3 = (new AbsoluteTime(123000000)).subtract(RelativeTime.SECOND); | ||
27 | Assert.assertEquals(122000000, t3.getMicroseconds()); | ||
28 | } | ||
29 | } | ||
diff --git a/src/test/java/org/gnunet/util/Wrapper.java b/src/test/java/org/gnunet/util/Wrapper.java new file mode 100644 index 0000000..be84b54 --- /dev/null +++ b/src/test/java/org/gnunet/util/Wrapper.java | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util; | ||
22 | |||
23 | |||
24 | /** | ||
25 | * Wrapper, used when it is necessary to store a value in a final-variable. | ||
26 | * | ||
27 | * @param <E> type of the stored value | ||
28 | */ | ||
29 | public class Wrapper<E> { | ||
30 | public E value; | ||
31 | public Wrapper(E initialValue) { | ||
32 | value = initialValue; | ||
33 | } | ||
34 | public Wrapper() { | ||
35 | // default constructor | ||
36 | } | ||
37 | public void set(E newValue) { | ||
38 | value = newValue; | ||
39 | } | ||
40 | public E get() { | ||
41 | return value; | ||
42 | } | ||
43 | } | ||
diff --git a/src/test/java/org/gnunet/util/getopt/GetoptTest.java b/src/test/java/org/gnunet/util/getopt/GetoptTest.java new file mode 100644 index 0000000..010c240 --- /dev/null +++ b/src/test/java/org/gnunet/util/getopt/GetoptTest.java | |||
@@ -0,0 +1,286 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.gnunet.util.getopt; | ||
22 | |||
23 | |||
24 | import org.junit.Assert; | ||
25 | import org.junit.Test; | ||
26 | |||
27 | class Target { | ||
28 | @Argument( | ||
29 | action = ArgumentAction.STORE_STRING, | ||
30 | shortname = "s", | ||
31 | longname = "string", | ||
32 | argumentName = "SOME_STRING", | ||
33 | description = "just some string" | ||
34 | ) | ||
35 | String someString; | ||
36 | |||
37 | @Argument( | ||
38 | action = ArgumentAction.SET, | ||
39 | shortname = "y", | ||
40 | longname = "set", | ||
41 | description = "enable, default disabled" | ||
42 | ) | ||
43 | boolean set = false; | ||
44 | |||
45 | @Argument( | ||
46 | action = ArgumentAction.RESET, | ||
47 | shortname = "n", | ||
48 | longname = "reset", | ||
49 | description = "disable, default enabled" | ||
50 | ) | ||
51 | boolean reset = true; | ||
52 | |||
53 | |||
54 | @Argument( | ||
55 | action = ArgumentAction.STORE_NUMBER, | ||
56 | shortname = "w", | ||
57 | longname = "value", | ||
58 | description = "some value" | ||
59 | ) | ||
60 | int intVal = 0; | ||
61 | |||
62 | static int someConstant = 42; | ||
63 | } | ||
64 | |||
65 | class InvalidTarget { | ||
66 | @Argument(action = ArgumentAction.SET, shortname = "foo", longname = "bar", description = "bla bla") | ||
67 | boolean foo; | ||
68 | } | ||
69 | |||
70 | public class GetoptTest { | ||
71 | @Test | ||
72 | public void test_str() { | ||
73 | Target t = new Target(); | ||
74 | Parser p = new Parser(t); | ||
75 | |||
76 | |||
77 | t.someString = null; | ||
78 | |||
79 | // argument directly with shortopt | ||
80 | p.parse(new String[]{"-sfoo"}); | ||
81 | Assert.assertEquals("foo", t.someString); | ||
82 | |||
83 | t.someString = null; | ||
84 | |||
85 | // argument after shortopt | ||
86 | p.parse(new String[]{"-s", "foo"}); | ||
87 | Assert.assertEquals("foo", t.someString); | ||
88 | |||
89 | t.someString = null; | ||
90 | |||
91 | p.parse(new String[]{"--string=foo"}); | ||
92 | Assert.assertEquals("foo", t.someString); | ||
93 | |||
94 | t.someString = null; | ||
95 | |||
96 | p.parse(new String[]{"--string", "foo"}); | ||
97 | Assert.assertEquals("foo", t.someString); | ||
98 | |||
99 | |||
100 | t.someString = null; | ||
101 | |||
102 | // last argument counts | ||
103 | p.parse(new String[]{"--string", "bar", "--string", "foo"}); | ||
104 | Assert.assertEquals("foo", t.someString); | ||
105 | |||
106 | t.someString = null; | ||
107 | |||
108 | boolean thrown; | ||
109 | |||
110 | thrown = false; | ||
111 | |||
112 | // absence of argument throws ArgumentError (longopt) | ||
113 | try { | ||
114 | p.parse(new String[]{"--string"}); | ||
115 | } catch (Parser.ArgumentError e) { | ||
116 | thrown = true; | ||
117 | } | ||
118 | |||
119 | Assert.assertTrue(thrown); | ||
120 | |||
121 | thrown = false; | ||
122 | // absence of argument throws ArgumentError (shortopt) | ||
123 | try { | ||
124 | p.parse(new String[]{"-s"}); | ||
125 | } catch (Parser.ArgumentError e) { | ||
126 | thrown = true; | ||
127 | } | ||
128 | |||
129 | Assert.assertTrue(thrown); | ||
130 | |||
131 | t.someString = null; | ||
132 | |||
133 | // a way to specify an empty string | ||
134 | p.parse(new String[]{"--string="}); | ||
135 | Assert.assertEquals("", t.someString); | ||
136 | } | ||
137 | |||
138 | @Test | ||
139 | public void test_help() { | ||
140 | Target t = new Target(); | ||
141 | Parser p = new Parser(t); | ||
142 | |||
143 | String help = p.getHelp(); | ||
144 | |||
145 | Assert.assertTrue(help.contains("--string")); | ||
146 | Assert.assertTrue(help.contains("-s")); | ||
147 | |||
148 | Assert.assertTrue(help.contains("-y")); | ||
149 | Assert.assertTrue(help.contains("--set")); | ||
150 | } | ||
151 | |||
152 | |||
153 | @Test | ||
154 | public void test_bool() { | ||
155 | Target t = new Target(); | ||
156 | Parser p = new Parser(t); | ||
157 | |||
158 | p.parse(new String[]{"--set", "--reset"}); | ||
159 | Assert.assertTrue(t.set); | ||
160 | Assert.assertTrue(!t.reset); | ||
161 | |||
162 | t.set = false; | ||
163 | t.reset = true; | ||
164 | |||
165 | p.parse(new String[]{"-y", "-n"}); | ||
166 | |||
167 | Assert.assertTrue(t.set); | ||
168 | Assert.assertTrue(!t.reset); | ||
169 | |||
170 | t.set = false; | ||
171 | t.reset = true; | ||
172 | |||
173 | p.parse(new String[]{"-yn"}); | ||
174 | |||
175 | Assert.assertTrue(t.set); | ||
176 | Assert.assertTrue(!t.reset); | ||
177 | } | ||
178 | |||
179 | @Test | ||
180 | public void test_positional() { | ||
181 | Target t = new Target(); | ||
182 | Parser p = new Parser(t); | ||
183 | |||
184 | String[] rest = p.parse(new String[]{"--string=bla", "foo", "bar", "--set", "--", "--reset", "baz"}); | ||
185 | |||
186 | Assert.assertArrayEquals(new String[]{"foo", "bar", "--reset", "baz"}, rest); | ||
187 | } | ||
188 | |||
189 | |||
190 | @Test(expected = Parser.ArgumentError.class) | ||
191 | public void test_missingLongopt() { | ||
192 | Target t = new Target(); | ||
193 | Parser p = new Parser(t); | ||
194 | p.parse(new String[]{"--foobar"}); | ||
195 | } | ||
196 | |||
197 | @Test(expected = Parser.ArgumentError.class) | ||
198 | public void test_missingShortopt_1() { | ||
199 | Target t = new Target(); | ||
200 | Parser p = new Parser(t); | ||
201 | p.parse(new String[]{"-x"}); | ||
202 | } | ||
203 | |||
204 | @Test | ||
205 | public void test_long() { | ||
206 | Target t = new Target(); | ||
207 | Parser p = new Parser(t); | ||
208 | |||
209 | String[] rest; | ||
210 | |||
211 | rest = p.parse(new String[]{"-w5"}); | ||
212 | Assert.assertEquals(5, t.intVal); | ||
213 | |||
214 | rest = p.parse(new String[]{"-w", "5"}); | ||
215 | Assert.assertEquals(5, t.intVal); | ||
216 | |||
217 | rest = p.parse(new String[]{"--value=6"}); | ||
218 | Assert.assertEquals(6, t.intVal); | ||
219 | |||
220 | rest = p.parse(new String[]{"--value", "7"}); | ||
221 | Assert.assertEquals(7, t.intVal); | ||
222 | |||
223 | rest = p.parse(new String[]{"--value", "-7"}); | ||
224 | Assert.assertEquals(-7, t.intVal); | ||
225 | |||
226 | boolean thrown = false; | ||
227 | try { | ||
228 | rest = p.parse(new String[]{"--value", "x"}); | ||
229 | } catch (Parser.ArgumentError e) { | ||
230 | thrown = true; | ||
231 | } | ||
232 | Assert.assertTrue(thrown); | ||
233 | } | ||
234 | |||
235 | |||
236 | @Test(expected = Parser.ArgumentError.class) | ||
237 | public void test_missingNumberArgument_short() { | ||
238 | Target t = new Target(); | ||
239 | Parser p = new Parser(t); | ||
240 | |||
241 | p.parse(new String[]{"-w"}); | ||
242 | Assert.assertEquals(5, t.intVal); | ||
243 | } | ||
244 | |||
245 | @Test(expected = Parser.ArgumentError.class) | ||
246 | public void test_missingNumberArgument_long() { | ||
247 | Target t = new Target(); | ||
248 | Parser p = new Parser(t); | ||
249 | |||
250 | |||
251 | p.parse(new String[]{"--w"}); | ||
252 | Assert.assertEquals(5, t.intVal); | ||
253 | } | ||
254 | |||
255 | |||
256 | @Test(expected = Parser.ArgumentError.class) | ||
257 | public void test_invalidNumberFormat() { | ||
258 | Target t = new Target(); | ||
259 | Parser p = new Parser(t); | ||
260 | |||
261 | |||
262 | p.parse(new String[]{"-w", "abc"}); | ||
263 | Assert.assertEquals(5, t.intVal); | ||
264 | } | ||
265 | |||
266 | |||
267 | @Test | ||
268 | public void test_dashRest() { | ||
269 | Target t = new Target(); | ||
270 | Parser p = new Parser(t); | ||
271 | |||
272 | String[] rest; | ||
273 | |||
274 | rest = p.parse(new String[]{"-w", "123", "-"}); | ||
275 | Assert.assertArrayEquals(new String[]{"-"}, rest); | ||
276 | } | ||
277 | |||
278 | |||
279 | @Test(expected = AssertionError.class) | ||
280 | public void test_invalid() { | ||
281 | InvalidTarget it = new InvalidTarget(); | ||
282 | Parser p = new Parser(it); | ||
283 | |||
284 | p.parse(new String[]{"-foo"}); | ||
285 | } | ||
286 | } | ||
diff --git a/src/test/java/org/grothoff/RunaboutBenchmark.java b/src/test/java/org/grothoff/RunaboutBenchmark.java new file mode 100644 index 0000000..18f456d --- /dev/null +++ b/src/test/java/org/grothoff/RunaboutBenchmark.java | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | package org.grothoff; | ||
22 | |||
23 | |||
24 | public class RunaboutBenchmark { | ||
25 | |||
26 | public static class MyRunabout extends Runabout { | ||
27 | public void visit(String s) { | ||
28 | |||
29 | } | ||
30 | public void visit(Integer i) { | ||
31 | |||
32 | } | ||
33 | } | ||
34 | |||
35 | |||
36 | public static void main(String[] args) { | ||
37 | final int runs = 5000000; | ||
38 | |||
39 | Runabout r = new MyRunabout(); | ||
40 | |||
41 | long start = System.currentTimeMillis(); | ||
42 | |||
43 | Integer integer = 42; | ||
44 | for (int i = 0; i < runs; ++i) { | ||
45 | r.visitAppropriate("foo"); | ||
46 | r.visitAppropriate(integer); | ||
47 | } | ||
48 | |||
49 | long end = System.currentTimeMillis(); | ||
50 | |||
51 | long duration1 = end - start; | ||
52 | |||
53 | |||
54 | Runabout r2 = new Runabout() { | ||
55 | public void visit(String s) { | ||
56 | |||
57 | } | ||
58 | public void visit(Integer i) { | ||
59 | |||
60 | } | ||
61 | }; | ||
62 | |||
63 | |||
64 | start = System.currentTimeMillis(); | ||
65 | |||
66 | for (int i = 0; i < runs; ++i) { | ||
67 | r2.visitAppropriate("foo"); | ||
68 | r2.visitAppropriate(integer); | ||
69 | } | ||
70 | |||
71 | end = System.currentTimeMillis(); | ||
72 | |||
73 | long duration2 = end - start; | ||
74 | |||
75 | |||
76 | System.out.println("Runs: " + runs); | ||
77 | System.out.println("public: " + duration1); | ||
78 | System.out.println("Anon Inner Class: " + duration2); | ||
79 | System.out.println("Overhead: " + duration2 / (double) duration1); | ||
80 | |||
81 | } | ||
82 | } | ||