Skip to content

Instantly share code, notes, and snippets.

View BrunoBonacci's full-sized avatar

Bruno Bonacci BrunoBonacci

View GitHub Profile
@BrunoBonacci
BrunoBonacci / snippet1.groovy
Created July 1, 2012 14:04
Vert.x Simple Telnet Server - session management
// this is the base directory where all user will be segregated (like: "chroot")
BASE_DIR = new File(System.properties.baseDir ?: "/tmp").canonicalPath;
// This map maintains the session state for every connection.
// for this telnet server the state will be limited to the current directory
_connections = [:];
// Connection id counter
_cids = 0;
@BrunoBonacci
BrunoBonacci / gist:3028571
Created July 1, 2012 14:22
Vert.x Simple Telnet Server - NIO Server
// general configuration
settings = [port: 1234];
// Vert.x server and event-bus
def server = vertx.createNetServer()
def bus = vertx.eventBus
// Connection handler. Here requests are captured, parsed, and executed.
server.connectHandler { sock ->
// upon a new connection, assigning a new connection id (cid)
@BrunoBonacci
BrunoBonacci / gist:3028594
Created July 1, 2012 14:29
Vert.x Simple Telnet Server - command handler
// Regular expression to verify/extract pathnames from commands
PATH_ELEM = "[A-Za-z0-9.-]+"
PATH = "(/?$PATH_ELEM(/$PATH_ELEM)*/?|/)"
// Commands handler
bus.registerHandler("commands") {
msg ->
def cmd = msg.body.command
switch (cmd) {
case ~$/cd( $PATH)?/$:
@BrunoBonacci
BrunoBonacci / gist:3028607
Created July 1, 2012 14:34
Vert.x Simple Telnet Server - command "cd"
////////////////////////////////////////////////////////////////////////// cd
def cd(def path, String cid) {
path = path == "" ? null : path
def newPath = virtual2real(path, cid);
if (!newPath.exists() || !newPath.isDirectory())
return [status: "ERROR", message: "### ERROR: $path: no such directory!".toString()]
// changing directory
_connections[cid].curDir = new File(newPath.canonicalPath);
@BrunoBonacci
BrunoBonacci / gist:5009721
Last active December 14, 2015 01:58
MongoDB Replica set configuration
# this is the replica set name
# every node will need to have the same name
replSet = setname
# this is the authetication encryption key
# this will be required to be same on every node
keyFile = /etc/mongodb.key
# This is the operational log size
# here we assume ~24GB for example
@BrunoBonacci
BrunoBonacci / gist:5009746
Created February 22, 2013 00:22
MongoDB key generation
# generate the key and make it readable only by mongo server
sudo openssl rand -base64 753 > /etc/mongodb.key
sudo chown mongod:mongod /etc/mongodb.key
sudo chmod 0600 /etc/mongodb.key
@BrunoBonacci
BrunoBonacci / gist:5009801
Created February 22, 2013 00:32
creating zero-filled oplog files.
cd /var/lib/mongo/local
# as root user
for i in {0..11}
do
echo "Initializing new-local.$i"
dd if=/dev/zero of=new-local.$i bs=2146435072 count=1
chown mongod:mongod new-local.$i
chmod 0600 new-local.$i
done
# stopping the server
sudo /etc/init.d/mongod stop
# now replace local.n files
cd /var/lib/mongo/local
sudo rm -fr local.*
sudo rename 's/new-local/local/g' new-local.*
# restarting the server
sudo /etc/init.d/mongod start
@BrunoBonacci
BrunoBonacci / gist:5225781
Created March 23, 2013 00:32
Reminder vs bitwise operation benchmark
/*
* this benchmark compares the speed of
* the reminder operator vs the bitwise
* equivalent.
*/
public class ReminderBenchmark {
final static long d = 8;
final static long mask = d-1;
final static long REPEAT = 1_000_000L;
@BrunoBonacci
BrunoBonacci / gist:cb13fc05bd720652eb17
Created May 16, 2015 11:39
Crypto Square problem
(defn crypto-square [s]
(->> (re-seq #"\w+" s)
(map clojure.string/lower-case)
(apply str)
(#(let [size (int (Math/ceil (Math/sqrt (count %))))]
(partition size size (repeat " ") %)))
(apply map vector)
(map (partial apply str))
(clojure.string/join "\n")))