Skip to content

Instantly share code, notes, and snippets.

View chrishulbert's full-sized avatar

Chris Hulbert chrishulbert

View GitHub Profile
@chrishulbert
chrishulbert / main.rs
Created August 27, 2023 13:15
Interactive Brokers TWS API in Rust
// MIT licensed.
use std::net::TcpStream;
use std::io::{Read, Write};
use std::sync::mpsc::channel;
use std::thread;
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:7496").expect("connect");
stream.set_nodelay(true).expect("nodelay"); // Because we're wannabe HFT traders.
@chrishulbert
chrishulbert / ssdp.node.js
Created March 30, 2011 21:46
Service discovery using node.js and ssdp / universal plug n play
var dgram = require('dgram'); // dgram is UDP
// Listen for responses
function listen(port) {
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
});
@chrishulbert
chrishulbert / launchdexample.xml
Created February 21, 2012 10:13
launchd example plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mycompanyname.mydepartment.mytaskname</string>
<key>ProgramArguments</key>
<array>
<string>/Users/myuser/Dashboard/mytask.sh</string>
</array>
@chrishulbert
chrishulbert / close_actionsheet_above_tap.m
Created May 30, 2011 05:34
Allow closing a UIActionSheet by tapping above it, in the shaded area
// For detecting taps outside of the alert view
-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self];
if (p.y < 0) { // They tapped outside
[self dismissWithClickedButtonIndex:0 animated:YES];
}
}
-(void) showFromTabBar:(UITabBar *)view {
[super showFromTabBar:view];
@chrishulbert
chrishulbert / curvisview.m
Created May 25, 2011 05:00
Finding the currently visible view in a UITabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Firstly, we want to figure out who is the currently visible view controller.
// There are 4 possibilities here:
// 1- They could be a non-nav controller, with a visible tab (ie not under the 'more' tab)
// Simple: self.selectedViewController
// 2- They could be a nav controller, with a visible tab (ie not under the 'more' tab)
// Get it's visible view: self.selectedViewController.visibleViewController
// 3- They could be a non-nav controller, under the 'more' tab
// Simple: self.selectedViewController
// 4- They could be a nav controller, under the 'more' tab
@chrishulbert
chrishulbert / gist:902774
Created April 5, 2011 00:24
Python unix socket example
import socket
minissdpdSocket = '/var/run/minissdpd.sock' # The socket for talking to minissdpd
# Sends a message to the given socket
def sendMessage(message):
s = socket.socket(socket.AF_UNIX)
s.settimeout(1) # 1 second timeout, after all it should be instant because its local
s.connect(minissdpdSocket)
s.send(message)
@chrishulbert
chrishulbert / ADXL345.c
Created September 24, 2011 06:23
Read ADXL345 accelerometer on arduino
#include <Wire.h>
#define DEVICE (0x53) // ADXL345 device address when the SDO pin (12) is grounded
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
// Wake up the accelerometer
@chrishulbert
chrishulbert / line.js
Created July 11, 2011 06:34
Reading a line from the console in node.js
console.log ('Input some code:');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.once('data', function (someCode) {
process.stdin.pause();
console.log ('Code: ' + someCode);
});
@chrishulbert
chrishulbert / Property.swift
Created July 23, 2015 10:14
Example of a simple Swift alternative to KVO. Inspired by reactive cocoa's MutableProperty. Weakly-referenced subscribers and all.
import Foundation
// Simple observable property
// Careful: Not thread safe! You should use this only from the main thread, or modify to
// have locks.
class Property<T> {
private var _value: T
var value: T {
get { return _value }
set {
@chrishulbert
chrishulbert / bcastpacket.m
Created April 6, 2011 01:23
Send a broadcast udp message using c / obj-c
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
- (void)sendBroadcastPacket {
// Open a socket
int sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sd<=0) {
NSLog(@"Error: Could not open socket");