1 package com.tudelft.triblerdroid.swift; 2 3 /* 4 * JNI interface for the libswift P2P engine. 5 * 6 * Written by Riccardo Petrocco and Arno Bakker 7 */ 8 9 public class NativeLib { 10 11 static { 12 //System.loadLibrary("swift"); 13 // System.loadLibrary("event"); 14 System.loadLibrary("cryptossl"); 15 } 16 17 /** 18 * Initialize swift engine. MUST be called before any other functions. 19 * Not thread-safe, only 1 global call allowed. 20 * 21 * @param listenaddr IP address and port to listen to, or empty 22 * (=IP 0.0.0.0 and port randomly chosen) 23 * @param httpgwaddr IP address and port for internal HTTP server to listen to 24 * @return empty string (=OK) or error string. 25 */ Init( String listenaddr, String httpgwaddr )26 public native String Init( String listenaddr, String httpgwaddr ); 27 28 /** 29 * Enter swift mainloop. Does not exit till shutdown is called! 30 * Not thread-safe, only 1 global call allowed. 31 */ Mainloop()32 public native void Mainloop(); 33 34 /** 35 * Shutdown swift engine. 36 * Thread-safe. 37 * 38 * @return empty string (=OK) or error string. 39 */ Shutdown()40 public native void Shutdown(); 41 42 /** 43 * Retrieve result of previous async* call, if available. 44 * Thread-safe. 45 * 46 * @param callid as returned by async* call 47 * @return "n/a" if not yet available or as described in async* call 48 */ asyncGetResult(int callid)49 public native String asyncGetResult(int callid); 50 51 52 /** 53 * Start swift download. For streaming via the internal HTTP server do not 54 * call this method, just do a HTTP GET /roothash-in-hex on the HTTPGW 55 * address configured via Init(). Append "@-1" to the path for live streams. 56 * 57 * If content is already on disk (e.g. starting a seed) and the swift engine 58 * finds a checkpoint for this content (i.e., a filename.mhash and 59 * filename.mbinmap) it will not hash check. 60 * 61 * If the swarmid is all 0 and no checkpoint is found, the swift engine 62 * will hash check the content in filename. This may take a while, so the 63 * asyncGetResult will not yield a result quickly. Moreover, all network 64 * traffic will also be halted during this period. To prevent this situation 65 * you should call hashCheckOffline(filename) beforehand, see below. 66 * 67 * Thread-safe. 68 * 69 * @param swarmid SwarmID in hex, may be all 0 for seeding. 70 * @param tracker tracker for this download. 71 * @param filename Location where to store content. 72 * @return callid or -1 on error. 73 * asyncGetResult(callid) will return swarmid (=OK) or error string. 74 */ asyncOpen( String swarmid, String tracker, String filename )75 public native int asyncOpen( String swarmid, String tracker, String filename ); 76 77 78 /** 79 * Remove swift download. 80 * Thread-safe. 81 * 82 * @param swarmid SwarmID in hex. 83 * @param removestate whether to remove an existing checkpoint. 84 * @param removecontent whether to remove the content already on disk. 85 * @return callid or -1 on error. 86 * asyncGetResult(callid) will return swarmid (=OK) or error string. 87 */ asyncClose( String swarmid, boolean removestate, boolean removecontent )88 public native int asyncClose( String swarmid, boolean removestate, boolean removecontent ); 89 90 91 /** 92 * Write a swift checkpoint for the specified file. 93 * 94 * Thread-safe. 95 * 96 * @param filename Location where content is stored. 97 * @return swarmid (=OK) or error string. 98 */ hashCheckOffline( String filename )99 public native String hashCheckOffline( String filename ); 100 101 102 103 /** 104 * Set default swift tracker. 105 * Thread-safe. 106 * 107 * @param tracker tracker for this download 108 */ SetTracker( String tracker )109 public native void SetTracker( String tracker ); 110 111 112 /** 113 * Returns progress for the specified swift download streamed via the HTTPGW 114 * as a fraction of the number of bytes written to the HTTP socket and the 115 * total size of the content (0 for live). 116 * Thread-safe. 117 * 118 * @param swarmid SwarmID in hex 119 * @return callid or -1 on error. 120 * asyncGetResult(callid) will return "xxx/yyy" 121 */ asyncGetHTTPProgress( String swarmid )122 public native int asyncGetHTTPProgress( String swarmid ); 123 124 125 /** 126 * Returns statistics for the specified swift download following KTH spec. 127 * Thread-safe. 128 * 129 * @param swarmid SwarmID in hex 130 * @return callid -1 on error. 131 * asyncGetResult(callid) will return "a/b/c/d/e/f" 132 */ asyncGetStats( String swarmid )133 public native int asyncGetStats( String swarmid ); 134 135 136 /** 137 * Create a live swarm (currently just one). 138 * Not thread-safe, only 1 global call allowed. 139 * 140 * @param swarmid public key to identify swarm (future) 141 * @return empty string (=OK) or error string 142 */ LiveCreate( String swarmid )143 public native String LiveCreate( String swarmid ); 144 145 146 /** 147 * Pass data to live source. 148 * Thread-safe. 149 * 150 * @param swarmid public key to identify swarm 151 * @param data content 152 * @param offset offset of content in data array (UNUSED) 153 * @param length size of the content 154 * @returns empty string (=OK) or error string. 155 */ LiveAdd( String swarmid, byte[] data, int offset, int length )156 public native String LiveAdd( String swarmid, byte[] data, int offset, int length ); 157 }