Skip to content

Commit 6edbc4b

Browse files
committed
copy sources from internal project
1 parent 39087a4 commit 6edbc4b

15 files changed

+493
-15
lines changed

.idea/.name

Lines changed: 0 additions & 1 deletion
This file was deleted.

.idea/compiler.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/geofancy.iml renamed to .idea/modules/geofancy-java.iml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/geofancy_main.iml renamed to .idea/modules/geofancy-java_main.iml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/geofancy_test.iml renamed to .idea/modules/geofancy-java_test.iml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
rootProject.name = 'geofancy'
1+
rootProject.name = 'geofancy-java'
22

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package za.co.movinggauteng.geofancy
2+
3+
import io.github.cdimascio.dotenv.dotenv
4+
import io.jaegertracing.Configuration
5+
import io.opentracing.util.GlobalTracer
6+
import za.co.movinggauteng.geofancy.grpc.*
7+
8+
val dotenv = dotenv {
9+
ignoreIfMalformed = true
10+
ignoreIfMissing = true
11+
}
12+
13+
fun main(args: Array<String>) {
14+
15+
val tracerConfig = Configuration("geofancy-java")
16+
tracerConfig.withReporter(Configuration.ReporterConfiguration()
17+
.withSender(Configuration.SenderConfiguration()
18+
.withEndpoint(dotenv["OPENTRACING_ENDPOINT"])))
19+
val tracer = tracerConfig.tracer
20+
21+
GlobalTracer.register(tracer)
22+
23+
val server = GeofancyServer((dotenv["GRPC_SERVER_PORT"] ?: "9003").toInt())
24+
server.start()
25+
26+
server.blockUntilShutdown()
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package za.co.movinggauteng.geofancy;
2+
3+
4+
import io.lettuce.core.protocol.LettuceCharsets;
5+
import io.lettuce.core.protocol.ProtocolKeyword;
6+
7+
public enum Tile38ProtocolCommand implements ProtocolKeyword
8+
{
9+
SETHOOK("SETHOOK"),
10+
NEARBY("NEARBY"),
11+
DELHOOK("DELHOOK"),
12+
PDELHOOK("PDELHOOK"),
13+
DROP("DROP");
14+
15+
private final byte[] name;
16+
17+
Tile38ProtocolCommand(String commandName) {
18+
name = commandName.getBytes(LettuceCharsets.ASCII);
19+
}
20+
21+
public byte[] getBytes() {
22+
return name;
23+
}
24+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package za.co.movinggauteng.geofancy.geoclient
2+
3+
import io.github.cdimascio.dotenv.dotenv
4+
import io.lettuce.core.RedisClient
5+
import io.lettuce.core.api.sync.RedisCommands
6+
import io.lettuce.core.codec.StringCodec
7+
import io.lettuce.core.output.MapOutput
8+
import io.lettuce.core.output.StatusOutput
9+
import io.lettuce.core.protocol.CommandArgs
10+
import io.lettuce.core.protocol.CommandType
11+
import za.co.movinggauteng.geofancy.Tile38ProtocolCommand
12+
import za.co.movinggauteng.protos.geofancy.Bounds
13+
import za.co.movinggauteng.protos.geofancy.GeoFence
14+
import za.co.movinggauteng.protos.geofancy.GeoFence.QueryCase.*
15+
import za.co.movinggauteng.protos.geofancy.Point
16+
import za.co.movinggauteng.protos.geofancy.SearchString
17+
18+
class Tile38Client {
19+
20+
private val redisClient: RedisClient = RedisClient.create(env["TILE38_CONNECTION"])
21+
22+
private val connection = redisClient.connect()
23+
24+
private val sync: RedisCommands<String, String> = connection.sync()
25+
26+
private val codec: StringCodec = StringCodec.UTF8
27+
28+
companion object {
29+
private val env = dotenv()
30+
}
31+
32+
fun setPoint(collection: String, id: String, point: Point) : String {
33+
return sync.dispatch(
34+
CommandType.SET,
35+
StatusOutput(codec),
36+
CommandArgs(codec)
37+
.add(collection)
38+
.add(id)
39+
.add("POINT")
40+
.add(point.coord.lat)
41+
.add(point.coord.lng)
42+
)
43+
}
44+
45+
fun setBounds(collection: String, id: String, bounds: Bounds) {
46+
TODO("Not yet implemented")
47+
}
48+
49+
fun setWebhook(geofence: GeoFence): MutableMap<String, String>? {
50+
val commands = CommandArgs(codec)
51+
// load commands
52+
commands.add(geofence.id)
53+
commands.add(geofence.endpoint)
54+
when(geofence.queryCase) {
55+
56+
NEARBY -> {
57+
commands.add(geofence.queryCase.name)
58+
commands.add(geofence.nearby.collection)
59+
}
60+
WITHIN -> TODO()
61+
INTERSECTS -> TODO()
62+
QUERY_NOT_SET -> TODO()
63+
}
64+
// filter
65+
commands.add("MATCH")
66+
commands.add(geofence.match)
67+
commands.add("FENCE")
68+
if (geofence.detectCount > 0) {
69+
commands.add("DETECT")
70+
commands.add(geofence.detectList.joinToString(",").toLowerCase())
71+
}
72+
if (geofence.commandsCount > 0) {
73+
commands.add("COMMANDS")
74+
commands.add(geofence.commandsList.joinToString(",").toLowerCase())
75+
}
76+
commands.add("POINT")
77+
commands.add(geofence.point.coord.lat)
78+
commands.add(geofence.point.coord.lng)
79+
commands.add(geofence.distance)
80+
81+
return sync.dispatch(Tile38ProtocolCommand.SETHOOK, MapOutput(codec), commands)
82+
}
83+
84+
fun setObject(collection: String, id: String, geoJson: String): String {
85+
return sync.dispatch(
86+
CommandType.SET,
87+
StatusOutput(codec),
88+
CommandArgs(codec)
89+
.add(collection)
90+
.add(id)
91+
.add("OBJECT")
92+
.add(geoJson)
93+
)
94+
}
95+
96+
fun deleteDocument(collection: String, id: String): String {
97+
return sync.dispatch(
98+
CommandType.DEL,
99+
StatusOutput(codec),
100+
CommandArgs(codec)
101+
.add(collection)
102+
.add(id)
103+
)
104+
}
105+
106+
fun deleteCollection(collection: String): String {
107+
return sync.dispatch(
108+
Tile38ProtocolCommand.DROP,
109+
StatusOutput(codec),
110+
CommandArgs(codec)
111+
.add(collection)
112+
)
113+
}
114+
115+
fun deleteWebhook(searchString: SearchString) : MutableMap<String, String>? {
116+
val commands = CommandArgs(codec)
117+
commands.add(searchString.value)
118+
119+
return sync.dispatch(Tile38ProtocolCommand.PDELHOOK, MapOutput(codec), commands)
120+
}
121+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package za.co.movinggauteng.geofancy.grpc
2+
3+
import io.grpc.ManagedChannel
4+
import io.grpc.ManagedChannelBuilder
5+
import io.grpc.Metadata
6+
import io.grpc.stub.MetadataUtils
7+
import za.co.movinggauteng.protos.geofancy.*
8+
import java.util.concurrent.Executors
9+
import java.util.concurrent.TimeUnit
10+
11+
class GeofancyClient(channelBuilder: ManagedChannelBuilder<*>) {
12+
val channel: ManagedChannel = channelBuilder.build()
13+
private var greeterBlockingStub: GeofancyServiceGrpc.GeofancyServiceBlockingStub
14+
private var greeterStub: GeofancyServiceGrpc.GeofancyServiceStub
15+
16+
constructor(host: String, port: Int, plainText: Boolean = true) : this(
17+
when(plainText) {
18+
true -> ManagedChannelBuilder.forAddress(host, port).usePlaintext().executor(Executors.newFixedThreadPool(16))
19+
false -> ManagedChannelBuilder.forAddress(host, port).executor(Executors.newFixedThreadPool(16))
20+
}
21+
)
22+
23+
init {
24+
val metadata: Metadata = Metadata()
25+
// metadata.put(io.grpc.Metadata.Key.of("Authorization", io.grpc.Metadata.ASCII_STRING_MARSHALLER), "${accessToken.tokenType} ${accessToken.accessToken}")
26+
greeterBlockingStub = MetadataUtils.attachHeaders(GeofancyServiceGrpc.newBlockingStub(channel), metadata)
27+
greeterStub = MetadataUtils.attachHeaders(GeofancyServiceGrpc.newStub(channel), metadata)
28+
}
29+
30+
@Throws(InterruptedException::class)
31+
fun shutdown() {
32+
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
33+
}
34+
35+
fun isShutdown() : Boolean = channel.isShutdown
36+
37+
fun getBlockingStub() : GeofancyServiceGrpc.GeofancyServiceBlockingStub {
38+
if (channel.isShutdown) {
39+
val metadata: Metadata = Metadata()
40+
// metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "${accessToken!!.tokenType} ${accessToken!!.accessToken}")
41+
greeterBlockingStub = MetadataUtils.attachHeaders(GeofancyServiceGrpc.newBlockingStub(channel), metadata)
42+
return greeterBlockingStub
43+
}
44+
return greeterBlockingStub
45+
}
46+
47+
fun getStub() : GeofancyServiceGrpc.GeofancyServiceStub {
48+
if (channel.isShutdown) {
49+
val metadata: Metadata = Metadata()
50+
// metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "${accessToken!!.tokenType} ${accessToken!!.accessToken}")
51+
greeterStub = MetadataUtils.attachHeaders(GeofancyServiceGrpc.newStub(channel), metadata)
52+
return greeterStub
53+
}
54+
return greeterStub
55+
}
56+
}

0 commit comments

Comments
 (0)