diff --git a/iguana/exchanges/LP_commands.c b/iguana/exchanges/LP_commands.c index f15df7e9a..0ba142513 100644 --- a/iguana/exchanges/LP_commands.c +++ b/iguana/exchanges/LP_commands.c @@ -84,6 +84,7 @@ char *stats_JSON(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,char *r if ( laststr != 0 ) free(laststr); laststr = newstr; + LP_gotmessage(argjson); retstr = clonestr(laststr); } } @@ -112,18 +113,11 @@ getutxos(coin, lastn)\n\ orderbook(base, rel, duration=3600)\n\ getprices(base, rel)\n\ sendmessage(base=coin, rel="", pubkey=zero, )\n\ +getmessages(firsti=0, num=100)\n\ +clearmessages(firsti=0, num=100)\n\ trust(pubkey, trust)\n\ \"}")); - /* - register(pubkey,pushaddr)\n\ - registerall(numnodes)\n\ - lookup(pubkey)\n\ - forward(pubkey,method2,)\n\ - forward(pubkey,method2=publish,)\n\ - forwardhex(pubkey,hex)\n\ - */ - base = jstr(argjson,"base"); rel = jstr(argjson,"rel"); if ( USERPASS[0] != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 && port != 0 ) @@ -142,8 +136,22 @@ trust(pubkey, trust)\n\ jdelete(argjson,"userpass"); if ( strcmp(method,"sendmessage") == 0 ) { - printf("broadcast message\n"); - LP_broadcast_message(LP_mypubsock,base!=0?base:jstr(argjson,"coin"),rel,jbits256(argjson,"pubkey"),jprint(argjson,0)); + if ( jobj(argjson,"method2") == 0 ) + { + printf("broadcast message\n"); + LP_broadcast_message(LP_mypubsock,base!=0?base:jstr(argjson,"coin"),rel,jbits256(argjson,"pubkey"),jprint(argjson,0)); + } + return(clonestr("{\"result\":\"success\"}")); + } + else if ( strcmp(method,"getmessages") == 0 ) + { + if ( (retjson= LP_getmessages(jint(argjson,"firsti"),jint(argjson,"num"))) != 0 ) + return(jprint(retjson,1)); + else return(clonestr("{\"error\":\"null messages\"}")); + } + else if ( strcmp(method,"deletemessages") == 0 ) + { + LP_deletemessages(jint(argjson,"firsti"),jint(argjson,"num")); return(clonestr("{\"result\":\"success\"}")); } if ( base != 0 && rel != 0 ) diff --git a/iguana/exchanges/LP_messages.c b/iguana/exchanges/LP_messages.c new file mode 100644 index 000000000..76cfca051 --- /dev/null +++ b/iguana/exchanges/LP_messages.c @@ -0,0 +1,92 @@ + +/****************************************************************************** + * Copyright © 2014-2017 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ +// +// LP_messages.c +// marketmaker +// + +struct LP_messageinfo { struct LP_messageinfo *next,*prev; cJSON *msgjson; int32_t ind; } *LP_MSGS; +int32_t Num_messages; + +void LP_gotmessage(cJSON *argjson) +{ + struct LP_messageinfo *msg = calloc(1,sizeof(*msg)); + msg->msgjson = jduplicate(argjson); + msg->ind = Num_messages++; +} + +void LP_deletemessages(int32_t firsti,int32_t num) +{ + struct LP_messageinfo *msg,*tmp; int32_t lasti; + if ( num == 0 ) + num = 100; + if ( firsti < 0 ) + firsti = 0; + else if ( firsti >= Num_messages ) + return; + lasti = firsti + num - 1; + if ( lasti < Num_messages-1 ) + lasti = Num_messages - 1; + DL_FOREACH_SAFE(LP_MSGS,msg,tmp) + { + if ( msg->ind >= firsti && msg->ind <= lasti ) + { + portable_mutex_lock(&LP_messagemutex); + DL_DELETE(LP_MSGS,msg); + portable_mutex_unlock(&LP_messagemutex); + free_json(msg->msgjson); + free(msg); + } + } +} + +cJSON *LP_getmessages(int32_t firsti,int32_t num) +{ + struct LP_messageinfo *msg,*tmp; int32_t lasti,n=0,maxi=-1,mini=-1; cJSON *retjson,*item,*array = cJSON_CreateArray(); + retjson = cJSON_CreateObject(); + if ( num == 0 ) + num = 100; + if ( firsti < 0 ) + firsti = 0; + else if ( firsti >= Num_messages ) + { + jadd(retjson,"messages",array); + return(retjson); + } + lasti = firsti + num - 1; + if ( lasti < Num_messages-1 ) + lasti = Num_messages - 1; + DL_FOREACH_SAFE(LP_MSGS,msg,tmp) + { + if ( msg->ind >= firsti && msg->ind <= lasti ) + { + item = cJSON_CreateObject(); + jaddnum(item,"ind",msg->ind); + jadd(item,"msg",jduplicate(msg->msgjson)); + jaddi(array,item); + if ( mini == -1 || msg->ind < mini ) + mini = msg->ind; + if ( maxi == -1 || msg->ind > maxi ) + maxi = msg->ind; + n++; + } + } + jadd(retjson,"messages",array); + jaddnum(retjson,"minind",mini); + jaddnum(retjson,"maxind",maxi); + jaddnum(retjson,"num",n); + return(retjson); +} diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index 50487756a..6d09611fa 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -36,7 +36,7 @@ #include #include "LP_include.h" -portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex; +portable_mutex_t LP_peermutex,LP_UTXOmutex,LP_utxomutex,LP_commandmutex,LP_cachemutex,LP_swaplistmutex,LP_forwardmutex,LP_pubkeymutex,LP_networkmutex,LP_psockmutex,LP_coinmutex,LP_messagemutex; int32_t LP_canbind; struct LP_utxoinfo *LP_utxoinfos[2],*LP_utxoinfos2[2]; @@ -97,6 +97,7 @@ char *blocktrail_listtransactions(char *symbol,char *coinaddr,int32_t num,int32_ #include "LP_utxos.c" #include "LP_forwarding.c" #include "LP_ordermatch.c" +#include "LP_messages.c" #include "LP_commands.c" char *LP_command_process(void *ctx,char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *data,int32_t datalen) @@ -651,6 +652,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,uint16_t mybu portable_mutex_init(&LP_psockmutex); portable_mutex_init(&LP_coinmutex); portable_mutex_init(&LP_pubkeymutex); + portable_mutex_init(&LP_messagemutex); LP_sessionid = (uint32_t)time(NULL); printf("getting myipaddr sessionid.%u\n",LP_sessionid); if ( system("curl -s4 checkip.amazonaws.com > /tmp/myipaddr") == 0 )