Skip to content

Instantly share code, notes, and snippets.

View mardambey's full-sized avatar
👾
Getting things done.

Hisham Mardam-Bey mardambey

👾
Getting things done.
View GitHub Profile
@mardambey
mardambey / LimitedOutputStream.scala
Created January 17, 2017 16:24
An output stream that throws an exception if more than the given bytes have been written to it.
import java.io.{FilterOutputStream, IOException, OutputStream}
class LimitedOutputStream(out: OutputStream , maxBytes: Long) extends FilterOutputStream(out) {
private var bytesWritten: Long = 0L
@throws(classOf[IOException])
override def write(b: Int) {
ensureCapacity(1)
super.write(b)
@mardambey
mardambey / fb-upload.html
Created June 13, 2015 14:20
POC client side Javascript image upload to Facebook's Graph API.
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<form>
<input type="file" accept="image/*" id="capture" capture="camera">
<img id="blah" src="#" alt="your image"/>
</form>
@mardambey
mardambey / JedisSentinelPoolRunner.java
Created August 24, 2013 19:05
Testing JedisSentinelPool subscribed to Redis Sentinel following +switch-master to find the new master.
package redis.clients.jedis;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
public class JedisSentinelPoolRunner {
private static Logger log = Logger.getLogger(JedisSentinelPoolRunner.class.getName());
@mardambey
mardambey / Crypto.scala
Created April 15, 2013 00:19
AES/PKCS5 - encrypted with Perl::CBC and decrypted in Scala/Java
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import java.math.BigInteger
import javax.crypto.SecretKeyFactory
import java.security.MessageDigest
import javax.xml.bind.DatatypeConverter._
object Crypto extends App {
@mardambey
mardambey / mod_log_remote.erl
Created August 23, 2012 03:44
mod_log_remote allows for logging Ejabberd messages / packets to a remote Erlang node.
%%
%% mod_log_remote is a simple Ejabberd module and gen_server that
%% allow for remote logging. It uses the filter_packet hook to
%% intercept <message> stanzas addressed to logger@vhost. These
%% messages are beamed off to the configured Erland node / pid in
%% the form:
%%
%% {Type, LogTime, Payload}
%%
%% where Type and Payload are specified by the caller and LogTime is
@mardambey
mardambey / ConsistentHashRouter.scala
Created August 23, 2012 02:22
An Akka router that sends messages to its routees based on a consistent hash.
object ConsistentHashRouter {
/**
* Creates a new ConsistentHashRouter, routing to the specified routees
*/
def apply(routees: Iterable[ActorRef]): ConsistentHashRouter =
new ConsistentHashRouter(routees = routees map (_.path.toString))
}
case class ConsistentHashRouter(
@mardambey
mardambey / AsyncValue.scala
Created May 12, 2012 01:51
An actor wrapping an object of type T providing async get/set calls without blocking (sacrificing accuracy).
import scala.actors.Actor
case class Set(value:Any)
case class Get()
case class Destroy()
/*
* An actor wrapping an object of type T providing async get/set
* calls without blocking (sacrificing accuracy).
*
@mardambey
mardambey / gist:2654382
Created May 10, 2012 16:41
Reset Kafka offsets in ZooKeeper by deleting the corresponding nodes.
/**
* Notes: This code uses AsyncValue[T], a custom class that uses actors
* to allow concurrent operations on the provided type. It can be replaced
* by an Atomic object from the java.util.concurrent package or something
* that provides similar functionality.
*/
/**
* Resets the offsets for the given group / topic pair.
@mardambey
mardambey / KafkaEmbedded.scala
Created May 10, 2012 02:58
Embedded Kafka broker / producer / simple consumer in a single process useful for testing or for persistent queues.
import java.util.Properties
import kafka.server.KafkaServer
import kafka.server.KafkaConfig
import kafka.producer.ProducerConfig
import kafka.producer.Producer
import kafka.message.Message
import kafka.producer.ProducerData
import kafka.consumer.ConsumerConfig
import kafka.consumer.Consumer
import kafka.utils.Utils
@mardambey
mardambey / AkkaKafkaMailboxTest.scala
Last active August 3, 2019 05:03
Akka 2.0 actors with Kafka 0.7.x backed durable mailboxes.
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.agent.Agent
import com.typesafe.config.ConfigFactory
import akka.event.Logging
import akka.actor.Props
import kafka.utils.Utils
import java.nio.ByteBuffer