Skip to content

Instantly share code, notes, and snippets.

@GuoJing
Created March 25, 2015 15:36
Show Gist options
  • Save GuoJing/e016efb1eb10dc5a702d to your computer and use it in GitHub Desktop.
Save GuoJing/e016efb1eb10dc5a702d to your computer and use it in GitHub Desktop.
ares thrift server
#import "AppDelegate.h"
#import "gen-cocoa/ares.h"
#import "thrift/Thrift.h"
#import "thrift/transport/TSSLSocketClient.h"
#import "thrift/transport/TTransport.h"
#import "thrift/transport/TSocketClient.h"
#import "thrift/transport/TFramedTransport.h"
#import "thrift/protocol/TBinaryProtocol.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
TSocketClient *client1 = [[TSocketClient alloc] initWithHostname:@"0.0.0.0" port:10001];
TFramedTransport *transport = [[TFramedTransport alloc] initWithTransport:client1];
TBinaryProtocol *protocol = [[TBinaryProtocol alloc] initWithTransport:transport strictRead:YES strictWrite:YES];
NSLog(@"come here");
AresClient *client = [[AresClient alloc] initWithInProtocol:protocol outProtocol:protocol];
NSLog(@"after client");
[client touch];
// Will raise No protocol version header error
// which code is in protocol/TBinaryProtocol.py line 140
NSLog(@"hello");
}
@end
import sys
sys.path.append('gen-py')
from ares import Ares
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
# Make socket
transport = TSocket.TSocket('0.0.0.0', 10001)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TFramedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = Ares.Client(protocol)
# Connect!
transport.open()
# It works!
r = client.touch()
print r
except:
pass
import sys
sys.path.append('gen-py')
from ares import Ares
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class AresHandler:
def touch(self):
print 'touch here'
return 'OK'
handler = AresHandler()
processor = Ares.Processor(handler)
transport = TSocket.TServerSocket('0.0.0.0', port=10001)
tfactory = TTransport.TFramedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory(strictRead=True)
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment